gitlab-org--gitlab-foss/app/models
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
..
blob_viewer Implement auxiliary blob viewers 2017-05-10 20:07:04 -05:00
ci Merge branch 'retried-in-database-mysql' into 'master' 2017-05-10 22:07:05 +00:00
concerns Use CTEs for nested groups and authorizations 2017-05-17 16:51:08 +02:00
cycle_analytics
hooks Make the new repository_update_events configurable in System Hooks UI 2017-05-12 23:26:48 +01:00
issue
members Refactor add_users method for project and group 2017-04-21 18:38:10 +03:00
merge_request
network Fix ordering of commits in the network graph. 2017-04-27 13:09:54 +00:00
project_services Merge branch '27439-performance-deltas' into 'master' 2017-05-12 17:15:31 +00:00
protected_branch Enabled no-one as a merge access level in protected branches 2017-05-10 17:19:40 +01:00
protected_tag Renamed ProtectedTag push_access_levels to create_access_levels 2017-04-04 03:38:58 +01:00
.gitkeep
ability.rb use a magic default :global symbol instead of nil 2017-03-09 11:49:52 -08:00
abuse_report.rb Hard delete users' associated records deleted from AbuseReports 2017-04-16 08:36:33 -07:00
appearance.rb Add `has_many` associations for models that can have Upload records 2017-03-06 14:41:10 -05:00
application_setting.rb Allow disabling usage ping in `gitlab.yml` 2017-05-11 09:20:11 +01:00
audit_event.rb
award_emoji.rb Move a user's award emoji to the ghost user 2017-04-06 18:59:17 +05:30
blob.rb Implement auxiliary blob viewers 2017-05-10 20:07:04 -05:00
board.rb Change Done column to Closed in issue boards 2017-03-24 23:40:35 +11:00
broadcast_message.rb
chat_name.rb
chat_team.rb Namespace can have only one chat team 2017-03-07 15:20:22 +01:00
commit.rb Add support for deltas_only under Gitaly 2017-05-13 18:27:06 +02:00
commit_range.rb
commit_status.rb Merge branch 'retried-in-database-mysql' into 'master' 2017-05-10 22:07:05 +00:00
compare.rb
container_repository.rb Sanitize container repository path in model class 2017-04-13 11:54:02 +02:00
cycle_analytics.rb
dashboard_milestone.rb
deploy_key.rb
deploy_keys_project.rb
deployment.rb Keep presentation logic in one place and remove unecessary arguments. 2017-05-11 23:07:12 +02:00
diff_discussion.rb Enable the Style/TrailingCommaInArguments cop 2017-05-10 18:25:45 +02:00
diff_note.rb Allow commenting on older versions of the diff and comparisons between diff versions 2017-05-03 14:47:21 -05:00
directly_addressed_user.rb
discussion.rb Fix bug where commit comment would not show up in the right discussion on the MR page 2017-04-07 11:29:29 -05:00
discussion_note.rb Refactor resolvability checks based on type 2017-04-06 10:51:45 -05:00
email.rb
environment.rb Custom queries for prometheus 2017-05-11 23:07:12 +02:00
event.rb Update last_repository_updated_at when a push event is created 2017-05-04 15:36:04 -03:00
external_issue.rb Fix issues mentioned but not closed for JIRA 2017-03-06 14:18:49 +00:00
forked_project_link.rb
generic_commit_status.rb Revert "Enable Style/DotPosition" 2017-02-23 09:33:19 -06:00
global_label.rb
global_milestone.rb Backport of multiple_assignees_feature [ci skip] 2017-05-04 17:11:53 +03:00
group.rb Use CTEs for nested groups and authorizations 2017-05-17 16:51:08 +02:00
group_label.rb
group_milestone.rb
guest.rb use a magic default :global symbol instead of nil 2017-03-09 11:49:52 -08:00
identity.rb Implement search by extern_uid in Users API 2017-04-18 14:55:32 -04:00
individual_note_discussion.rb Ensure replying to an individual note by email creates a note with its own discussion ID 2017-04-24 12:24:39 -05:00
issue.rb Consistently use monospace font for commit SHAs and branch and tag names 2017-05-12 09:55:25 -05:00
issue_assignee.rb Move update_assignee_cache_counts to the service 2017-05-12 14:12:29 +03:00
issue_collection.rb Revert "Prefer leading style for Style/DotPosition" 2017-02-23 09:33:05 -06:00
key.rb Enable the Style/TrailingCommaInArguments cop 2017-05-10 18:25:45 +02:00
label.rb Add board_move slash command 2017-04-28 12:11:53 +00:00
label_link.rb
label_priority.rb
legacy_diff_discussion.rb Allow commenting on older versions of the diff and comparisons between diff versions 2017-05-03 14:47:21 -05:00
legacy_diff_note.rb Link to outdated diff in older MR version from outdated diff discussion 2017-04-08 14:37:46 -05:00
lfs_object.rb Revert "Enable Style/DotPosition" 2017-02-23 09:33:19 -06:00
lfs_objects_project.rb
list.rb Change Done column to Closed in issue boards 2017-03-24 23:40:35 +11:00
member.rb Collect all users by single query when using Member#add_users 2017-04-28 11:50:11 +03:00
merge_request.rb Merge branch 'fix-conflict-resolution-with-corrupt-repos' into 'master' 2017-05-12 20:37:30 +00:00
merge_request_diff.rb Show correct size when MR diff overflows 2017-04-25 16:54:10 +01:00
merge_requests_closing_issues.rb Revert "Prefer leading style for Style/DotPosition" 2017-02-23 09:33:05 -06:00
milestone.rb Backport of multiple_assignees_feature [ci skip] 2017-05-04 17:11:53 +03:00
namespace.rb Use CTEs for nested groups and authorizations 2017-05-17 16:51:08 +02:00
note.rb Add comment to notes aliases 2017-05-04 12:58:41 +11:00
notification_setting.rb Quiet pipeline emails 2017-04-03 13:59:48 +01:00
oauth_access_grant.rb Implement OpenID Connect identity provider 2017-03-07 14:54:35 +01:00
oauth_access_token.rb Implement OpenID Connect identity provider 2017-03-07 14:54:35 +01:00
out_of_context_discussion.rb Ensure replying to an individual note by email creates a note with its own discussion ID 2017-04-24 12:24:39 -05:00
pages_domain.rb Enable Rails/Validation 2017-02-23 09:31:57 -06:00
personal_access_token.rb Merge branch 'siemens/gitlab-ce-feature/openid-connect' 2017-03-07 16:16:08 +00:00
personal_snippet.rb
project.rb Remove rails dependent: :destroy statement 2017-05-11 14:52:47 +02:00
project_authorization.rb Use CTEs for nested groups and authorizations 2017-05-17 16:51:08 +02:00
project_feature.rb Enable Style/MutableConstant 2017-02-23 09:31:56 -06:00
project_group_link.rb Don't allow a project to be shared with an ancestor of the group it is in 2017-02-28 15:11:21 +02:00
project_import_data.rb
project_label.rb
project_snippet.rb Spam check and reCAPTCHA improvements 2017-02-21 13:32:49 -03:00
project_statistics.rb Enable Style/MutableConstant 2017-02-23 09:31:56 -06:00
project_team.rb Refactor add_users method for project and group 2017-04-21 18:38:10 +03:00
project_wiki.rb Update last_repository_updated_at when the wiki is updated 2017-05-04 15:44:11 -03:00
protectable_dropdown.rb Protected Tags backend review changes 2017-04-06 10:56:21 +01:00
protected_branch.rb Removed protected_tags_array 2017-04-03 19:47:08 +01:00
protected_ref_matcher.rb Protected tags changes from backend maintainer review 2017-04-07 01:17:38 +01:00
protected_tag.rb Renamed ProtectedTag push_access_levels to create_access_levels 2017-04-04 03:38:58 +01:00
readme_blob.rb Load tree readme asynchronously 2017-05-10 08:21:58 -05:00
redirect_route.rb Delete conflicting redirects 2017-05-05 12:11:58 -07:00
release.rb
repository.rb Fix displaying a repository without a readme 2017-05-11 15:38:30 -05:00
route.rb Change project list cache key to use route.cache_key instead of namespace 2017-05-12 18:27:59 +03:00
security_event.rb
sent_notification.rb Enable the Style/TrailingCommaInArguments cop 2017-05-10 18:25:45 +02:00
service.rb Merge branch '31138-improve-test-settings-for-services-in-empty-projects' into 'master' 2017-04-27 19:16:31 +00:00
snippet.rb Merge branch 'snippets-finder-visibility' into 'security' 2017-05-10 16:48:18 +02:00
snippet_blob.rb Refactor Blob support of external storage in preparation of job artifact blobs 2017-05-03 10:20:05 -05:00
spam_log.rb Use DeleteUserWorker for removing users via spam logs 2017-04-17 17:17:31 -07:00
subscription.rb
system_note_metadata.rb Add system note on description change of issue/merge request 2017-05-04 12:58:41 +11:00
timelog.rb
todo.rb Improve text on todo list when the todo action comes from yourself 2017-04-28 14:24:32 +02:00
tree.rb Load tree readme asynchronously 2017-05-10 08:21:58 -05:00
trending_project.rb
u2f_registration.rb
upload.rb Add Upload model and UploadChecksumWorker worker 2017-03-06 14:41:09 -05:00
user.rb Use CTEs for nested groups and authorizations 2017-05-17 16:51:08 +02:00
user_agent_detail.rb
users_star_project.rb
wiki_directory.rb
wiki_page.rb Fix Project Wiki update 2017-03-20 13:53:23 +00:00