diff options
Diffstat (limited to 'db')
-rw-r--r-- | db/post_migrate/20191115115043_migrate_epic_mentions_to_db.rb | 36 | ||||
-rw-r--r-- | db/post_migrate/20191115115522_migrate_epic_notes_mentions_to_db.rb | 45 | ||||
-rw-r--r-- | db/schema.rb | 1 |
3 files changed, 82 insertions, 0 deletions
diff --git a/db/post_migrate/20191115115043_migrate_epic_mentions_to_db.rb b/db/post_migrate/20191115115043_migrate_epic_mentions_to_db.rb new file mode 100644 index 00000000000..97f2e568a7e --- /dev/null +++ b/db/post_migrate/20191115115043_migrate_epic_mentions_to_db.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +class MigrateEpicMentionsToDb < ActiveRecord::Migration[5.2] + DOWNTIME = false + + disable_ddl_transaction! + + DELAY = 2.minutes.to_i + BATCH_SIZE = 10000 + MIGRATION = 'UserMentions::CreateResourceUserMention' + + JOIN = "LEFT JOIN epic_user_mentions on epics.id = epic_user_mentions.epic_id" + QUERY_CONDITIONS = "(description like '%@%' OR title like '%@%') AND epic_user_mentions.epic_id is null" + + class Epic < ActiveRecord::Base + include EachBatch + + self.table_name = 'epics' + end + + def up + return unless Gitlab.ee? + + Epic + .joins(JOIN) + .where(QUERY_CONDITIONS) + .each_batch(of: BATCH_SIZE) do |batch, index| + range = batch.pluck(Arel.sql('MIN(epics.id)'), Arel.sql('MAX(epics.id)')).first + BackgroundMigrationWorker.perform_in(index * DELAY, MIGRATION, ['Epic', JOIN, QUERY_CONDITIONS, false, *range]) + end + end + + def down + # no-op + end +end diff --git a/db/post_migrate/20191115115522_migrate_epic_notes_mentions_to_db.rb b/db/post_migrate/20191115115522_migrate_epic_notes_mentions_to_db.rb new file mode 100644 index 00000000000..e0b3c36b57d --- /dev/null +++ b/db/post_migrate/20191115115522_migrate_epic_notes_mentions_to_db.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +class MigrateEpicNotesMentionsToDb < 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 = 'epic_mentions_temp_index' + INDEX_CONDITION = "note LIKE '%@%'::text AND notes.noteable_type = 'Epic'" + QUERY_CONDITIONS = "#{INDEX_CONDITION} AND epic_user_mentions.epic_id IS NULL" + JOIN = 'LEFT JOIN epic_user_mentions ON notes.id = epic_user_mentions.note_id' + + class Note < ActiveRecord::Base + include EachBatch + + self.table_name = 'notes' + end + + def up + return unless Gitlab.ee? + + # 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, ['Epic', 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 3248768aa0b..f48ead215bc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -2778,6 +2778,7 @@ ActiveRecord::Schema.define(version: 2020_01_27_090233) 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: "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 t.index ["noteable_id", "noteable_type"], name: "index_notes_on_noteable_id_and_noteable_type" |