Commit c730563e authored by Dmytro Zaporozhets (DZ)'s avatar Dmytro Zaporozhets (DZ)

Merge branch '198-add-automation-friendly-migration-rake-tasks' into 'master'

Add unattended database migration option

See merge request gitlab-org/gitlab!44392
parents 6abe2c3b d35eb49f
---
title: Add unattended database migration option
merge_request: 44392
author:
type: added
......@@ -65,6 +65,19 @@ namespace :gitlab do
end
end
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
desc 'GitLab | DB | 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]
......
......@@ -98,6 +98,29 @@ RSpec.describe 'gitlab:db namespace rake task' do
end
end
describe 'unattended' do
using RSpec::Parameterized::TableSyntax
where(:schema_migration_table_exists, :needs_migrations, :rake_output) do
false | false | "unattended_migrations_completed"
false | true | "unattended_migrations_completed"
true | false | "unattended_migrations_static"
true | true | "unattended_migrations_completed"
end
before do
allow(Rake::Task['gitlab:db:configure']).to receive(:invoke).and_return(true)
end
with_them do
it 'outputs changed message for automation after operations happen' do
allow(ActiveRecord::Base.connection.schema_migration).to receive(:table_exists?).and_return(schema_migration_table_exists)
allow_any_instance_of(ActiveRecord::MigrationContext).to receive(:needs_migration?).and_return(needs_migrations)
expect { run_rake_task('gitlab:db:unattended') }. to output(/^#{rake_output}$/).to_stdout
end
end
end
describe 'clean_structure_sql' do
let_it_be(:clean_rake_task) { 'gitlab:db:clean_structure_sql' }
let_it_be(:test_task_name) { 'gitlab:db:_test_multiple_structure_cleans' }
......
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