Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate check constraints being generated in schema:dump with multiple db schemas #44899

Closed
frank-west-iii opened this issue Apr 14, 2022 · 0 comments · Fixed by #44900
Closed

Comments

@frank-west-iii
Copy link

Steps to reproduce

I've create a repo with more information and reproduction steps here: https://github.com/frank-west-iii/check_demo

Additional tracing info:

check_info
[
  {"conname"=>"users_email_null", "constraintdef"=>"CHECK (email IS NOT NULL) NOT VALID", "valid"=>false},
  {"conname"=>"users_email_null", "constraintdef"=>"CHECK (email IS NOT NULL) NOT VALID", "valid"=>false}
]

Updating to use DISTINCT "fixes" the issue, but that is very likely not the right fix for this issue and is now approaching my edge of knowledge.

          check_info = exec_query(<<-SQL, "SCHEMA")
            SELECT DISTINCT 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
            WHERE c.contype = 'c'
              AND t.relname = #{scope[:name]}
          SQL

Expected behavior

Dumping the db schema should not duplicate check constraints when multiple schemas are present in the database

ApplicationRecord.connection.check_constraints("users")
=> [#<struct ActiveRecord::ConnectionAdapters::CheckConstraintDefinition table_name="users", expression="email IS NOT NULL", options={:name=>"users_email_null", :validate=>false}>]

Actual behavior

Dumping the db schema creates a duplicate check constraints entry when multiple schemas are present in the database

ApplicationRecord.connection.check_constraints("users")
=> [#<struct ActiveRecord::ConnectionAdapters::CheckConstraintDefinition table_name="users", expression="email IS NOT NULL", options={:name=>"users_email_null", :validate=>false}>, #<struct ActiveRecord::ConnectionAdapters::CheckConstraintDefinition table_name="users", expression="email IS NOT NULL", options={:name=>"users_email_null", :validate=>false}>]

System configuration

Rails version: 7.0.2.3

Ruby version: 3.1.1p18

@frank-west-iii frank-west-iii changed the title Duplicate check constraints being generated in schema:dump with multiple db schemas containing same table name Duplicate check constraints being generated in schema:dump with multiple db schemas Apr 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant