Commit 4102b96f authored by Andreas Brandl's avatar Andreas Brandl Committed by Yannis Roussos

Capture total_tuple_count only if present

The column `total_tuple_count` has only been introduced after the
migration helper had been used for the first time. That is, there is a
preceding migration using the helper. This migration and its schema
version does not know about the later column addition and hence the code
path fails.

We check if the attribute exists here, so we can safely work with it.

This is only needed in code paths that are already being used in
existing migrations.

See https://gitlab.com/gitlab-org/gitlab/-/issues/327447
parent ca02d83c
...@@ -6,13 +6,13 @@ class BackfillEventsIdForBigintConversion < ActiveRecord::Migration[6.0] ...@@ -6,13 +6,13 @@ class BackfillEventsIdForBigintConversion < ActiveRecord::Migration[6.0]
DOWNTIME = false DOWNTIME = false
def up def up
return unless Gitlab.dev_env_or_com? return unless should_run?
backfill_conversion_of_integer_to_bigint :events, :id, batch_size: 15000, sub_batch_size: 100 backfill_conversion_of_integer_to_bigint :events, :id, batch_size: 15000, sub_batch_size: 100
end end
def down def down
return unless Gitlab.dev_env_or_com? return unless should_run?
Gitlab::Database::BackgroundMigration::BatchedMigration Gitlab::Database::BackgroundMigration::BatchedMigration
.where(job_class_name: 'CopyColumnUsingBackgroundMigrationJob') .where(job_class_name: 'CopyColumnUsingBackgroundMigrationJob')
...@@ -20,4 +20,10 @@ class BackfillEventsIdForBigintConversion < ActiveRecord::Migration[6.0] ...@@ -20,4 +20,10 @@ class BackfillEventsIdForBigintConversion < ActiveRecord::Migration[6.0]
.where('job_arguments = ?', %w[id id_convert_to_bigint].to_json) .where('job_arguments = ?', %w[id id_convert_to_bigint].to_json)
.delete_all .delete_all
end end
private
def should_run?
Gitlab.dev_or_test_env? || Gitlab.com?
end
end end
...@@ -6,14 +6,14 @@ class BackfillPushEventPayloadEventIdForBigintConversion < ActiveRecord::Migrati ...@@ -6,14 +6,14 @@ class BackfillPushEventPayloadEventIdForBigintConversion < ActiveRecord::Migrati
DOWNTIME = false DOWNTIME = false
def up def up
return unless Gitlab.dev_env_or_com? return unless should_run?
backfill_conversion_of_integer_to_bigint :push_event_payloads, :event_id, primary_key: :event_id, backfill_conversion_of_integer_to_bigint :push_event_payloads, :event_id, primary_key: :event_id,
batch_size: 15000, sub_batch_size: 100 batch_size: 15000, sub_batch_size: 100
end end
def down def down
return unless Gitlab.dev_env_or_com? return unless should_run?
Gitlab::Database::BackgroundMigration::BatchedMigration Gitlab::Database::BackgroundMigration::BatchedMigration
.where(job_class_name: 'CopyColumnUsingBackgroundMigrationJob') .where(job_class_name: 'CopyColumnUsingBackgroundMigrationJob')
...@@ -21,4 +21,10 @@ class BackfillPushEventPayloadEventIdForBigintConversion < ActiveRecord::Migrati ...@@ -21,4 +21,10 @@ class BackfillPushEventPayloadEventIdForBigintConversion < ActiveRecord::Migrati
.where('job_arguments = ?', %w[event_id event_id_convert_to_bigint].to_json) .where('job_arguments = ?', %w[event_id event_id_convert_to_bigint].to_json)
.delete_all .delete_all
end end
private
def should_run?
Gitlab.dev_or_test_env? || Gitlab.com?
end
end end
...@@ -190,11 +190,7 @@ module Gitlab ...@@ -190,11 +190,7 @@ module Gitlab
migration_status = batch_max_value.nil? ? :finished : :active migration_status = batch_max_value.nil? ? :finished : :active
batch_max_value ||= batch_min_value batch_max_value ||= batch_min_value
# We keep track of the estimated number of tuples to reason later migration = Gitlab::Database::BackgroundMigration::BatchedMigration.create!(
# about the overall progress of a migration.
total_tuple_count = Gitlab::Database::PgClass.for_table(batch_table_name)&.cardinality_estimate
Gitlab::Database::BackgroundMigration::BatchedMigration.create!(
job_class_name: job_class_name, job_class_name: job_class_name,
table_name: batch_table_name, table_name: batch_table_name,
column_name: batch_column_name, column_name: batch_column_name,
...@@ -205,8 +201,18 @@ module Gitlab ...@@ -205,8 +201,18 @@ module Gitlab
batch_size: batch_size, batch_size: batch_size,
sub_batch_size: sub_batch_size, sub_batch_size: sub_batch_size,
job_arguments: job_arguments, job_arguments: job_arguments,
status: migration_status, status: migration_status)
total_tuple_count: total_tuple_count)
# This guard is necessary since #total_tuple_count was only introduced schema-wise,
# after this migration helper had been used for the first time.
return migration unless migration.respond_to?(:total_tuple_count)
# We keep track of the estimated number of tuples to reason later
# about the overall progress of a migration.
migration.total_tuple_count = Gitlab::Database::PgClass.for_table(batch_table_name)&.cardinality_estimate
migration.save!
migration
end end
def perform_background_migration_inline? def perform_background_migration_inline?
......
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