diff options
-rw-r--r-- | db/15_11_migration_fixes.txt | 299 | ||||
-rw-r--r-- | lib/tasks/gitlab/db/migration_fix_15_11.rake | 30 | ||||
-rw-r--r-- | spec/tasks/gitlab/db/migration_fix_15_11_rake_spec.rb | 66 |
3 files changed, 395 insertions, 0 deletions
diff --git a/db/15_11_migration_fixes.txt b/db/15_11_migration_fixes.txt new file mode 100644 index 00000000000..7fa291f7b29 --- /dev/null +++ b/db/15_11_migration_fixes.txt @@ -0,0 +1,299 @@ +20211202041233 +20211202094944 +20211202135508 +20211202145237 +20211203091642 +20211203160952 +20211203161149 +20211203161840 +20211203161942 +20211204010826 +20211206073851 +20211206074547 +20211206161271 +20211207081708 +20211207090503 +20211207125331 +20211207135331 +20211207154413 +20211207154414 +20211207165508 +20211207173510 +20211207173511 +20211208111425 +20211208122200 +20211208122201 +20211208171402 +20211209093636 +20211209093828 +20211209093923 +20211209094222 +20211209103048 +20211209203820 +20211209203821 +20211209230042 +20211210025754 +20211210031721 +20211210140000 +20211210140629 +20211210173137 +20211213064821 +20211213102111 +20211213130324 +20211213142344 +20211213154259 +20211213154704 +20211214012507 +20211214110307 +20211215090620 +20211215182006 +20211216133107 +20211216134134 +20211216135651 +20211216220939 +20211217050753 +20211217120000 +20211217145923 +20211217174331 +20211220064757 +20211220120402 +20211220123956 +20211220174504 +20211223125921 +20211224112937 +20211224114539 +20211229023654 +20211230112517 +20211230113031 +20220104060049 +20220104174445 +20220105020514 +20220105082217 +20220105121325 +20220105152547 +20220105153149 +20220106111958 +20220106112043 +20220106112085 +20220106141756 +20220106163326 +20220106185033 +20220106230629 +20220106230712 +20220106231518 +20220106233459 +20220106235626 +20220107064845 +20220107091629 +20220107165036 +20220109133006 +20220109134455 +20220110170953 +20220110171049 +20220110224913 +20220110231420 +20220110233155 +20220111002756 +20220111023852 +20220111093534 +20220111095006 +20220111095007 +20220111101421 +20220111102314 +20220111154950 +20220111154951 +20220111200254 +20220111221516 +20220112015940 +20220112090556 +20220112115413 +20220112205111 +20220112230642 +20220112232037 +20220112232605 +20220112232723 +20220113013319 +20220113014438 +20220113015830 +20220113035519 +20220113040447 +20220113111440 +20220113125401 +20220113135449 +20220113135924 +20220113164801 +20220113164901 +20220114105525 +20220114131950 +20220116175851 +20220117034056 +20220117082611 +20220117225936 +20220118015633 +20220118020026 +20220118141950 +20220118155846 +20220118155847 +20220118155848 +20220118204039 +20220119094023 +20220119094503 +20220119141407 +20220119141736 +20220119143130 +20220119144253 +20220119144458 +20220119151221 +20220119153706 +20220119154442 +20220119170426 +20220119193130 +20220119201340 +20220119203119 +20220119220620 +20220120033115 +20220120085655 +20220120094340 +20220120123700 +20220120123800 +20220120160625 +20220120211831 +20220120211832 +20220121214752 +20220121214753 +20220121221651 +20220124130028 +20220124145019 +20220124151456 +20220124151949 +20220124152824 +20220124153233 +20220124153234 +20220124180704 +20220124180705 +20220124184338 +20220124200927 +20220124204046 +20220124214131 +20220124215857 +20220124221521 +20220125083520 +20220125084127 +20220125084348 +20220125122228 +20220125122640 +20220125122725 +20220125230538 +20220126191624 +20220126201752 +20220126202654 +20220126203421 +20220126210021 +20220126210022 +20220126210657 +20220127112243 +20220127112412 +20220127132200 +20220127132201 +20220128093756 +20220128103042 +20220128155251 +20220128155814 +20220128194722 +20220131000000 +20220131000001 +20220131135725 +20220131192643 +20220201034731 +20220201141705 +20220201173212 +20220201193033 +20220201205300 +20220202034409 +20220202105733 +20220202115350 +20220203074916 +20220203091304 +20220203123333 +20220203133652 +20220203134942 +20220204053655 +20220204093120 +20220204095121 +20220204110725 +20220204154220 +20220204193000 +20220204194347 +20220207080758 +20220207083129 +20220208080921 +20220208115439 +20220208170445 +20220208171826 +20220209111007 +20220211090920 +20220211125954 +20220211214605 +20220212120735 +20220213100000 +20220213103859 +20220213104531 +20220215164709 +20220215190020 +20220216110023 +20220216201949 +20220217100008 +20220217113058 +20220217135229 +20220221102333 +20220221214928 +20220222072536 +20220222191845 +20220222192524 +20220222192525 +20220223112304 +20220223124428 +20220224000000 +20220224204415 +20220225133705 +20220301002101 +20220301003502 +20220301091503 +20220301093434 +20220301175104 +20220301175426 +20220302110724 +20220302114046 +20220302203410 +20220303190555 +20220303191047 +20220304052335 +20220304061631 +20220304062107 +20220304152729 +20220304165107 +20220304201847 +20220305223212 +20220307192534 +20220307192610 +20220307192645 +20220307192725 +20220307203458 +20220307203459 +20220308000205 +20220308115219 +20220308115502 +20220309084838 +20220309084954 +20220309100648 +20220309154855 +20220310011530 +20220310011613 +20220310095341 +20220310101118 +20220310134207 +20220310141349 +20220311010352 +20220314094841 +20220314154235 +20220314162342
\ No newline at end of file diff --git a/lib/tasks/gitlab/db/migration_fix_15_11.rake b/lib/tasks/gitlab/db/migration_fix_15_11.rake new file mode 100644 index 00000000000..fbfee856abb --- /dev/null +++ b/lib/tasks/gitlab/db/migration_fix_15_11.rake @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +desc 'db | migration_fix_15_11' +task migration_fix_15_11: [:environment] do + next if Gitlab.com? + + only_on = %i[main ci].select { |db| Gitlab::Database.has_database?(db) } + Gitlab::Database::EachDatabase.each_database_connection(only: only_on) do |conn, database| + begin + first_migration = conn.execute('SELECT * FROM schema_migrations ORDER BY version ASC LIMIT 1') + rescue ActiveRecord::StatementInvalid + # Uninitialized DB, skip + next + end + next if first_migration.none? # No migrations have been run yet + # If we are affected, the first migration in the schema_migrations table + # will be 20220314184009 + next unless first_migration.first['version'] == '20220314184009' + + puts "Running 15.11 migration fix for #{database}" + fixes = File.readlines(Rails.root.join('db/15_11_migration_fixes.txt')).map(&:chomp) + conn.transaction do + fixes.each do |version| + conn.execute("INSERT INTO schema_migrations (version) VALUES ('#{version}')") + end + end + end +end + +Rake::Task['db:migrate'].enhance(['migration_fix_15_11']) diff --git a/spec/tasks/gitlab/db/migration_fix_15_11_rake_spec.rb b/spec/tasks/gitlab/db/migration_fix_15_11_rake_spec.rb new file mode 100644 index 00000000000..3ff07698ad4 --- /dev/null +++ b/spec/tasks/gitlab/db/migration_fix_15_11_rake_spec.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require 'rake_helper' + +RSpec.describe 'migration_fix_15_11', :reestablished_active_record_base, feature_category: :database do + let(:db) { ApplicationRecord.connection } + let(:target_init_schema) { '20220314184009' } + let(:earlier_init_schema) { '20210101010101' } + + before :all do + Rake.application.rake_require 'active_record/railties/databases' + Rake.application.rake_require 'tasks/gitlab/db/migration_fix_15_11' + + Rake::Task.define_task :environment + end + + describe 'migration_fix_15_11' do + context 'when fix is needed' do + it 'patches init_schema' do + db.execute('DELETE FROM schema_migrations') + db.execute("INSERT INTO schema_migrations (version) VALUES ('#{target_init_schema}')") + run_rake_task(:migration_fix_15_11) + result = db.execute('SELECT * FROM schema_migrations') + expect(result.count).to eq(300) + end + end + + context 'when fix is not needed because no migrations have been run' do + it 'does nothing' do + db.execute('DELETE FROM schema_migrations') + run_rake_task(:migration_fix_15_11) + result = db.execute('SELECT * FROM schema_migrations') + expect(result.count).to eq(0) + end + end + + context 'when fix is not needed because DB has not been initialized' do + it 'does nothing' do + db.execute('DROP TABLE schema_migrations') + expect { run_rake_task(:migration_fix_15_11) }.not_to raise_error + end + end + + context 'when fix is not needed because there is an earlier init_schema' do + it 'does nothing' do + db.execute('DELETE FROM schema_migrations') + db.execute("INSERT INTO schema_migrations (version) VALUES ('#{earlier_init_schema}')") + run_rake_task(:migration_fix_15_11) + result = db.execute('SELECT * FROM schema_migrations') + expect(result.pluck('version')).to match_array [earlier_init_schema] + end + end + + context 'when fix is not needed because the fix has been run already' do + it 'does not affect the schema_migrations table' do + db.execute('DELETE FROM schema_migrations') + db.execute("INSERT INTO schema_migrations (version) VALUES ('#{target_init_schema}')") + run_rake_task(:migration_fix_15_11) + fixed_table = db.execute('SELECT version FROM schema_migrations').pluck('version') + run_rake_task(:migration_fix_15_11) + test_fixed_table = db.execute('SELECT version FROM schema_migrations').pluck('version') + expect(fixed_table).to match_array test_fixed_table + end + end + end +end |