Auto-update: Tue Jul 30 15:36:57 PDT 2024

This commit is contained in:
sanj 2024-07-30 15:36:57 -07:00
parent 1fe99ec75d
commit 612f2b31c0

View file

@ -603,6 +603,12 @@ class APIConfig(BaseModel):
for table in tables: for table in tables:
table_name = table['tablename'] table_name = table['tablename']
try: try:
if table_name == 'spatial_ref_sys':
changes_count = await self.sync_spatial_ref_sys(source_conn, dest_conn)
total_changes += changes_count
info(f"Synced spatial_ref_sys: {changes_count} changes. Total so far: {total_changes}")
else:
# Existing code for other tables
last_synced_version = await self.get_last_synced_version(dest_conn, table_name, source_id) last_synced_version = await self.get_last_synced_version(dest_conn, table_name, source_id)
changes = await source_conn.fetch(f""" changes = await source_conn.fetch(f"""
@ -647,12 +653,24 @@ class APIConfig(BaseModel):
# Convert the keys to a list # Convert the keys to a list
columns = list(changes[0].keys()) columns = list(changes[0].keys())
placeholders = [f'${i+1}' for i in range(len(columns))] placeholders = [f'${i+1}' for i in range(len(columns))]
# Check if 'id' column exists
id_exists = 'id' in columns
if id_exists:
insert_query = f""" insert_query = f"""
INSERT INTO "{table_name}" ({', '.join(columns)}) INSERT INTO "{table_name}" ({', '.join(columns)})
VALUES ({', '.join(placeholders)}) VALUES ({', '.join(placeholders)})
ON CONFLICT (id) DO UPDATE SET ON CONFLICT (id) DO UPDATE SET
{', '.join(f"{col} = EXCLUDED.{col}" for col in columns if col != 'id')} {', '.join(f"{col} = EXCLUDED.{col}" for col in columns if col != 'id')}
""" """
else:
# For tables without 'id', use all columns as conflict target
insert_query = f"""
INSERT INTO "{table_name}" ({', '.join(columns)})
VALUES ({', '.join(placeholders)})
ON CONFLICT DO NOTHING
"""
# Execute the insert for each change # Execute the insert for each change
affected_rows = 0 affected_rows = 0
@ -669,6 +687,56 @@ class APIConfig(BaseModel):
return 0 return 0
async def sync_spatial_ref_sys(self, source_conn, dest_conn):
try:
# Get all entries from the source
source_entries = await source_conn.fetch("""
SELECT * FROM spatial_ref_sys
ORDER BY srid
""")
# Get all entries from the destination
dest_entries = await dest_conn.fetch("""
SELECT * FROM spatial_ref_sys
ORDER BY srid
""")
# Convert to dictionaries for easier comparison
source_dict = {entry['srid']: entry for entry in source_entries}
dest_dict = {entry['srid']: entry for entry in dest_entries}
updates = 0
inserts = 0
for srid, source_entry in source_dict.items():
if srid not in dest_dict:
# Insert new entry
columns = source_entry.keys()
placeholders = [f'${i+1}' for i in range(len(columns))]
insert_query = f"""
INSERT INTO spatial_ref_sys ({', '.join(columns)})
VALUES ({', '.join(placeholders)})
"""
await dest_conn.execute(insert_query, *source_entry.values())
inserts += 1
elif source_entry != dest_dict[srid]:
# Update existing entry
update_query = f"""
UPDATE spatial_ref_sys
SET {', '.join(f"{col} = ${i+1}" for i, col in enumerate(source_entry.keys()) if col != 'srid')}
WHERE srid = ${len(source_entry)}
"""
await dest_conn.execute(update_query, *[v for k, v in source_entry.items() if k != 'srid'], srid)
updates += 1
info(f"spatial_ref_sys sync complete. Inserts: {inserts}, Updates: {updates}")
return inserts + updates
except Exception as e:
err(f"Error syncing spatial_ref_sys table: {str(e)}")
err(f"Traceback: {traceback.format_exc()}")
return 0
async def push_changes_to_all(self): async def push_changes_to_all(self):
for pool_entry in self.POOL: for pool_entry in self.POOL: