From 7f45515dd4f820f6902088d0ef807f6177db2661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20W=C3=A4lter?= Date: Mon, 30 Aug 2021 12:48:15 +0200 Subject: [PATCH] Reschedule 'ExtractProjectTopicsIntoSeparateTable' post migration Changelog: fixed --- ...tract_project_topics_into_separate_table.rb | 18 ++++++++++++++++++ db/schema_migrations/20210830104800 | 1 + ...tract_project_topics_into_separate_table.rb | 17 +++++++++++++---- ..._project_topics_into_separate_table_spec.rb | 4 +++- 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 db/post_migrate/20210830104800_reschedule_extract_project_topics_into_separate_table.rb create mode 100644 db/schema_migrations/20210830104800 diff --git a/db/post_migrate/20210830104800_reschedule_extract_project_topics_into_separate_table.rb b/db/post_migrate/20210830104800_reschedule_extract_project_topics_into_separate_table.rb new file mode 100644 index 00000000000000..d6b2db7790f1e5 --- /dev/null +++ b/db/post_migrate/20210830104800_reschedule_extract_project_topics_into_separate_table.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +class RescheduleExtractProjectTopicsIntoSeparateTable < ActiveRecord::Migration[6.0] + include Gitlab::Database::MigrationHelpers + + MIGRATION = 'ExtractProjectTopicsIntoSeparateTable' + DELAY_INTERVAL = 4.minutes + + disable_ddl_transaction! + + def up + requeue_background_migration_jobs_by_range_at_intervals(MIGRATION, DELAY_INTERVAL) + end + + def down + # no-op + end +end diff --git a/db/schema_migrations/20210830104800 b/db/schema_migrations/20210830104800 new file mode 100644 index 00000000000000..ca1aa9180e2246 --- /dev/null +++ b/db/schema_migrations/20210830104800 @@ -0,0 +1 @@ +84a68304f95ae04b85625c214b28a251014582fb142390ff3df8ea6d6f0947e1 \ No newline at end of file diff --git a/lib/gitlab/background_migration/extract_project_topics_into_separate_table.rb b/lib/gitlab/background_migration/extract_project_topics_into_separate_table.rb index 15ce038321cefd..c5beb5a686537e 100644 --- a/lib/gitlab/background_migration/extract_project_topics_into_separate_table.rb +++ b/lib/gitlab/background_migration/extract_project_topics_into_separate_table.rb @@ -26,12 +26,21 @@ class ProjectTopic < ActiveRecord::Base belongs_to :topic end + # Temporary AR table for projects + class Project < ActiveRecord::Base + self.table_name = 'projects' + end + def perform(start_id, stop_id) Tagging.includes(:tag).where(taggable_type: 'Project', id: start_id..stop_id).each do |tagging| - topic = Topic.find_or_create_by(name: tagging.tag.name) - project_topic = ProjectTopic.find_or_create_by(project_id: tagging.taggable_id, topic: topic) - - tagging.delete if project_topic.persisted? + if Project.exists?(id: tagging.taggable_id) + topic = Topic.find_or_create_by(name: tagging.tag.name) + project_topic = ProjectTopic.find_or_create_by(project_id: tagging.taggable_id, topic: topic) + + tagging.delete if project_topic.persisted? + else + tagging.delete + end end mark_job_as_succeeded(start_id, stop_id) diff --git a/spec/lib/gitlab/background_migration/extract_project_topics_into_separate_table_spec.rb b/spec/lib/gitlab/background_migration/extract_project_topics_into_separate_table_spec.rb index d8f8fda3e579ec..05f40ee2501d81 100644 --- a/spec/lib/gitlab/background_migration/extract_project_topics_into_separate_table_spec.rb +++ b/spec/lib/gitlab/background_migration/extract_project_topics_into_separate_table_spec.rb @@ -21,14 +21,16 @@ tagging_2 = taggings.create!(taggable_type: 'Project', taggable_id: project.id, context: 'topics', tag_id: tag_2.id) other_tagging = taggings.create!(taggable_type: 'Other', taggable_id: project.id, context: 'topics', tag_id: tag_1.id) tagging_3 = taggings.create!(taggable_type: 'Project', taggable_id: project.id, context: 'topics', tag_id: tag_3.id) + tagging_4 = taggings.create!(taggable_type: 'Project', taggable_id: -1, context: 'topics', tag_id: tag_1.id) - subject.perform(tagging_1.id, tagging_3.id) + subject.perform(tagging_1.id, tagging_4.id) # Tagging records expect { tagging_1.reload }.to raise_error(ActiveRecord::RecordNotFound) expect { tagging_2.reload }.to raise_error(ActiveRecord::RecordNotFound) expect { other_tagging.reload }.not_to raise_error(ActiveRecord::RecordNotFound) expect { tagging_3.reload }.to raise_error(ActiveRecord::RecordNotFound) + expect { tagging_4.reload }.to raise_error(ActiveRecord::RecordNotFound) # Topic records topic_1 = topics.find_by(name: 'Topic1') -- GitLab