diff --git a/lib/strong_migrations/checks.rb b/lib/strong_migrations/checks.rb index fb8aa963..bc3b8cc5 100644 --- a/lib/strong_migrations/checks.rb +++ b/lib/strong_migrations/checks.rb @@ -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" diff --git a/lib/strong_migrations/safe_methods.rb b/lib/strong_migrations/safe_methods.rb index 80860a15..d9bdceb2 100644 --- a/lib/strong_migrations/safe_methods.rb +++ b/lib/strong_migrations/safe_methods.rb @@ -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