Module: SafePgMigrations::StatementInsurer::ChangeColumnNull

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

Instance Method Summary collapse

Instance Method Details

#change_column_null(table_name, column_name, null, default = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/safe-pg-migrations/plugins/statement_insurer/change_column_null.rb', line 6

def change_column_null(table_name, column_name, null, default = nil)
  return super unless should_create_constraint? default, null

  expression = "#{column_name} IS NOT NULL"
  # constraint will be defined if the constraint was manually created in another migration
  constraint = check_constraint_by_expression table_name, expression

  default_name = check_constraint_name(table_name, expression: expression)
  constraint_name = constraint&.name || default_name

  add_check_constraint table_name, expression, name: constraint_name

  Helpers::Logger.say_method_call :change_column_null, table_name, column_name, false
  super(table_name, column_name, false)

  return unless should_remove_constraint? default_name, constraint_name

  Helpers::Logger.say_method_call :remove_check_constraint, table_name, expression, name: constraint_name
  remove_check_constraint table_name, expression, name: constraint_name
end