Skip to content

Commit

Permalink
Picking only one table for the primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
s7clarke10 authored Oct 9, 2024
1 parent b82bafb commit 7cdc3fe
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions tap_mssql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,31 @@ def discover_catalog(mssql_conn, config):
table_info[db][table] = {"row_count": None, "is_view": table_type == "VIEW"}
LOGGER.info("Tables fetched, fetching columns")
cur.execute(
"""with constraint_columns as (
""" with table_constraints as (
select tc.TABLE_SCHEMA,
tc.TABLE_NAME,
tc.CONSTRAINT_NAME,
tc.CONSTRAINT_TYPE,
row_number() over (partition by tc.TABLE_SCHEMA, tc.TABLE_NAME
order by tc.constraint_TYPE) as row_number_rank
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
where tc.CONSTRAINT_TYPE in ('PRIMARY KEY', 'UNIQUE')
)
,constraint_columns as (
select c.TABLE_SCHEMA
, c.TABLE_NAME
, c.COLUMN_NAME
, c.CONSTRAINT_NAME
from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE c
join INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
join table_constraints tc
on tc.TABLE_SCHEMA = c.TABLE_SCHEMA
and tc.TABLE_NAME = c.TABLE_NAME
and tc.CONSTRAINT_NAME = c.CONSTRAINT_NAME
and tc.CONSTRAINT_TYPE in ('PRIMARY KEY', 'UNIQUE'))
and tc.row_number_rank = 1
)
SELECT c.TABLE_SCHEMA,
c.TABLE_NAME,
c.COLUMN_NAME,
Expand Down

0 comments on commit 7cdc3fe

Please sign in to comment.