Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix check used when adding non null constraint #142

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions test/StatementInsurer/change_column_null_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading