diff --git a/lib/safe-pg-migrations/plugins/statement_insurer/change_column_null.rb b/lib/safe-pg-migrations/plugins/statement_insurer/change_column_null.rb index 85624bb..6cb85da 100644 --- a/lib/safe-pg-migrations/plugins/statement_insurer/change_column_null.rb +++ b/lib/safe-pg-migrations/plugins/statement_insurer/change_column_null.rb @@ -27,7 +27,7 @@ def change_column_null(table_name, column_name, null, default = nil) private def check_constraint_by_expression(table_name, expression) - check_constraints(table_name).detect { |check_constraint| check_constraint.expression = expression } + check_constraints(table_name).detect { |check_constraint| check_constraint.expression == expression } end def should_create_constraint?(default, null) diff --git a/test/StatementInsurer/change_column_null_test.rb b/test/StatementInsurer/change_column_null_test.rb index 85f9fae..6a1a49a 100644 --- a/test/StatementInsurer/change_column_null_test.rb +++ b/test/StatementInsurer/change_column_null_test.rb @@ -99,6 +99,23 @@ def change CALLS end + def test_when_a_non_matching_check_constraint_exists_creates_new_constraint + skip_if_unmet_requirements! + + @connection.add_check_constraint :users, 'length(email) > 5', name: 'chk_email_length', validate: true + + @migration = + Class.new(ActiveRecord::Migration::Current) do + def change + change_column_null(:users, :email, false) + end + end.new + + calls = record_calls(@connection, :execute) { run_migration } + + assert_calls met_requirements? ? safe_pg_calls : base_calls, calls + end + private def safe_pg_calls