Skip to content

Commit c9e5057

Browse files
authored
Merge pull request rails#44900 from fatkodima/check_constraints-pg-schemas
Scope PostgreSQL check constraints to current schemas
2 parents cbfe735 + cc8637d commit c9e5057

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,10 @@ def check_constraints(table_name) # :nodoc:
528528
SELECT conname, pg_get_constraintdef(c.oid, true) AS constraintdef, c.convalidated AS valid
529529
FROM pg_constraint c
530530
JOIN pg_class t ON c.conrelid = t.oid
531+
JOIN pg_namespace n ON n.oid = c.connamespace
531532
WHERE c.contype = 'c'
532533
AND t.relname = #{scope[:name]}
534+
AND n.nspname = #{scope[:schema]}
533535
SQL
534536

535537
check_info.map do |row|

activerecord/test/cases/migration/check_constraint_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ def test_check_constraints
5858
end
5959
end
6060

61+
if current_adapter?(:PostgreSQLAdapter)
62+
def test_check_constraints_scoped_to_schemas
63+
@connection.add_check_constraint :trades, "quantity > 0"
64+
65+
assert_no_changes -> { @connection.check_constraints("trades").size } do
66+
@connection.create_schema "test_schema"
67+
@connection.create_table "test_schema.trades" do |t|
68+
t.integer :quantity
69+
end
70+
@connection.add_check_constraint "test_schema.trades", "quantity > 0"
71+
end
72+
ensure
73+
@connection.drop_schema "test_schema"
74+
end
75+
end
76+
6177
def test_add_check_constraint
6278
@connection.add_check_constraint :trades, "quantity > 0"
6379

0 commit comments

Comments
 (0)