Commit 74bfe125 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'pb-configure-task-for-multiple-dbs' into 'master'

Update gitlab:db:configure for multiple databases

See merge request gitlab-org/gitlab!83204
parents 76fea6fa 616fc1f2
...@@ -84,16 +84,38 @@ namespace :gitlab do ...@@ -84,16 +84,38 @@ namespace :gitlab do
desc 'GitLab | DB | Configures the database by running migrate, or by loading the schema and seeding if needed' desc 'GitLab | DB | Configures the database by running migrate, or by loading the schema and seeding if needed'
task configure: :environment do task configure: :environment do
# Check if we have existing db tables databases_with_tasks = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env)
# The schema_migrations table will still exist if drop_tables was called
if ActiveRecord::Base.connection.tables.count > 1 databases_loaded = []
Rake::Task['db:migrate'].invoke
if databases_with_tasks.size == 1
next unless databases_with_tasks.first.name == 'main'
connection = Gitlab::Database.database_base_models['main'].connection
databases_loaded << configure_database(connection)
else else
# Add post-migrate paths to ensure we mark all migrations as up Gitlab::Database.database_base_models.each do |name, model|
next unless databases_with_tasks.any? { |db_with_tasks| db_with_tasks.name == name }
databases_loaded << configure_database(model.connection, database_name: name)
end
end
Rake::Task['db:seed_fu'].invoke if databases_loaded.present? && databases_loaded.all?
end
def configure_database(connection, database_name: nil)
database_name = ":#{database_name}" if database_name
load_database = connection.tables.count <= 1
if load_database
Gitlab::Database.add_post_migrate_path_to_rails(force: true) Gitlab::Database.add_post_migrate_path_to_rails(force: true)
Rake::Task['db:structure:load'].invoke Rake::Task["db:schema:load#{database_name}"].invoke
Rake::Task['db:seed_fu'].invoke else
Rake::Task["db:migrate#{database_name}"].invoke
end end
load_database
end end
desc 'GitLab | DB | Run database migrations and print `unattended_migrations_completed` if action taken' desc 'GitLab | DB | Run database migrations and print `unattended_migrations_completed` if action taken'
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment