Skip to content

Commit e0f008b

Browse files
authored
Merge pull request #142 from doctolib/fix_change_column_null
Fix check used when adding non null constraint
2 parents fe0be8d + 01617e7 commit e0f008b

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/safe-pg-migrations/plugins/statement_insurer/change_column_null.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def change_column_null(table_name, column_name, null, default = nil)
2727
private
2828

2929
def check_constraint_by_expression(table_name, expression)
30-
check_constraints(table_name).detect { |check_constraint| check_constraint.expression = expression }
30+
check_constraints(table_name).detect { |check_constraint| check_constraint.expression == expression }
3131
end
3232

3333
def should_create_constraint?(default, null)

test/StatementInsurer/change_column_null_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,23 @@ def change
9999
CALLS
100100
end
101101

102+
def test_when_a_non_matching_check_constraint_exists_creates_new_constraint
103+
skip_if_unmet_requirements!
104+
105+
@connection.add_check_constraint :users, 'length(email) > 5', name: 'chk_email_length', validate: true
106+
107+
@migration =
108+
Class.new(ActiveRecord::Migration::Current) do
109+
def change
110+
change_column_null(:users, :email, false)
111+
end
112+
end.new
113+
114+
calls = record_calls(@connection, :execute) { run_migration }
115+
116+
assert_calls met_requirements? ? safe_pg_calls : base_calls, calls
117+
end
118+
102119
private
103120

104121
def safe_pg_calls

0 commit comments

Comments
 (0)