debian-mirror-gitlab/lib/tasks/gitlab/db.rake

83 lines
3.1 KiB
Ruby
Raw Normal View History

2016-06-02 11:05:42 +05:30
namespace :gitlab do
namespace :db do
2020-03-09 13:42:32 +05:30
desc 'GitLab | DB | Manually insert schema migration version'
2016-06-02 11:05:42 +05:30
task :mark_migration_complete, [:version] => :environment do |_, args|
unless args[:version]
puts "Must specify a migration version as an argument".color(:red)
2016-06-02 11:05:42 +05:30
exit 1
end
version = args[:version].to_i
if version == 0
puts "Version '#{args[:version]}' must be a non-zero integer".color(:red)
2016-06-02 11:05:42 +05:30
exit 1
end
sql = "INSERT INTO schema_migrations (version) VALUES (#{version})"
begin
ActiveRecord::Base.connection.execute(sql)
puts "Successfully marked '#{version}' as complete".color(:green)
2016-06-02 11:05:42 +05:30
rescue ActiveRecord::RecordNotUnique
puts "Migration version '#{version}' is already marked complete".color(:yellow)
2016-06-02 11:05:42 +05:30
end
end
2020-03-09 13:42:32 +05:30
desc 'GitLab | DB | Drop all tables'
2017-08-17 22:00:37 +05:30
task drop_tables: :environment do
2016-06-02 11:05:42 +05:30
connection = ActiveRecord::Base.connection
2016-08-24 12:49:21 +05:30
2019-10-12 21:52:04 +05:30
# In PostgreSQLAdapter, data_sources returns both views and tables, so use
# #tables instead
tables = connection.tables
2019-07-07 11:18:12 +05:30
2019-03-02 22:35:43 +05:30
# Removes the entry from the array
2016-06-02 11:05:42 +05:30
tables.delete 'schema_migrations'
# Truncate schema_migrations to ensure migrations re-run
2019-10-12 21:52:04 +05:30
connection.execute('TRUNCATE schema_migrations') if connection.table_exists? 'schema_migrations'
2016-06-02 11:05:42 +05:30
# Drop tables with cascade to avoid dependent table errors
# PG: http://www.postgresql.org/docs/current/static/ddl-depend.html
# Add `IF EXISTS` because cascade could have already deleted a table.
tables.each { |t| connection.execute("DROP TABLE IF EXISTS #{connection.quote_table_name(t)} CASCADE") }
end
2020-03-09 13:42:32 +05:30
desc 'GitLab | DB | Configures the database by running migrate, or by loading the schema and seeding if needed'
task configure: :environment do
2018-11-08 19:23:39 +05:30
# 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
Rake::Task['db:migrate'].invoke
else
2018-12-05 23:21:45 +05:30
# Add post-migrate paths to ensure we mark all migrations as up
Gitlab::Database.add_post_migrate_path_to_rails(force: true)
Rake::Task['db:schema:load'].invoke
Rake::Task['db:seed_fu'].invoke
end
2016-06-02 11:05:42 +05:30
end
2016-08-24 12:49:21 +05:30
2020-03-09 13:42:32 +05:30
desc 'GitLab | DB | Checks if migrations require downtime or not'
2016-08-24 12:49:21 +05:30
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-08-17 22:00:37 +05:30
migrations = `git diff #{ref}.. --diff-filter=A --name-only -- db/migrate`.lines
.map { |file| Rails.root.join(file.strip).to_s }
.select { |file| File.file?(file) }
.select { |file| /\A[0-9]+.*\.rb\z/ =~ File.basename(file) }
2016-08-24 12:49:21 +05:30
Gitlab::DowntimeCheck.new.check_and_print(migrations)
end
2019-10-12 21:52:04 +05:30
2020-03-09 13:42:32 +05:30
desc 'GitLab | DB | Sets up EE specific database functionality'
2019-10-12 21:52:04 +05:30
if Gitlab.ee?
task setup_ee: %w[geo:db:drop geo:db:create geo:db:schema:load geo:db:migrate]
else
task :setup_ee
end
2016-06-02 11:05:42 +05:30
end
end