1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #44900 from fatkodima/check_constraints-pg-schemas

Scope PostgreSQL check constraints to current schemas
This commit is contained in:
Ryuta Kamizono 2022-04-15 08:33:14 +09:00 committed by GitHub
commit c9e5057de4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View file

@ -528,8 +528,10 @@ module ActiveRecord
SELECT conname, pg_get_constraintdef(c.oid, true) AS constraintdef, c.convalidated AS valid SELECT conname, pg_get_constraintdef(c.oid, true) AS constraintdef, c.convalidated AS valid
FROM pg_constraint c FROM pg_constraint c
JOIN pg_class t ON c.conrelid = t.oid JOIN pg_class t ON c.conrelid = t.oid
JOIN pg_namespace n ON n.oid = c.connamespace
WHERE c.contype = 'c' WHERE c.contype = 'c'
AND t.relname = #{scope[:name]} AND t.relname = #{scope[:name]}
AND n.nspname = #{scope[:schema]}
SQL SQL
check_info.map do |row| check_info.map do |row|

View file

@ -58,6 +58,22 @@ if ActiveRecord::Base.connection.supports_check_constraints?
end end
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 def test_add_check_constraint
@connection.add_check_constraint :trades, "quantity > 0" @connection.add_check_constraint :trades, "quantity > 0"