diff options
Diffstat (limited to 'lib/tasks/gitlab/db/migration_fix_15_11.rake')
-rw-r--r-- | lib/tasks/gitlab/db/migration_fix_15_11.rake | 30 |
1 files changed, 30 insertions, 0 deletions
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']) |