gitlab-org--gitlab-foss/spec/lib/gitlab
Yorick Peterse ac382b5682
Use CTEs for nested groups and authorizations
This commit introduces the usage of Common Table Expressions (CTEs) to
efficiently retrieve nested group hierarchies, without having to rely on
the "routes" table (which is an _incredibly_ inefficient way of getting
the data). This requires a patch to ActiveRecord (found in the added
initializer) to work properly as ActiveRecord doesn't support WITH
statements properly out of the box.

Unfortunately MySQL provides no efficient way of getting nested groups.
For example, the old routes setup could easily take 5-10 seconds
depending on the amount of "routes" in a database. Providing vastly
different logic for both MySQL and PostgreSQL will negatively impact the
development process. Because of this the various nested groups related
methods return empty relations when used in combination with MySQL.

For project authorizations the logic is split up into two classes:

* Gitlab::ProjectAuthorizations::WithNestedGroups
* Gitlab::ProjectAuthorizations::WithoutNestedGroups

Both classes get the fresh project authorizations (= as they should be
in the "project_authorizations" table), including nested groups if
PostgreSQL is used. The logic of these two classes is quite different
apart from their public interface. This complicates development a bit,
but unfortunately there is no way around this.

This commit also introduces Gitlab::GroupHierarchy. This class can be
used to get the ancestors and descendants of a base relation, or both by
using a UNION. This in turn is used by methods such as:

* Namespace#ancestors
* Namespace#descendants
* User#all_expanded_groups

Again this class relies on CTEs and thus only works on PostgreSQL. The
Namespace methods will return an empty relation when MySQL is used,
while User#all_expanded_groups will return only the groups a user is a
direct member of.

Performance wise the impact is quite large. For example, on GitLab.com
Namespace#descendants used to take around 580 ms to retrieve data for a
particular user. Using CTEs we are able to reduce this down to roughly 1
millisecond, returning the exact same data.

== On The Fly Refreshing

Refreshing of authorizations on the fly (= when
users.authorized_projects_populated was not set) is removed with this
commit. This simplifies the code, and ensures any queries used for
authorizations are not mutated because they are executed in a Rails
scope (e.g. Project.visible_to_user).

This commit includes a migration to schedule refreshing authorizations
for all users, ensuring all of them have their authorizations in place.
Said migration schedules users in batches of 5000, with 5 minutes
between every batch to smear the load around a bit.

== Spec Changes

This commit also introduces some changes to various specs. For example,
some specs for ProjectTeam assumed that creating a personal project
would _not_ lead to the owner having access, which is incorrect. Because
we also no longer refresh authorizations on the fly for new users some
code had to be added to the "empty_project" factory. This chunk of code
ensures that the owner's permissions are refreshed after creating the
project, something that is normally done in Projects::CreateService.
2017-05-17 16:51:08 +02:00
..
auth Make Warden set_user hook validate user ip uniquness 2017-03-06 15:41:25 +01:00
backup Enable the Style/TrailingCommaInLiteral cop 2017-05-10 18:25:45 +02:00
badge Revert "Enable Style/DotPosition" 2017-02-23 09:33:19 -06:00
bitbucket_import Revert "Enable Style/DotPosition" 2017-02-23 09:33:19 -06:00
cache/ci Use CTEs for nested groups and authorizations 2017-05-17 16:51:08 +02:00
chat_commands Fix chatops specs with incorrectly defined project 2017-04-06 17:15:15 +02:00
checks Add confirm delete protected branch modal 2017-05-08 07:41:58 +00:00
ci Merge branch 'allow_numeric_values_in_gitlab_ci_yml' into 'master' 2017-05-15 07:59:45 +00:00
conflict Fix conflict resolution from corrupted upstream 2017-05-12 20:47:51 +01:00
cycle_analytics Fix specs 2 2017-05-08 11:29:10 -03:00
data_builder Add username field to push webhook 2017-05-10 17:11:36 -04:00
database Merge branch 'rename-column-concurrently-defaults' into 'master' 2017-05-15 07:50:44 +00:00
dependency_linker Remove unrelated link from spec 2017-05-12 11:20:33 -05:00
diff Merge branch 'dm-dependency-linker-gemfile' into 'master' 2017-05-12 17:27:42 +00:00
downtime_check
email Handle incoming emails from aliases correctly 2017-05-03 20:51:10 +01:00
etag_caching Test all enabled routes in ETag caching middleware and fix pipeline routes 2017-04-10 10:45:37 +02:00
fogbugz_import
gfm Revert "Enable Style/BarePercentLiterals" 2017-02-23 09:32:42 -06:00
git Enable the Style/TrailingCommaInArguments cop 2017-05-10 18:25:45 +02:00
gitaly_client Add support for deltas_only under Gitaly 2017-05-13 18:27:06 +02:00
github_import Backport of multiple_assignees_feature [ci skip] 2017-05-04 17:11:53 +03:00
gitlab_import Revert "Prefer leading style for Style/DotPosition" 2017-02-23 09:33:05 -06:00
google_code_import Backport of multiple_assignees_feature [ci skip] 2017-05-04 17:11:53 +03:00
graphs
health_checks Set minimum latency to be non-negative number. 2017-05-05 11:34:01 +02:00
import_export Use CTEs for nested groups and authorizations 2017-05-17 16:51:08 +02:00
ldap Auto-correct RSpec/DescribedClass violations 2017-05-01 11:13:33 -04:00
metrics Revert "Prefer leading style for Style/DotPosition" 2017-02-23 09:33:05 -06:00
middleware Fix go-get support for projects in nested groups 2017-03-07 19:40:19 -06:00
o_auth Ensures that OAuth/LDAP/SAML users don't need to be confirmed 2017-04-26 10:08:44 +02:00
prometheus/queries match query times with appropriate delta 2017-05-11 23:24:26 +02:00
saml Ensures that OAuth/LDAP/SAML users don't need to be confirmed 2017-04-26 10:08:44 +02:00
sanitizers
serializer Revert "Enable Style/DotPosition" 2017-02-23 09:33:19 -06:00
sherlock Revert "Prefer leading style for Style/DotPosition" 2017-02-23 09:33:05 -06:00
sidekiq_status Periodically mark projects that are stuck in importing as failed 2017-04-05 16:11:51 +02:00
slash_commands Display slash commands outcome when previewing Markdown 2017-05-04 17:02:25 +02:00
sql Use CTEs for nested groups and authorizations 2017-05-17 16:51:08 +02:00
template Auto-correct RSpec/DescribedClass violations 2017-05-01 11:13:33 -04:00
view/presenter Revert "Prefer leading style for Style/DotPosition" 2017-02-23 09:33:05 -06:00
allowable_spec.rb
asciidoc_spec.rb Merge branch 'bvl-security-9-1-validate-urls-in-markdown-using-uri' 2017-05-10 16:44:20 +02:00
auth_spec.rb Enable the Style/TrailingCommaInArguments cop 2017-05-10 18:25:45 +02:00
blame_spec.rb
changes_list_spec.rb Auto-correct RSpec/DescribedClass violations 2017-05-01 11:13:33 -04:00
chat_name_token_spec.rb
closing_issue_extractor_spec.rb Revert "Prefer leading style for Style/DotPosition" 2017-02-23 09:33:05 -06:00
color_schemes_spec.rb
contributions_calendar_spec.rb Enable the Style/TrailingCommaInArguments cop 2017-05-10 18:25:45 +02:00
current_settings_spec.rb Auto-correct RSpec/DescribedClass violations 2017-05-01 11:13:33 -04:00
database_spec.rb removes redundant code from database.rb 2017-04-11 20:20:19 +01:00
dependency_linker_spec.rb Autolink package names in Gemfile 2017-05-10 08:26:21 -05:00
downtime_check_spec.rb Revert "Prefer leading style for Style/DotPosition" 2017-02-23 09:33:05 -06:00
exclusive_lease_spec.rb
file_detector_spec.rb Revert "Prefer leading style for Style/DotPosition" 2017-02-23 09:33:05 -06:00
file_finder_spec.rb Backport FileFinder from EE 2017-05-12 13:21:30 +03:00
git_access_spec.rb Add confirm delete protected branch modal 2017-05-08 07:41:58 +00:00
git_access_wiki_spec.rb
git_ref_validator_spec.rb Enable RSpec/FilePath cop 2017-04-26 12:50:32 +01:00
git_spec.rb Don't use FFaker in factories, use sequences instead 2017-04-03 18:54:48 +02:00
gitaly_client_spec.rb Add support for Gitaly calls over TCP connection 2017-04-03 18:45:36 +02:00
gl_repository_spec.rb Generate and handle a gl_repository param to pass around components 2017-05-03 17:37:30 -03:00
group_hierarchy_spec.rb Use CTEs for nested groups and authorizations 2017-05-17 16:51:08 +02:00
highlight_spec.rb Autolink package names in Gemfile 2017-05-10 08:26:21 -05:00
i18n_spec.rb Add CHANGELOG entry, some specs and locale file for German. 2017-05-04 18:44:19 -05:00
identifier_spec.rb Revert "Prefer leading style for Style/DotPosition" 2017-02-23 09:33:05 -06:00
import_sources_spec.rb Fix new offenses 2017-02-23 09:32:42 -06:00
incoming_email_spec.rb Revert "Enable Style/DotPosition" 2017-02-23 09:33:19 -06:00
issuable_sorter_spec.rb Change issues sentence to use natural sorting 2017-04-25 18:57:08 -05:00
job_waiter_spec.rb Revert "Prefer leading style for Style/DotPosition" 2017-02-23 09:33:05 -06:00
key_fingerprint_spec.rb
kubernetes_spec.rb Enable Style/ColonMethodCall 2017-02-23 09:31:57 -06:00
lazy_spec.rb
lfs_token_spec.rb
markup_helper_spec.rb
metrics_spec.rb Auto-correct RSpec/DescribedClass violations 2017-05-01 11:13:33 -04:00
optimistic_locking_spec.rb Add basic specs for optimistic locking mixin 2017-02-23 12:07:57 +01:00
other_markup_spec.rb Merge branch 'bvl-security-9-1-markup-pipeline' 2017-05-10 16:44:20 +02:00
polling_interval_spec.rb Convert value to string before setting Poll-Interval header 2017-04-05 15:35:05 +02:00
popen_spec.rb
project_authorizations_spec.rb Use CTEs for nested groups and authorizations 2017-05-17 16:51:08 +02:00
project_search_results_spec.rb Backport FileFinder from EE 2017-05-12 13:21:30 +03:00
project_transfer_spec.rb
prometheus_client_spec.rb Custom queries for prometheus 2017-05-11 23:07:12 +02:00
redis_spec.rb Fix redis spec 2017-03-15 18:36:20 -06:00
reference_extractor_spec.rb Revert "Enable Style/BarePercentLiterals" 2017-02-23 09:32:42 -06:00
regex_spec.rb Reuse Gitlab::Regex.full_namespace_regex in the DynamicPathValidator 2017-05-02 09:13:41 +02:00
repo_path_spec.rb Enable the Style/TrailingCommaInLiteral cop 2017-05-10 18:25:45 +02:00
request_context_spec.rb Remove unecessary defaults for uniq ip block, cleanup refactoring leftovers 2017-03-06 15:45:43 +01:00
request_profiler_spec.rb Add specs for Gitlab::RequestProfiler 2017-05-01 17:32:45 +02:00
route_map_spec.rb Revert "Prefer leading style for Style/DotPosition" 2017-02-23 09:33:05 -06:00
routing_spec.rb
search_results_spec.rb Backport of multiple_assignees_feature [ci skip] 2017-05-04 17:11:53 +03:00
shell_spec.rb Merge branch 'mrchrisw-import-shell-timeout' 2017-05-05 14:40:52 +01:00
sidekiq_status_spec.rb Periodically mark projects that are stuck in importing as failed 2017-04-05 16:11:51 +02:00
sidekiq_throttler_spec.rb Auto-correct RSpec/DescribedClass violations 2017-05-01 11:13:33 -04:00
snippet_search_results_spec.rb
string_range_marker_spec.rb Add Gitlab::StringRegexMarker 2017-05-10 08:26:21 -05:00
string_regex_marker_spec.rb Add Gitlab::StringRegexMarker 2017-05-10 08:26:21 -05:00
upgrader_spec.rb Enable Style/MultilineArrayBraceLayout 2017-02-23 09:32:22 -06:00
url_blocker_spec.rb Merge branch 'ssrf' into 'security' 2017-03-20 18:53:04 -07:00
url_builder_spec.rb Revert "Prefer leading style for Style/DotPosition" 2017-02-23 09:33:05 -06:00
url_sanitizer_spec.rb Fix ProjectWiki#http_url_to_repo signature 2017-03-20 16:24:01 +01:00
usage_data_spec.rb Add hostname to usage ping 2017-05-11 09:20:12 +01:00
user_access_spec.rb Add confirm delete protected branch modal 2017-05-08 07:41:58 +00:00
user_activities_spec.rb Add a new Gitlab::UserActivities class to track user activities 2017-04-14 15:20:55 +02:00
utils_spec.rb Enable Rails/Delegate 2017-02-23 09:32:42 -06:00
version_info_spec.rb
visibility_level_spec.rb Make level_value accept string integers 2017-03-20 22:53:29 +01:00
workhorse_spec.rb Enable the Style/TrailingCommaInLiteral cop 2017-05-10 18:25:45 +02:00