Auto-update: Mon Jul 29 19:35:42 PDT 2024

This commit is contained in:
sanj 2024-07-29 19:35:42 -07:00
parent 5461c9668d
commit be31fbd36e
2 changed files with 7 additions and 4 deletions

View file

@ -59,10 +59,10 @@ async def lifespan(app: FastAPI):
crit("Starting database synchronization...") crit("Starting database synchronization...")
try: try:
# Initialize sync structures # Initialize sync structures on all databases
await API.initialize_sync() await API.initialize_sync()
# Now that tables are initialized, check for the most recent source # Check if other instances have more recent data
source = await API.get_most_recent_source() source = await API.get_most_recent_source()
if source: if source:
crit(f"Pulling changes from {source['ts_id']} ({source['ts_ip']})...") crit(f"Pulling changes from {source['ts_id']} ({source['ts_ip']})...")
@ -82,6 +82,7 @@ async def lifespan(app: FastAPI):
# Perform any cleanup operations here if needed # Perform any cleanup operations here if needed
app = FastAPI(lifespan=lifespan) app = FastAPI(lifespan=lifespan)
app.add_middleware( app.add_middleware(

View file

@ -315,6 +315,7 @@ class APIConfig(BaseModel):
err(f"Error: {str(e)}") err(f"Error: {str(e)}")
raise raise
async def initialize_sync(self): async def initialize_sync(self):
for pool_entry in self.POOL: for pool_entry in self.POOL:
async with self.get_connection(pool_entry) as conn: async with self.get_connection(pool_entry) as conn:
@ -367,6 +368,7 @@ class APIConfig(BaseModel):
info(f"Sync initialization complete for {pool_entry['ts_ip']}. All tables now have version and server_id columns with appropriate triggers.") info(f"Sync initialization complete for {pool_entry['ts_ip']}. All tables now have version and server_id columns with appropriate triggers.")
async def get_most_recent_source(self): async def get_most_recent_source(self):
most_recent_source = None most_recent_source = None
max_version = -1 max_version = -1
@ -379,8 +381,8 @@ class APIConfig(BaseModel):
async with self.get_connection(pool_entry) as conn: async with self.get_connection(pool_entry) as conn:
version = await conn.fetchval(""" version = await conn.fetchval("""
SELECT COALESCE(MAX(version), -1) FROM ( SELECT COALESCE(MAX(version), -1) FROM (
SELECT MAX(version) as version FROM pg_tables SELECT MAX(version) as version FROM information_schema.columns
WHERE schemaname = 'public' WHERE table_schema = 'public' AND column_name = 'version'
) as subquery ) as subquery
""") """)
if version > max_version: if version > max_version: