2016-03-08 17:52:48 -05:00
|
|
|
namespace :gitlab do
|
|
|
|
namespace :db do
|
2020-01-23 01:08:32 -05:00
|
|
|
desc 'GitLab | DB | Manually insert schema migration version'
|
2016-03-08 17:52:48 -05:00
|
|
|
task :mark_migration_complete, [:version] => :environment do |_, args|
|
|
|
|
unless args[:version]
|
2016-06-01 18:37:15 -04:00
|
|
|
puts "Must specify a migration version as an argument".color(:red)
|
2016-03-08 17:52:48 -05:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
version = args[:version].to_i
|
|
|
|
if version == 0
|
2016-06-01 18:37:15 -04:00
|
|
|
puts "Version '#{args[:version]}' must be a non-zero integer".color(:red)
|
2016-03-08 17:52:48 -05:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
sql = "INSERT INTO schema_migrations (version) VALUES (#{version})"
|
|
|
|
begin
|
|
|
|
ActiveRecord::Base.connection.execute(sql)
|
2016-06-01 18:37:15 -04:00
|
|
|
puts "Successfully marked '#{version}' as complete".color(:green)
|
2016-03-08 17:52:48 -05:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
2016-06-01 18:37:15 -04:00
|
|
|
puts "Migration version '#{version}' is already marked complete".color(:yellow)
|
2016-03-08 17:52:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-23 01:08:32 -05:00
|
|
|
desc 'GitLab | DB | Drop all tables'
|
2017-02-22 13:18:40 -05:00
|
|
|
task drop_tables: :environment do
|
2016-03-08 17:52:48 -05:00
|
|
|
connection = ActiveRecord::Base.connection
|
2016-07-25 11:01:02 -04:00
|
|
|
|
2019-06-13 09:12:28 -04:00
|
|
|
# In PostgreSQLAdapter, data_sources returns both views and tables, so use
|
|
|
|
# #tables instead
|
|
|
|
tables = connection.tables
|
2019-04-01 20:19:22 -04:00
|
|
|
|
2016-09-21 17:54:09 -04:00
|
|
|
# Removes the entry from the array
|
2016-03-08 17:52:48 -05:00
|
|
|
tables.delete 'schema_migrations'
|
|
|
|
# Truncate schema_migrations to ensure migrations re-run
|
2019-07-16 23:51:22 -04:00
|
|
|
connection.execute('TRUNCATE schema_migrations') if connection.table_exists? 'schema_migrations'
|
2016-05-09 13:20:18 -04:00
|
|
|
|
2020-10-02 05:08:33 -04:00
|
|
|
# Drop any views
|
|
|
|
connection.views.each do |view|
|
|
|
|
connection.execute("DROP VIEW IF EXISTS #{connection.quote_table_name(view)} CASCADE")
|
|
|
|
end
|
|
|
|
|
2016-05-03 10:29:15 -04:00
|
|
|
# Drop tables with cascade to avoid dependent table errors
|
|
|
|
# PG: http://www.postgresql.org/docs/current/static/ddl-depend.html
|
2016-05-09 13:20:18 -04:00
|
|
|
# Add `IF EXISTS` because cascade could have already deleted a table.
|
2016-06-09 17:51:57 -04:00
|
|
|
tables.each { |t| connection.execute("DROP TABLE IF EXISTS #{connection.quote_table_name(t)} CASCADE") }
|
2020-06-24 20:09:26 -04:00
|
|
|
|
|
|
|
# Drop all extra schema objects GitLab owns
|
|
|
|
Gitlab::Database::EXTRA_SCHEMAS.each do |schema|
|
|
|
|
connection.execute("DROP SCHEMA IF EXISTS #{connection.quote_table_name(schema)}")
|
|
|
|
end
|
2016-03-08 17:52:48 -05:00
|
|
|
end
|
2016-05-05 19:06:34 -04:00
|
|
|
|
2020-01-23 01:08:32 -05:00
|
|
|
desc 'GitLab | DB | Configures the database by running migrate, or by loading the schema and seeding if needed'
|
2016-05-06 13:08:05 -04:00
|
|
|
task configure: :environment do
|
2018-06-27 16:34:45 -04: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 19:06:34 -04:00
|
|
|
Rake::Task['db:migrate'].invoke
|
|
|
|
else
|
2018-09-11 21:41:14 -04:00
|
|
|
# Add post-migrate paths to ensure we mark all migrations as up
|
|
|
|
Gitlab::Database.add_post_migrate_path_to_rails(force: true)
|
2020-03-22 11:09:49 -04:00
|
|
|
Rake::Task['db:structure:load'].invoke
|
2016-05-05 19:06:34 -04:00
|
|
|
Rake::Task['db:seed_fu'].invoke
|
|
|
|
end
|
|
|
|
end
|
2016-06-24 12:29:23 -04:00
|
|
|
|
2020-10-13 11:08:53 -04:00
|
|
|
desc 'GitLab | DB | Run database migrations and print `unattended_migrations_completed` if action taken'
|
|
|
|
task unattended: :environment do
|
|
|
|
no_database = !ActiveRecord::Base.connection.schema_migration.table_exists?
|
|
|
|
needs_migrations = ActiveRecord::Base.connection.migration_context.needs_migration?
|
|
|
|
|
|
|
|
if no_database || needs_migrations
|
|
|
|
Rake::Task['gitlab:db:configure'].invoke
|
|
|
|
puts "unattended_migrations_completed"
|
|
|
|
else
|
|
|
|
puts "unattended_migrations_static"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-23 01:08:32 -05:00
|
|
|
desc 'GitLab | DB | Checks if migrations require downtime or not'
|
2016-06-24 12:29:23 -04:00
|
|
|
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 16:45:03 -05:00
|
|
|
migrations = `git diff #{ref}.. --diff-filter=A --name-only -- db/migrate`.lines
|
2017-02-22 13:18:40 -05:00
|
|
|
.map { |file| Rails.root.join(file.strip).to_s }
|
|
|
|
.select { |file| File.file?(file) }
|
2017-05-02 05:17:48 -04:00
|
|
|
.select { |file| /\A[0-9]+.*\.rb\z/ =~ File.basename(file) }
|
2016-06-24 12:29:23 -04:00
|
|
|
|
|
|
|
Gitlab::DowntimeCheck.new.check_and_print(migrations)
|
|
|
|
end
|
2019-07-22 11:11:50 -04:00
|
|
|
|
2020-01-23 01:08:32 -05:00
|
|
|
desc 'GitLab | DB | Sets up EE specific database functionality'
|
2019-07-22 11:11:50 -04:00
|
|
|
|
|
|
|
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
|
2020-03-22 11:09:49 -04:00
|
|
|
|
|
|
|
desc 'This adjusts and cleans db/structure.sql - it runs after db:structure:dump'
|
2020-04-09 08:09:24 -04:00
|
|
|
task :clean_structure_sql do |task_name|
|
2020-03-22 11:09:49 -04:00
|
|
|
structure_file = 'db/structure.sql'
|
|
|
|
schema = File.read(structure_file)
|
|
|
|
|
|
|
|
File.open(structure_file, 'wb+') do |io|
|
|
|
|
Gitlab::Database::SchemaCleaner.new(schema).clean(io)
|
|
|
|
end
|
2020-04-09 08:09:24 -04:00
|
|
|
|
|
|
|
# Allow this task to be called multiple times, as happens when running db:migrate:redo
|
|
|
|
Rake::Task[task_name].reenable
|
2020-03-22 11:09:49 -04:00
|
|
|
end
|
|
|
|
|
2020-06-05 05:08:05 -04:00
|
|
|
desc 'This dumps GitLab specific database details - it runs after db:structure:dump'
|
|
|
|
task :dump_custom_structure do |task_name|
|
|
|
|
Gitlab::Database::CustomStructure.new.dump
|
|
|
|
|
|
|
|
# Allow this task to be called multiple times, as happens when running db:migrate:redo
|
|
|
|
Rake::Task[task_name].reenable
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'This loads GitLab specific database details - runs after db:structure:dump'
|
|
|
|
task :load_custom_structure do
|
|
|
|
configuration = Rails.application.config_for(:database)
|
|
|
|
|
|
|
|
ENV['PGHOST'] = configuration['host'] if configuration['host']
|
|
|
|
ENV['PGPORT'] = configuration['port'].to_s if configuration['port']
|
|
|
|
ENV['PGPASSWORD'] = configuration['password'].to_s if configuration['password']
|
|
|
|
ENV['PGUSER'] = configuration['username'].to_s if configuration['username']
|
|
|
|
|
|
|
|
command = 'psql'
|
|
|
|
dump_filepath = Gitlab::Database::CustomStructure.custom_dump_filepath.to_path
|
|
|
|
args = ['-v', 'ON_ERROR_STOP=1', '-q', '-X', '-f', dump_filepath, configuration['database']]
|
|
|
|
|
|
|
|
unless Kernel.system(command, *args)
|
|
|
|
raise "failed to execute:\n#{command} #{args.join(' ')}\n\n" \
|
|
|
|
"Please ensure `#{command}` is installed in your PATH and has proper permissions.\n\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Inform Rake that custom tasks should be run every time rake db:structure:dump is run
|
2020-03-22 11:09:49 -04:00
|
|
|
Rake::Task['db:structure:dump'].enhance do
|
|
|
|
Rake::Task['gitlab:db:clean_structure_sql'].invoke
|
2020-06-05 05:08:05 -04:00
|
|
|
Rake::Task['gitlab:db:dump_custom_structure'].invoke
|
|
|
|
end
|
|
|
|
|
|
|
|
# Inform Rake that custom tasks should be run every time rake db:structure:load is run
|
|
|
|
Rake::Task['db:structure:load'].enhance do
|
|
|
|
Rake::Task['gitlab:db:load_custom_structure'].invoke
|
2020-03-22 11:09:49 -04:00
|
|
|
end
|
2020-07-14 20:09:23 -04:00
|
|
|
|
|
|
|
desc 'Create missing dynamic database partitions'
|
|
|
|
task :create_dynamic_partitions do
|
|
|
|
Gitlab::Database::Partitioning::PartitionCreator.new.create_partitions
|
|
|
|
end
|
|
|
|
|
|
|
|
# This is targeted towards deploys and upgrades of GitLab.
|
|
|
|
# Since we're running migrations already at this time,
|
|
|
|
# we also check and create partitions as needed here.
|
|
|
|
Rake::Task['db:migrate'].enhance do
|
|
|
|
Rake::Task['gitlab:db:create_dynamic_partitions'].invoke
|
|
|
|
end
|
|
|
|
|
|
|
|
# When we load the database schema from db/structure.sql
|
|
|
|
# we don't have any dynamic partitions created. We don't really need to
|
|
|
|
# because application initializers/sidekiq take care of that, too.
|
|
|
|
# However, the presence of partitions for a table has influence on their
|
|
|
|
# position in db/structure.sql (which is topologically sorted).
|
|
|
|
#
|
|
|
|
# Other than that it's helpful to create partitions early when bootstrapping
|
|
|
|
# a new installation.
|
|
|
|
Rake::Task['db:structure:load'].enhance do
|
|
|
|
Rake::Task['gitlab:db:create_dynamic_partitions'].invoke
|
|
|
|
end
|
|
|
|
|
|
|
|
# During testing, db:test:load restores the database schema from scratch
|
|
|
|
# which does not include dynamic partitions. We cannot rely on application
|
|
|
|
# initializers here as the application can continue to run while
|
|
|
|
# a rake task reloads the database schema.
|
|
|
|
Rake::Task['db:test:load'].enhance do
|
|
|
|
Rake::Task['gitlab:db:create_dynamic_partitions'].invoke
|
|
|
|
end
|
2020-09-17 20:09:39 -04:00
|
|
|
|
|
|
|
desc 'reindex a regular (non-unique) index without downtime to eliminate bloat'
|
|
|
|
task :reindex, [:index_name] => :environment do |_, args|
|
2020-09-18 11:09:22 -04:00
|
|
|
unless Feature.enabled?(:database_reindexing, type: :ops)
|
2020-09-22 11:09:37 -04:00
|
|
|
puts "This feature (database_reindexing) is currently disabled.".color(:yellow)
|
2020-09-18 11:09:22 -04:00
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2020-09-25 11:09:36 -04:00
|
|
|
indexes = if args[:index_name]
|
2020-10-13 14:08:58 -04:00
|
|
|
[Gitlab::Database::PostgresIndex.by_identifier(args[:index_name])]
|
2020-09-25 11:09:36 -04:00
|
|
|
else
|
2020-10-01 14:10:20 -04:00
|
|
|
Gitlab::Database::Reindexing.candidate_indexes.random_few(2)
|
2020-09-25 11:09:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Gitlab::Database::Reindexing.perform(indexes)
|
|
|
|
rescue => e
|
|
|
|
Gitlab::AppLogger.error(e)
|
|
|
|
raise
|
2020-09-17 20:09:39 -04:00
|
|
|
end
|
2016-03-08 17:52:48 -05:00
|
|
|
end
|
|
|
|
end
|