Files
Robert SpeicherandRémy Coutable 3129e135eb Merge branch 'drop_db_before_restore' into 'master'
Reload the schema before restoring a database backup

If a user tries to downgrade and restore after a failed upgrade,
the database may still contain newer tables. Reload the older
schema before restoring the database to avoid future upgrade
problems. Also, add a rake task to help users add migration
versions to the database so it's easier to recover from these
errors if they do occur. Fixes #13419

See merge request !2807
2016-03-23 12:25:06 +01:00

36 lines
1.2 KiB
Ruby

namespace :gitlab do
namespace :db do
desc 'GitLab | Manually insert schema migration version'
task :mark_migration_complete, [:version] => :environment do |_, args|
unless args[:version]
puts "Must specify a migration version as an argument".red
exit 1
end
version = args[:version].to_i
if version == 0
puts "Version '#{args[:version]}' must be a non-zero integer".red
exit 1
end
sql = "INSERT INTO schema_migrations (version) VALUES (#{version})"
begin
ActiveRecord::Base.connection.execute(sql)
puts "Successfully marked '#{version}' as complete".green
rescue ActiveRecord::RecordNotUnique
puts "Migration version '#{version}' is already marked complete".yellow
end
end
desc 'Drop all tables'
task :drop_tables => :environment do
connection = ActiveRecord::Base.connection
tables = connection.tables
tables.delete 'schema_migrations'
# Truncate schema_migrations to ensure migrations re-run
connection.execute('TRUNCATE schema_migrations')
tables.each { |t| connection.execute("DROP TABLE #{t}") }
end
end
end