diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-18 15:08:51 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-18 15:08:51 +0000 |
commit | 163a7046ac76eb4109184e82ce0af911633e6626 (patch) | |
tree | 9f22bb438db435d518e8f5520b309c6319ae0bd8 /db | |
parent | 0637ba1e6e9024f35b2cbf561d9002ec17350bb3 (diff) | |
download | gitlab-ce-163a7046ac76eb4109184e82ce0af911633e6626.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'db')
-rw-r--r-- | db/post_migrate/20200124110831_migrate_design_notes_mentions_to_db.rb | 61 | ||||
-rw-r--r-- | db/schema.rb | 1 |
2 files changed, 62 insertions, 0 deletions
diff --git a/db/post_migrate/20200124110831_migrate_design_notes_mentions_to_db.rb b/db/post_migrate/20200124110831_migrate_design_notes_mentions_to_db.rb new file mode 100644 index 00000000000..f1e4ee9807b --- /dev/null +++ b/db/post_migrate/20200124110831_migrate_design_notes_mentions_to_db.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +class MigrateDesignNotesMentionsToDb < ActiveRecord::Migration[5.2] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + DELAY = 2.minutes.to_i + BATCH_SIZE = 10000 + MIGRATION = 'UserMentions::CreateResourceUserMention' + + INDEX_NAME = 'design_mentions_temp_index' + INDEX_CONDITION = "note LIKE '%@%'::text AND notes.noteable_type = 'DesignManagement::Design'" + QUERY_CONDITIONS = "#{INDEX_CONDITION} AND design_user_mentions.design_id IS NULL" + JOIN = 'INNER JOIN design_management_designs ON design_management_designs.id = notes.noteable_id LEFT JOIN design_user_mentions ON notes.id = design_user_mentions.note_id' + + class DesignUserMention < ActiveRecord::Base + include EachBatch + + self.table_name = 'design_user_mentions' + end + + class Note < ActiveRecord::Base + include EachBatch + + self.table_name = 'notes' + end + + def up + return unless Gitlab.ee? + + # cleanup design user mentions with no actual mentions, + # re https://gitlab.com/gitlab-org/gitlab/-/merge_requests/24586#note_285982468 + DesignUserMention + .where(mentioned_users_ids: nil) + .where(mentioned_groups_ids: nil) + .where(mentioned_projects_ids: nil) + .each_batch(of: BATCH_SIZE) do |batch| + batch.delete_all + end + + # create temporary index for notes with mentions, may take well over 1h + add_concurrent_index(:notes, :id, where: INDEX_CONDITION, name: INDEX_NAME) + + Note + .joins(JOIN) + .where(QUERY_CONDITIONS) + .each_batch(of: BATCH_SIZE) do |batch, index| + range = batch.pluck(Arel.sql('MIN(notes.id)'), Arel.sql('MAX(notes.id)')).first + BackgroundMigrationWorker.perform_in(index * DELAY, MIGRATION, ['DesignManagement::Design', JOIN, QUERY_CONDITIONS, true, *range]) + end + end + + def down + # no-op + # temporary index is to be dropped in a different migration in an upcoming release: + # https://gitlab.com/gitlab-org/gitlab/issues/196842 + end +end diff --git a/db/schema.rb b/db/schema.rb index 435d994e201..fec0ba6e753 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -2808,6 +2808,7 @@ ActiveRecord::Schema.define(version: 2020_02_13_220211) do t.index ["commit_id"], name: "index_notes_on_commit_id" t.index ["created_at"], name: "index_notes_on_created_at" t.index ["discussion_id"], name: "index_notes_on_discussion_id" + t.index ["id"], name: "design_mentions_temp_index", where: "((note ~~ '%@%'::text) AND ((noteable_type)::text = 'DesignManagement::Design'::text))" t.index ["id"], name: "epic_mentions_temp_index", where: "((note ~~ '%@%'::text) AND ((noteable_type)::text = 'Epic'::text))" t.index ["line_code"], name: "index_notes_on_line_code" t.index ["note"], name: "index_notes_on_note_trigram", opclass: :gin_trgm_ops, using: :gin |