Class: Gitlab::BackgroundMigration::CleanupConcurrentSchemaChange

Inherits:
Object
  • Object
show all
Includes:
Database::MigrationHelpers
Defined in:
lib/gitlab/background_migration/cleanup_concurrent_schema_change.rb

Overview

Base class for background migration for rename/type changes.

Constant Summary

Constants included from Database::MigrationHelpers

Database::MigrationHelpers::DEFAULT_TIMESTAMP_COLUMNS, Database::MigrationHelpers::MAX_IDENTIFIER_NAME_LENGTH, Database::MigrationHelpers::PERMITTED_TIMESTAMP_COLUMNS

Constants included from Database::Migrations::BackgroundMigrationHelpers

Database::Migrations::BackgroundMigrationHelpers::BACKGROUND_MIGRATION_BATCH_SIZE, Database::Migrations::BackgroundMigrationHelpers::BACKGROUND_MIGRATION_JOB_BUFFER_SIZE

Instance Method Summary collapse

Methods included from Database::MigrationHelpers

#add_check_constraint, #add_column_with_default, #add_concurrent_foreign_key, #add_concurrent_index, #add_not_null_constraint, #add_text_limit, #add_timestamps_with_timezone, #backfill_iids, #change_column_type_concurrently, #change_column_type_using_background_migration, #check_constraint_exists?, #check_constraint_name, #check_not_null_constraint_exists?, #check_text_limit_exists?, #check_trigger_permissions!, #cleanup_concurrent_column_rename, #cleanup_concurrent_column_type_change, #column_for, #concurrent_foreign_key_name, #copy_foreign_keys, #copy_indexes, #create_extension, #create_or_update_plan_limit, #disable_statement_timeout, #drop_extension, #false_value, #foreign_key_exists?, #foreign_keys_for, #index_exists_by_name?, #indexes_for, #install_rename_triggers, #install_rename_triggers_for_postgresql, #postgres_exists_by_name?, #remove_check_constraint, #remove_concurrent_index, #remove_concurrent_index_by_name, #remove_foreign_key_if_exists, #remove_foreign_key_without_error, #remove_not_null_constraint, #remove_rename_triggers_for_postgresql, #remove_text_limit, #remove_timestamps, #rename_column_concurrently, #rename_column_using_background_migration, #rename_trigger_name, #replace_sql, #sidekiq_queue_length, #sidekiq_queue_migrate, #true_value, #undo_cleanup_concurrent_column_rename, #undo_rename_column_concurrently, #update_column_in_batches, #validate_check_constraint, #validate_foreign_key, #validate_not_null_constraint, #validate_text_limit, #with_lock_retries

Methods included from Database::Migrations::BackgroundMigrationHelpers

#bulk_migrate_async, #bulk_migrate_in, #bulk_queue_background_migration_jobs_by_range, #migrate_async, #migrate_in, #perform_background_migration_inline?, #queue_background_migration_jobs_by_range_at_intervals, #with_migration_context

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object


41
42
43
# File 'lib/gitlab/background_migration/cleanup_concurrent_schema_change.rb', line 41

def method_missing(name, *args, &block)
  connection.__send__(name, *args, &block) # rubocop: disable GitlabSecurity/PublicSend
end

Instance Method Details

#cleanup_concurrent_schema_change(_table, _old_column, _new_column) ⇒ Object

Raises:

  • (NotImplementedError)

31
32
33
# File 'lib/gitlab/background_migration/cleanup_concurrent_schema_change.rb', line 31

def cleanup_concurrent_schema_change(_table, _old_column, _new_column)
  raise NotImplementedError
end

#connectionObject

These methods are necessary so we can re-use the migration helpers in this class.


37
38
39
# File 'lib/gitlab/background_migration/cleanup_concurrent_schema_change.rb', line 37

def connection
  ActiveRecord::Base.connection
end

#define_model_for(table) ⇒ Object


49
50
51
52
53
# File 'lib/gitlab/background_migration/cleanup_concurrent_schema_change.rb', line 49

def define_model_for(table)
  Class.new(ActiveRecord::Base) do
    self.table_name = table
  end
end

#perform(table, old_column, new_column) ⇒ Object

table - The name of the table the migration is performed for. old_column - The name of the old (to drop) column. new_column - The name of the new column.


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gitlab/background_migration/cleanup_concurrent_schema_change.rb', line 12

def perform(table, old_column, new_column)
  return unless column_exists?(table, new_column) && column_exists?(table, old_column)

  rows_to_migrate = define_model_for(table)
    .where(new_column => nil)
    .where
    .not(old_column => nil)

  if rows_to_migrate.any?
    BackgroundMigrationWorker.perform_in(
      RESCHEDULE_DELAY,
      self.class.name,
      [table, old_column, new_column]
    )
  else
    cleanup_concurrent_schema_change(table, old_column, new_column)
  end
end

#respond_to_missing?(*args) ⇒ Boolean

Returns:

  • (Boolean)

45
46
47
# File 'lib/gitlab/background_migration/cleanup_concurrent_schema_change.rb', line 45

def respond_to_missing?(*args)
  connection.respond_to?(*args) || super
end