Module: SafePgMigrations::StatementInsurer::AddColumn

Included in:
SafePgMigrations::StatementInsurer
Defined in:
lib/safe-pg-migrations/plugins/statement_insurer/add_column.rb

Instance Method Summary collapse

Instance Method Details

#add_column(table_name, column_name, type, **options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/safe-pg-migrations/plugins/statement_insurer/add_column.rb', line 6

def add_column(table_name, column_name, type, **options)
  return super if should_keep_default_implementation?(**options)

  options.delete(:default_value_backfill)

  raise <<~ERROR unless backfill_column_default_safe?(table_name)
    Table #{table_name} has more than #{SafePgMigrations.config.default_value_backfill_threshold} rows.
    Backfilling the default value for column #{column_name} on table #{table_name} would take too long.

    Please revert this migration, and backfill the default value manually.

    This check is configurable through the configuration "default_value_backfill_threshold".
  ERROR

  default = options.delete(:default)
  null = options.delete(:null)

  Helpers::Logger.say_method_call(:add_column, table_name, column_name, type, options)
  super(table_name, column_name, type, **options)

  Helpers::Logger.say_method_call(:change_column_default, table_name, column_name, default)
  change_column_default(table_name, column_name, default)

  backfill_column_default(table_name, column_name)

  change_column_null(table_name, column_name, null) if null == false
end