Commit 542876b8 authored by Andreas Brandl's avatar Andreas Brandl

Inline batched migrations only in dev env

We keep the dev env inlining for convenience for now. Otherwise,
developers would have to run the rake task whenever a data migration had
been scheduled.

For test env, we don't want this. Primarily due to executing this in the
database testing pipeline - where we don't want to inline the full
migration (it can take a long time with data from GitLab.com).

Closes https://gitlab.com/gitlab-org/gitlab/-/issues/328755
parent ba41e132
......@@ -1031,7 +1031,7 @@ module Gitlab
[column, temporary_name]
end
batched_migration = queue_batched_background_migration(
queue_batched_background_migration(
'CopyColumnUsingBackgroundMigrationJob',
table,
primary_key,
......@@ -1040,12 +1040,6 @@ module Gitlab
job_interval: interval,
batch_size: batch_size,
sub_batch_size: sub_batch_size)
if perform_background_migration_inline?
# To ensure the schema is up to date immediately we perform the
# migration inline in dev / test environments.
Gitlab::Database::BackgroundMigration::BatchedMigrationRunner.new.run_entire_migration(batched_migration)
end
end
# Performs a concurrent column rename when using PostgreSQL.
......
......@@ -248,5 +248,19 @@ namespace :gitlab do
ActiveRecord::Base.clear_cache!
ActiveRecord::Migration.verbose = verbose_was
end
desc 'Run all pending batched migrations'
task execute_batched_migrations: :environment do
Gitlab::Database::BackgroundMigration::BatchedMigration.active.queue_order.each do |migration|
Gitlab::AppLogger.info("Executing batched migration #{migration.id} inline")
Gitlab::Database::BackgroundMigration::BatchedMigrationRunner.new.run_entire_migration(migration)
end
end
# Only for development environments,
# we execute pending data migrations inline for convenience.
Rake::Task['db:migrate'].enhance do
Rake::Task['gitlab:db:execute_batched_migrations'].invoke if Rails.env.development?
end
end
end
......@@ -1902,22 +1902,6 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
)
end
end
context 'when the migration should be performed inline' do
let(:columns) { column }
it 'calls the runner to run the entire migration' do
expect(model).to receive(:perform_background_migration_inline?).and_return(true)
expect_next_instance_of(Gitlab::Database::BackgroundMigration::BatchedMigrationRunner) do |scheduler|
expect(scheduler).to receive(:run_entire_migration) do |batched_migration|
expect(batched_migration).to eq(migration_relation.last)
end
end
model.backfill_conversion_of_integer_to_bigint(table, column, batch_size: 2, sub_batch_size: 1)
end
end
end
end
......
......@@ -348,6 +348,26 @@ RSpec.describe 'gitlab:db namespace rake task' do
end
end
describe '#execute_batched_migrations' do
subject { run_rake_task('gitlab:db:execute_batched_migrations') }
let(:migrations) { create_list(:batched_background_migration, 2) }
let(:runner) { instance_double('Gitlab::Database::BackgroundMigration::BatchedMigrationRunner') }
before do
allow(Gitlab::Database::BackgroundMigration::BatchedMigration).to receive_message_chain(:active, :queue_order).and_return(migrations)
allow(Gitlab::Database::BackgroundMigration::BatchedMigrationRunner).to receive(:new).and_return(runner)
end
it 'executes all migrations' do
migrations.each do |migration|
expect(runner).to receive(:run_entire_migration).with(migration)
end
subject
end
end
def run_rake_task(task_name, arguments = '')
Rake::Task[task_name].reenable
Rake.application.invoke_task("#{task_name}#{arguments}")
......
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