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

Dedupe entries when generating schema tables #5

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sinker"
version = "0.1.1"
version = "0.1.2"
description = "Synchronize Postgres to Elasticsearch"
authors = ["Loren Siebert <[email protected]>"]
license = "MIT/Apache-2.0"
Expand Down
8 changes: 6 additions & 2 deletions src/sinker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

def generate_schema_tables(view_select_query: str) -> Iterable[str]:
"""
Given a view select query, return a list of tables that are referenced in the query.
Given a view select query, return a list of unique tables that are referenced in the query
in the order they were encountered.
Skip anything that looks like a function call.
:param view_select_query: The select query from the view
"""
seen: set = set()
for table_candidate in TABLE_RE.findall(view_select_query):
if "(" not in table_candidate:
yield table_candidate
if table_candidate not in seen:
seen.add(table_candidate)
yield table_candidate
7 changes: 5 additions & 2 deletions tests/test_generate_schema_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ def test_generate_schema_tables():
view_select_query = """select id,
json_build_object(
'name', "name",
'emailDomains',(select array_agg(split_part(email, '@', 2)) FROM unnest(emails) as email),
'otherEmailDomains',(select array_agg(split_part(email, '@', 2)) FROM unnest(emails) as email),
'emailDomains', (select array_agg(split_part(value, '@', 2))
from "EmailAddress" EA where "personId"="Person".id),
'emailAddresses', (select array_agg(value) from "EmailAddress" EA where "personId"="Person".id),
) as "person"
from "person"
"""
assert list(generate_schema_tables(view_select_query)) == ["person"]
assert list(generate_schema_tables(view_select_query)) == ["EmailAddress", "person"]