summaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-04-01 15:07:45 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-04-01 15:07:45 +0000
commit1219a9dce91f4edbc135dfc08299b4122b4825a8 (patch)
treee7d12a55d75a2d56e60d9527bef3724e3578866d /db
parent1a0d6dbdc2ac3047f4953a359ef27ba6e26074ae (diff)
downloadgitlab-ce-1219a9dce91f4edbc135dfc08299b4122b4825a8.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'db')
-rw-r--r--db/migrate/20200317110602_add_migrating_user_highest_roles_table_index_to_users.rb24
-rw-r--r--db/post_migrate/20200311130802_schedule_populate_user_highest_roles_table.rb37
-rw-r--r--db/structure.sql4
3 files changed, 65 insertions, 0 deletions
diff --git a/db/migrate/20200317110602_add_migrating_user_highest_roles_table_index_to_users.rb b/db/migrate/20200317110602_add_migrating_user_highest_roles_table_index_to_users.rb
new file mode 100644
index 00000000000..e67dc323c39
--- /dev/null
+++ b/db/migrate/20200317110602_add_migrating_user_highest_roles_table_index_to_users.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+# See https://docs.gitlab.com/ee/development/migration_style_guide.html
+# for more information on how to write migrations for GitLab.
+
+class AddMigratingUserHighestRolesTableIndexToUsers < ActiveRecord::Migration[6.0]
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+ INDEX_NAME = 'index_for_migrating_user_highest_roles_table'
+
+ disable_ddl_transaction!
+
+ def up
+ add_concurrent_index :users,
+ :id,
+ where: "state = 'active' AND user_type IS NULL AND bot_type IS NULL AND ghost IS NOT TRUE",
+ name: INDEX_NAME
+ end
+
+ def down
+ remove_concurrent_index :users, :id, name: INDEX_NAME
+ end
+end
diff --git a/db/post_migrate/20200311130802_schedule_populate_user_highest_roles_table.rb b/db/post_migrate/20200311130802_schedule_populate_user_highest_roles_table.rb
new file mode 100644
index 00000000000..36f0d42a855
--- /dev/null
+++ b/db/post_migrate/20200311130802_schedule_populate_user_highest_roles_table.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+class SchedulePopulateUserHighestRolesTable < ActiveRecord::Migration[6.0]
+ include Gitlab::Database::MigrationHelpers
+
+ BATCH_SIZE = 10_000
+ DELAY = 5.minutes.to_i
+ DOWNTIME = false
+ MIGRATION = 'PopulateUserHighestRolesTable'
+
+ disable_ddl_transaction!
+
+ class User < ActiveRecord::Base
+ include EachBatch
+
+ scope :active, -> {
+ where(state: 'active', user_type: nil, bot_type: nil)
+ .where('ghost IS NOT TRUE')
+ }
+ end
+
+ def up
+ # We currently have ~5_300_000 users with the state active on GitLab.com.
+ # This means it'll schedule ~530 jobs (10k Users each) with a 5 minutes gap,
+ # so this should take ~44 hours for all background migrations to complete.
+ User.active.each_batch(of: BATCH_SIZE) do |batch, index|
+ range = batch.pluck(Arel.sql('MIN(id)'), Arel.sql('MAX(id)')).first
+ delay = index * DELAY
+
+ migrate_in(delay.seconds, MIGRATION, [*range])
+ end
+ end
+
+ def down
+ # nothing
+ end
+end
diff --git a/db/structure.sql b/db/structure.sql
index aa43d49b6dc..178cf4c63da 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -9066,6 +9066,8 @@ CREATE UNIQUE INDEX index_feature_gates_on_feature_key_and_key_and_value ON publ
CREATE UNIQUE INDEX index_features_on_key ON public.features USING btree (key);
+CREATE INDEX index_for_migrating_user_highest_roles_table ON public.users USING btree (id) WHERE (((state)::text = 'active'::text) AND (user_type IS NULL) AND (bot_type IS NULL) AND (ghost IS NOT TRUE));
+
CREATE INDEX index_for_resource_group ON public.ci_builds USING btree (resource_group_id, id) WHERE (resource_group_id IS NOT NULL);
CREATE INDEX index_for_status_per_branch_per_project ON public.merge_trains USING btree (target_project_id, target_branch, status);
@@ -12865,6 +12867,7 @@ COPY "schema_migrations" (version) FROM STDIN;
20200311084025
20200311093210
20200311094020
+20200311130802
20200311141053
20200311141943
20200311154110
@@ -12880,6 +12883,7 @@ COPY "schema_migrations" (version) FROM STDIN;
20200316111759
20200316162648
20200316173312
+20200317110602
20200317142110
20200318140400
20200318152134