Skip to content

Commit

Permalink
Support: Generalize one more use case into support.util.refresh_table
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Jun 24, 2024
1 parent 67b2e32 commit ed91bab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
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.TableClause"]):
"""
Invoke a `REFRESH TABLE` statement.
"""
if hasattr(target, "__tablename__"):
sql = f"REFRESH TABLE {target.__tablename__}"

if isinstance(target, sa.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

0 comments on commit ed91bab

Please sign in to comment.