2016-03-08 22:52:48 +00:00
|
|
|
namespace :gitlab do
|
|
|
|
namespace :db do
|
|
|
|
desc 'GitLab | Manually insert schema migration version'
|
|
|
|
task :mark_migration_complete, [:version] => :environment do |_, args|
|
|
|
|
unless args[:version]
|
2016-06-01 22:37:15 +00:00
|
|
|
puts "Must specify a migration version as an argument".color(:red)
|
2016-03-08 22:52:48 +00:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
version = args[:version].to_i
|
|
|
|
if version == 0
|
2016-06-01 22:37:15 +00:00
|
|
|
puts "Version '#{args[:version]}' must be a non-zero integer".color(:red)
|
2016-03-08 22:52:48 +00:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
sql = "INSERT INTO schema_migrations (version) VALUES (#{version})"
|
|
|
|
begin
|
|
|
|
ActiveRecord::Base.connection.execute(sql)
|
2016-06-01 22:37:15 +00:00
|
|
|
puts "Successfully marked '#{version}' as complete".color(:green)
|
2016-03-08 22:52:48 +00:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
2016-06-01 22:37:15 +00:00
|
|
|
puts "Migration version '#{version}' is already marked complete".color(:yellow)
|
2016-03-08 22:52:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Drop all tables'
|
2017-02-22 18:18:40 +00:00
|
|
|
task drop_tables: :environment do
|
2016-03-08 22:52:48 +00:00
|
|
|
connection = ActiveRecord::Base.connection
|
2016-07-25 15:01:02 +00:00
|
|
|
|
2019-06-13 13:12:28 +00:00
|
|
|
# In PostgreSQLAdapter, data_sources returns both views and tables, so use
|
|
|
|
# #tables instead
|
|
|
|
tables = connection.tables
|
2019-04-02 00:19:22 +00:00
|
|
|
|
2016-09-21 21:54:09 +00:00
|
|
|
# Removes the entry from the array
|
2016-03-08 22:52:48 +00:00
|
|
|
tables.delete 'schema_migrations'
|
|
|
|
# Truncate schema_migrations to ensure migrations re-run
|
2016-09-21 21:54:09 +00:00
|
|
|
connection.execute('TRUNCATE schema_migrations') if connection.data_source_exists? 'schema_migrations'
|
2016-05-09 17:20:18 +00:00
|
|
|
|
2016-05-03 14:29:15 +00:00
|
|
|
# Drop tables with cascade to avoid dependent table errors
|
|
|
|
# PG: http://www.postgresql.org/docs/current/static/ddl-depend.html
|
2016-05-09 17:20:18 +00:00
|
|
|
# Add `IF EXISTS` because cascade could have already deleted a table.
|
2016-06-09 21:51:57 +00:00
|
|
|
tables.each { |t| connection.execute("DROP TABLE IF EXISTS #{connection.quote_table_name(t)} CASCADE") }
|
2016-03-08 22:52:48 +00:00
|
|
|
end
|
2016-05-05 23:06:34 +00:00
|
|
|
|
|
|
|
desc 'Configures the database by running migrate, or by loading the schema and seeding if needed'
|
2016-05-06 17:08:05 +00:00
|
|
|
task configure: :environment do
|
2018-06-27 20:34:45 +00:00
|
|
|
# Check if we have existing db tables
|
|
|
|
# The schema_migrations table will still exist if drop_tables was called
|
|
|
|
if ActiveRecord::Base.connection.tables.count > 1
|
2016-05-05 23:06:34 +00:00
|
|
|
Rake::Task['db:migrate'].invoke
|
|
|
|
else
|
2018-09-12 01:41:14 +00:00
|
|
|
# Add post-migrate paths to ensure we mark all migrations as up
|
|
|
|
Gitlab::Database.add_post_migrate_path_to_rails(force: true)
|
2016-05-05 23:06:34 +00:00
|
|
|
Rake::Task['db:schema:load'].invoke
|
|
|
|
Rake::Task['db:seed_fu'].invoke
|
|
|
|
end
|
|
|
|
end
|
2016-06-24 16:29:23 +00:00
|
|
|
|
|
|
|
desc 'Checks if migrations require downtime or not'
|
|
|
|
task :downtime_check, [:ref] => :environment do |_, args|
|
|
|
|
abort 'You must specify a Git reference to compare with' unless args[:ref]
|
|
|
|
|
|
|
|
require 'shellwords'
|
|
|
|
|
|
|
|
ref = Shellwords.escape(args[:ref])
|
|
|
|
|
2017-03-07 21:45:03 +00:00
|
|
|
migrations = `git diff #{ref}.. --diff-filter=A --name-only -- db/migrate`.lines
|
2017-02-22 18:18:40 +00:00
|
|
|
.map { |file| Rails.root.join(file.strip).to_s }
|
|
|
|
.select { |file| File.file?(file) }
|
2017-05-02 09:17:48 +00:00
|
|
|
.select { |file| /\A[0-9]+.*\.rb\z/ =~ File.basename(file) }
|
2016-06-24 16:29:23 +00:00
|
|
|
|
|
|
|
Gitlab::DowntimeCheck.new.check_and_print(migrations)
|
|
|
|
end
|
2016-03-08 22:52:48 +00:00
|
|
|
end
|
|
|
|
end
|