From 7dcb71d6adb864dc4f84ccd0e8e1b84b7211183f Mon Sep 17 00:00:00 2001 From: sanj <67624670+iodrift@users.noreply.github.com> Date: Mon, 29 Jul 2024 18:17:53 -0700 Subject: [PATCH] Auto-update: Mon Jul 29 18:17:53 PDT 2024 --- sijapi/classes.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sijapi/classes.py b/sijapi/classes.py index b043445..f18f18c 100644 --- a/sijapi/classes.py +++ b/sijapi/classes.py @@ -378,6 +378,7 @@ class APIConfig(BaseModel): return most_recent_source + async def pull_changes(self, source_pool_entry): total_inserts = 0 total_updates = 0 @@ -392,8 +393,7 @@ class APIConfig(BaseModel): try: async with self.get_connection(source_pool_entry) as source_conn: - async with self.get_connection(self.local_db) as dest_conn: # Connect to local DB explicitly - # Compare tables + async with self.get_connection(self.local_db) as dest_conn: source_tables = await self.get_tables(source_conn) dest_tables = await self.get_tables(dest_conn) @@ -405,6 +405,12 @@ class APIConfig(BaseModel): info(f"Tables only in destination: {tables_only_in_dest}") info(f"Common tables: {common_tables}") + for table in tables_only_in_source: + create_table_stmt = await source_conn.fetchval(f"SELECT pg_get_tabledef('{table}'::regclass::oid);") + await dest_conn.execute(create_table_stmt) + info(f"Created table '{table}' in destination database.") + common_tables.add(table) + for table in common_tables: await self.compare_table_structure(source_conn, dest_conn, table) inserts, updates = await self.compare_and_sync_data(source_conn, dest_conn, table, source_id) @@ -413,10 +419,6 @@ class APIConfig(BaseModel): total_updates += updates table_changes[table] = {'inserts': inserts, 'updates': updates} - # Optionally, handle tables only in source - for table in tables_only_in_source: - warn(f"Table '{table}' exists in source but not in destination. Consider manual migration.") - info(f"Comprehensive sync complete from {source_id} ({source_ip}) to {dest_id} ({dest_ip})") info(f"Total changes: {total_inserts} inserts, {total_updates} updates") info("Changes by table:") @@ -429,8 +431,6 @@ class APIConfig(BaseModel): return total_inserts + total_updates - - async def get_tables(self, conn): tables = await conn.fetch(""" SELECT tablename FROM pg_tables