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

Support: Generalize one more use case into support.util.refresh_table #140

Merged
merged 1 commit into from
Jun 25, 2024
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
5 changes: 1 addition & 4 deletions src/sqlalchemy_cratedb/support/polyfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ def receive_after_execute(
):
if isinstance(clauseelement, (sa.sql.Insert, sa.sql.Update, sa.sql.Delete)):
if not isinstance(clauseelement.table, sa.sql.Join):
full_table_name = f'"{clauseelement.table.name}"'
if clauseelement.table.schema is not None:
full_table_name = f'"{clauseelement.table.schema}".' + full_table_name
refresh_table(conn, full_table_name)
refresh_table(conn, clauseelement.table)

sa.event.listen(engine, "after_execute", receive_after_execute)

Expand Down
15 changes: 11 additions & 4 deletions src/sqlalchemy_cratedb/support/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@
pass


def refresh_table(connection, target: t.Union[str, "DeclarativeBase"]):
def refresh_table(connection, target: t.Union[str, "DeclarativeBase", "sa.sql.selectable.TableClause"]):
"""
Invoke a `REFRESH TABLE` statement.
"""
if hasattr(target, "__tablename__"):
sql = f"REFRESH TABLE {target.__tablename__}"

if isinstance(target, sa.sql.selectable.TableClause):
full_table_name = f'"{target.name}"'
if target.schema is not None:
full_table_name = f'"{target.schema}".' + full_table_name

Check warning on line 21 in src/sqlalchemy_cratedb/support/util.py

View check run for this annotation

Codecov / codecov/patch

src/sqlalchemy_cratedb/support/util.py#L21

Added line #L21 was not covered by tests
elif hasattr(target, "__tablename__"):
full_table_name = target.__tablename__
else:
sql = f"REFRESH TABLE {target}"
full_table_name = target

Check warning on line 25 in src/sqlalchemy_cratedb/support/util.py

View check run for this annotation

Codecov / codecov/patch

src/sqlalchemy_cratedb/support/util.py#L25

Added line #L25 was not covered by tests

sql = f"REFRESH TABLE {full_table_name}"
connection.execute(sa.text(sql))


Expand Down