From a8e483eb104eaa2ffabb1f1824a235ccd554f4b6 Mon Sep 17 00:00:00 2001 From: David Gamez Diaz <1192523+davidgamez@users.noreply.github.com> Date: Thu, 11 Apr 2024 17:10:40 -0400 Subject: [PATCH] fix refresh view --- .../batch_process_dataset/src/main.py | 2 +- functions-python/test_utils/database_utils.py | 33 +++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/functions-python/batch_process_dataset/src/main.py b/functions-python/batch_process_dataset/src/main.py index 8cea7fc7b..e8f7ba60d 100644 --- a/functions-python/batch_process_dataset/src/main.py +++ b/functions-python/batch_process_dataset/src/main.py @@ -215,7 +215,7 @@ def create_dataset(self, dataset_file: DatasetFile): session.add(latest_dataset) session.add(new_dataset) - refresh_materialized_view(t_feedsearch.name) + refresh_materialized_view(session, t_feedsearch.name) session.commit() logging.info(f"[{self.feed_stable_id}] Dataset created successfully.") except Exception as e: diff --git a/functions-python/test_utils/database_utils.py b/functions-python/test_utils/database_utils.py index 16d8264d1..96aafa9be 100644 --- a/functions-python/test_utils/database_utils.py +++ b/functions-python/test_utils/database_utils.py @@ -58,17 +58,30 @@ def get_testing_session() -> Session: def clean_testing_db(): - """Truncates all table in the test db.""" + """Deletes all rows from all tables in the test db, excluding those in excluded_tables.""" engine = get_testing_engine() with contextlib.closing(engine.connect()) as con: trans = con.begin() - query = "TRUNCATE {} RESTART IDENTITY;".format( - ",".join( + try: + tables_to_delete = [ table.name - for table in filter( - lambda t: t.name not in excluded_tables, Base.metadata.sorted_tables - ) - ) - ) - con.execute(text(query)) - trans.commit() + for table in reversed(Base.metadata.sorted_tables) + if table.name not in excluded_tables + ] + # Disable triggers for each table + for table_name in tables_to_delete: + con.execute(text(f"ALTER TABLE {table_name} DISABLE TRIGGER ALL;")) + + # Delete all rows from each table + for table_name in tables_to_delete: + delete_query = f"DELETE FROM {table_name};" + con.execute(text(delete_query)) + + # Re-enable triggers for each table + for table_name in tables_to_delete: + con.execute(text(f"ALTER TABLE {table_name} ENABLE TRIGGER ALL;")) + + trans.commit() + except Exception as error: + trans.rollback() + logging.error(f"Error while deleting from test db: {error}")