Module: SafePgMigrations::StrongMigrationsIntegration

Included in:
Migration
Defined in:
lib/safe-pg-migrations/plugins/strong_migrations_integration.rb

Constant Summary collapse

SAFE_METHODS =
%i[
  execute
  add_index
  add_reference
  add_belongs_to
  change_column_null
  add_foreign_key
  add_check_constraint
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.initializeObject



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
33
34
35
36
37
# File 'lib/safe-pg-migrations/plugins/strong_migrations_integration.rb', line 6

def initialize
  return unless strong_migration_available?

  StrongMigrations.disable_check(:add_column_default)
  StrongMigrations.disable_check(:add_column_default_callable)
  StrongMigrations.add_check do |method, args|
    next unless method == :add_column

    options = args.last.is_a?(Hash) ? args.last : {}

    default_value_backfill = options.fetch(:default_value_backfill, :auto)

    if default_value_backfill == :update_in_batches
      check_message = <<~CHECK
        default_value_backfill: :update_in_batches will take time if the table is too big.

        Your configuration sets a pause of #{SafePgMigrations.config.backfill_pause} seconds between batches of
        #{SafePgMigrations.config.backfill_batch_size} rows. Each batch execution will take time as well. Please
        check that the estimated duration of the migration is acceptable
        before adding `safety_assured`.
      CHECK

      check_message += <<~CHECK if SafePgMigrations.config.default_value_backfill_threshold

        Also, please note that SafePgMigrations is configured to raise if the table has more than
        #{SafePgMigrations.config.default_value_backfill_threshold} rows.
      CHECK

      stop! check_message
    end
  end
end

Instance Method Details

#add_column(table_name, *args, **options) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/safe-pg-migrations/plugins/strong_migrations_integration.rb', line 64

def add_column(table_name, *args, **options)
  return super unless respond_to?(:safety_assured)

  return safety_assured { super } if options.fetch(:default_value_backfill, :auto) == :auto

  super
end