Skip to content

Commit

Permalink
fix(schema_statements): Add hidden column check
Browse files Browse the repository at this point in the history
  • Loading branch information
BuonOmo authored and rafiss committed Jul 1, 2024
1 parent 1d98132 commit 0f9a418
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def primary_key(table_name)

# OVERRIDE: Added `unique_rowid` to the last line of the second query.
# This is a CockroachDB-specific function used for primary keys.
# Also make sure we don't consider `NOT VISIBLE` columns.
#
# Returns a table's primary key and belonging sequence.
def pk_and_sequence_for(table) # :nodoc:
Expand All @@ -68,14 +69,21 @@ def pk_and_sequence_for(table) # :nodoc:
pg_attribute attr,
pg_depend dep,
pg_constraint cons,
pg_namespace nsp
pg_namespace nsp,
-- TODO: use the pg_catalog.pg_attribute(attishidden) column when
-- it is added instead of joining on crdb_internal.
-- See https://github.com/cockroachdb/cockroach/pull/126397
crdb_internal.table_columns tc
WHERE seq.oid = dep.objid
AND seq.relkind = 'S'
AND attr.attrelid = dep.refobjid
AND attr.attnum = dep.refobjsubid
AND attr.attrelid = cons.conrelid
AND attr.attnum = cons.conkey[1]
AND seq.relnamespace = nsp.oid
AND attr.attrelid = tc.descriptor_id
AND attr.attname = tc.column_name
AND tc.hidden = false
AND cons.contype = 'p'
AND dep.classid = 'pg_class'::regclass
AND dep.refobjid = #{quote(quote_table_name(table))}::regclass
Expand All @@ -96,7 +104,12 @@ def pk_and_sequence_for(table) # :nodoc:
JOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum)
JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])
JOIN pg_namespace nsp ON (t.relnamespace = nsp.oid)
-- TODO: use the pg_catalog.pg_attribute(attishidden) column when
-- it is added instead of joining on crdb_internal.
-- See https://github.com/cockroachdb/cockroach/pull/126397
JOIN crdb_internal.table_columns tc ON (attr.attrelid = tc.descriptor_id AND attr.attname = tc.column_name)
WHERE t.oid = #{quote(quote_table_name(table))}::regclass
AND tc.hidden = false
AND cons.contype = 'p'
AND pg_get_expr(def.adbin, def.adrelid) ~* 'nextval|uuid_generate|gen_random_uuid|unique_rowid'
SQL
Expand Down

0 comments on commit 0f9a418

Please sign in to comment.