Skip to content

Commit

Permalink
Try indices instead of UNIQUE constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Dec 3, 2024
1 parent d291084 commit a567254
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions singer_sdk/connectors/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ def _discover_catalog_entry_from_inspected(
schema_name: str | None,
columns: list[reflection.ReflectedColumn],
primary_key: reflection.ReflectedPrimaryKeyConstraint | None,
unique_constraints: list[reflection.ReflectedUniqueConstraint],
indices: list[reflection.ReflectedIndex],
is_view: bool = False,
) -> CatalogEntry:
unique_stream_id = f"{schema_name}-{table_name}" if schema_name else table_name
Expand All @@ -982,10 +982,11 @@ def _discover_catalog_entry_from_inspected(
if primary_key and "constrained_columns" in primary_key:
possible_primary_keys.append(primary_key["constrained_columns"])

# Check UNIQUE constraints
# Get key property candidates from indices
possible_primary_keys.extend(
unique_constraint["column_names"]
for unique_constraint in unique_constraints
index_def["column_names"] # type: ignore[misc]
for index_def in indices
if index_def.get("unique", False)
)

key_properties = next(iter(possible_primary_keys), [])
Expand Down Expand Up @@ -1059,9 +1060,9 @@ def discover_catalog_entries(
primary_keys = inspected.get_multi_pk_constraint(schema=schema_name)

if reflect_indices:
constraints = inspected.get_multi_unique_constraints(schema=schema_name)
indices = inspected.get_multi_indexes(schema=schema_name)
else:
constraints = {}
indices = {}

for object_kind, is_view in object_kinds:
columns = inspected.get_multi_columns(
Expand All @@ -1075,7 +1076,7 @@ def discover_catalog_entries(
schema_name=schema,
columns=columns[schema, table],
primary_key=primary_keys.get((schema, table)),
unique_constraints=constraints.get((schema, table), []),
indices=indices.get((schema, table), []),
is_view=is_view,
).to_dict()
for schema, table in columns
Expand Down

0 comments on commit a567254

Please sign in to comment.