Skip to content

Commit

Permalink
Use methods for change_column_null with safe_by_default
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Nov 7, 2024
1 parent 84dee68 commit c79a07b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
14 changes: 7 additions & 7 deletions lib/strong_migrations/checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,21 +234,21 @@ def check_change_column_null(*args)
# match https://github.com/nullobject/rein
constraint_name = "#{table}_#{column}_null"

add_code = constraint_str("ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s IS NOT NULL) NOT VALID", [table, constraint_name, column])
validate_code = constraint_str("ALTER TABLE %s VALIDATE CONSTRAINT %s", [table, constraint_name])
add_args = [table, "#{quote_column_if_needed(column)} IS NOT NULL", {name: constraint_name, validate: false}]
validate_args = [table, {name: constraint_name}]
change_args = [table, column, null]
remove_code = constraint_str("ALTER TABLE %s DROP CONSTRAINT %s", [table, constraint_name])
remove_args = [table, {name: constraint_name}]

if StrongMigrations.safe_by_default
safe_change_column_null(add_code, validate_code, change_args, remove_code, default)
safe_change_column_null(add_args, validate_args, change_args, remove_args, default)
throw :safe
end

add_constraint_code = command_str(:add_check_constraint, [table, "#{quote_column_if_needed(column)} IS NOT NULL", {name: constraint_name, validate: false}])
add_constraint_code = command_str(:add_check_constraint, add_args)

up_code = String.new(command_str(:validate_check_constraint, [table, {name: constraint_name}]))
up_code = String.new(command_str(:validate_check_constraint, validate_args))
up_code << "\n #{command_str(:change_column_null, change_args)}"
up_code << "\n #{command_str(:remove_check_constraint, [table, {name: constraint_name}])}"
up_code << "\n #{command_str(:remove_check_constraint, remove_args)}"
down_code = "#{add_constraint_code}\n #{command_str(:change_column_null, [table, column, true])}"
validate_constraint_code = "def up\n #{up_code}\n end\n\n def down\n #{down_code}\n end"

Expand Down
18 changes: 8 additions & 10 deletions lib/strong_migrations/safe_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,23 @@ def safe_add_check_constraint(table, expression, *args, add_options, validate_op
end
end

def safe_change_column_null(add_code, validate_code, change_args, remove_code, default)
def safe_change_column_null(add_args, validate_args, change_args, remove_args, default)
@migration.reversible do |dir|
dir.up do
unless default.nil?
raise Error, "default value not supported yet with safe_by_default"
end

@migration.safety_assured do
@migration.execute(add_code)
end
add_options = add_args.extract_options!
validate_options = validate_args.extract_options!
remove_options = remove_args.extract_options!

@migration.add_check_constraint(*add_args, **add_options)
disable_transaction
@migration.connection.begin_db_transaction
@migration.safety_assured do
@migration.execute(validate_code)
end
@migration.validate_check_constraint(*validate_args, **validate_options)
@migration.change_column_null(*change_args)
@migration.safety_assured do
@migration.execute(remove_code)
end
@migration.remove_check_constraint(*remove_args, **remove_options)
@migration.connection.commit_db_transaction
end
dir.down do
Expand Down

0 comments on commit c79a07b

Please sign in to comment.