diff --git a/sijapi/__main__.py b/sijapi/__main__.py index c70c4f6..6bfe6b4 100755 --- a/sijapi/__main__.py +++ b/sijapi/__main__.py @@ -59,10 +59,10 @@ async def lifespan(app: FastAPI): crit("Starting database synchronization...") try: - # Initialize sync structures + # Initialize sync structures on all databases 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() if source: 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 + app = FastAPI(lifespan=lifespan) app.add_middleware( diff --git a/sijapi/classes.py b/sijapi/classes.py index dd06260..084118f 100644 --- a/sijapi/classes.py +++ b/sijapi/classes.py @@ -315,6 +315,7 @@ class APIConfig(BaseModel): err(f"Error: {str(e)}") raise + async def initialize_sync(self): for pool_entry in self.POOL: 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.") + async def get_most_recent_source(self): most_recent_source = None max_version = -1 @@ -379,8 +381,8 @@ class APIConfig(BaseModel): async with self.get_connection(pool_entry) as conn: version = await conn.fetchval(""" SELECT COALESCE(MAX(version), -1) FROM ( - SELECT MAX(version) as version FROM pg_tables - WHERE schemaname = 'public' + SELECT MAX(version) as version FROM information_schema.columns + WHERE table_schema = 'public' AND column_name = 'version' ) as subquery """) if version > max_version: