Skip to content

Commit

Permalink
Scope PostgreSQL check constraints to current schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Apr 14, 2022
1 parent cdabe88 commit cc8637d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,10 @@ def check_constraints(table_name) # :nodoc:
SELECT conname, pg_get_constraintdef(c.oid, true) AS constraintdef, c.convalidated AS valid
FROM pg_constraint c
JOIN pg_class t ON c.conrelid = t.oid
JOIN pg_namespace n ON n.oid = c.connamespace
WHERE c.contype = 'c'
AND t.relname = #{scope[:name]}
AND n.nspname = #{scope[:schema]}
SQL

check_info.map do |row|
Expand Down
16 changes: 16 additions & 0 deletions activerecord/test/cases/migration/check_constraint_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ def test_check_constraints
end
end

if current_adapter?(:PostgreSQLAdapter)
def test_check_constraints_scoped_to_schemas
@connection.add_check_constraint :trades, "quantity > 0"

assert_no_changes -> { @connection.check_constraints("trades").size } do
@connection.create_schema "test_schema"
@connection.create_table "test_schema.trades" do |t|
t.integer :quantity
end
@connection.add_check_constraint "test_schema.trades", "quantity > 0"
end
ensure
@connection.drop_schema "test_schema"
end
end

def test_add_check_constraint
@connection.add_check_constraint :trades, "quantity > 0"

Expand Down

0 comments on commit cc8637d

Please sign in to comment.