Fix remove_check_constraint with if_exists: true never removing any check constraint

It was never finding existing constraints, because `CheckConstraintDefinition` was
validating all options--including `if_exists`--matched.
This commit is contained in:
Cody Cutrer 2023-09-06 13:46:59 -06:00
parent 9044d35c2f
commit e02ac345e8
3 changed files with 7 additions and 5 deletions

@ -1256,10 +1256,10 @@ def check_constraint_options(table_name, expression, options) # :nodoc:
# The +expression+ parameter will be ignored if present. It can be helpful
# to provide this in a migration's +change+ method so it can be reverted.
# In that case, +expression+ will be used by #add_check_constraint.
def remove_check_constraint(table_name, expression = nil, **options)
def remove_check_constraint(table_name, expression = nil, if_exists: false, **options)
return unless supports_check_constraints?
return if options[:if_exists] && !check_constraint_exists?(table_name, **options)
return if if_exists && !check_constraint_exists?(table_name, **options)
chk_name_to_delete = check_constraint_for!(table_name, expression: expression, **options).name

@ -102,8 +102,8 @@ def add_check_constraint(table_name, expression, **options)
end
end
def remove_check_constraint(table_name, expression = nil, **options)
return if options[:if_exists] && !check_constraint_exists?(table_name, **options)
def remove_check_constraint(table_name, expression = nil, if_exists: false, **options)
return if if_exists && !check_constraint_exists?(table_name, **options)
check_constraints = check_constraints(table_name)
chk_name_to_delete = check_constraint_for!(table_name, expression: expression, **options).name

@ -259,7 +259,9 @@ def test_remove_check_constraint
def test_removing_check_constraint_with_if_exists_option
@connection.add_check_constraint :trades, "quantity > 0", name: "quantity_check"
@connection.remove_check_constraint :trades, name: "quantity_check"
assert_nothing_raised do
@connection.remove_check_constraint :trades, name: "quantity_check", if_exists: true
end
error = assert_raises ArgumentError do
@connection.remove_check_constraint :trades, name: "quantity_check"