Auto-update: Mon Jul 29 18:17:53 PDT 2024

This commit is contained in:
sanj 2024-07-29 18:17:53 -07:00
parent 57ea1db0b2
commit 7dcb71d6ad

View file

@ -378,6 +378,7 @@ class APIConfig(BaseModel):
return most_recent_source return most_recent_source
async def pull_changes(self, source_pool_entry): async def pull_changes(self, source_pool_entry):
total_inserts = 0 total_inserts = 0
total_updates = 0 total_updates = 0
@ -392,8 +393,7 @@ class APIConfig(BaseModel):
try: try:
async with self.get_connection(source_pool_entry) as source_conn: 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 async with self.get_connection(self.local_db) as dest_conn:
# Compare tables
source_tables = await self.get_tables(source_conn) source_tables = await self.get_tables(source_conn)
dest_tables = await self.get_tables(dest_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"Tables only in destination: {tables_only_in_dest}")
info(f"Common tables: {common_tables}") 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: for table in common_tables:
await self.compare_table_structure(source_conn, dest_conn, table) 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) 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 total_updates += updates
table_changes[table] = {'inserts': inserts, '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"Comprehensive sync complete from {source_id} ({source_ip}) to {dest_id} ({dest_ip})")
info(f"Total changes: {total_inserts} inserts, {total_updates} updates") info(f"Total changes: {total_inserts} inserts, {total_updates} updates")
info("Changes by table:") info("Changes by table:")
@ -429,8 +431,6 @@ class APIConfig(BaseModel):
return total_inserts + total_updates return total_inserts + total_updates
async def get_tables(self, conn): async def get_tables(self, conn):
tables = await conn.fetch(""" tables = await conn.fetch("""
SELECT tablename FROM pg_tables SELECT tablename FROM pg_tables