From cc8637dcef359b013689e7cee1aa0cdbdc52f545 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Thu, 14 Apr 2022 23:39:10 +0300 Subject: [PATCH] Scope PostgreSQL check constraints to current schemas --- .../postgresql/schema_statements.rb | 2 ++ .../cases/migration/check_constraint_test.rb | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index 4b5e077aaa..ccd8814406 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -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| diff --git a/activerecord/test/cases/migration/check_constraint_test.rb b/activerecord/test/cases/migration/check_constraint_test.rb index a7d119ea72..ef71281bc7 100644 --- a/activerecord/test/cases/migration/check_constraint_test.rb +++ b/activerecord/test/cases/migration/check_constraint_test.rb @@ -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"