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:
commit
c9e5057de4
2 changed files with 18 additions and 0 deletions
|
@ -528,8 +528,10 @@ module ActiveRecord
|
|||
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|
|
||||
|
|
|
@ -58,6 +58,22 @@ if ActiveRecord::Base.connection.supports_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"
|
||||
|
||||
|
|
Loading…
Reference in a new issue