diff --git a/sijapi/classes.py b/sijapi/classes.py index a789e1b..6c27104 100644 --- a/sijapi/classes.py +++ b/sijapi/classes.py @@ -833,34 +833,30 @@ class APIConfig(BaseModel): async def execute_write_query(self, query: str, *args, table_name: str): - local_ts_id = os.environ.get('TS_ID') conn = await self.get_connection(self.local_db) if conn is None: err(f"Unable to connect to local database. Write operation failed.") return [] try: - # Remove any formatting from the query - query = ' '.join(query.split()) - - # Add version and server_id to the query - columns = query.split('(')[1].split(')')[0] - values = query.split('VALUES')[1].split('RETURNING')[0] + # Execute the original query + result = await conn.fetch(query, *args) - modified_query = f""" - INSERT INTO {table_name} ({columns}, version, server_id) - VALUES {values[:-1]}, (SELECT COALESCE(MAX(version), 0) + 1 FROM {table_name}), '{local_ts_id}') - ON CONFLICT (id) DO UPDATE SET - version = {table_name}.version + 1, - server_id = EXCLUDED.server_id - RETURNING id, version - """ - - result = await conn.fetch(modified_query, *args) + if result: + # Update version and server_id + update_query = f""" + UPDATE {table_name} + SET version = COALESCE(version, 0) + 1, + server_id = $1 + WHERE id = $2 + RETURNING id, version + """ + update_result = await conn.fetch(update_query, os.environ.get('TS_ID'), result[0]['id']) + return update_result return result except Exception as e: err(f"Error executing write query: {str(e)}") - err(f"Query: {modified_query}") + err(f"Query: {query}") err(f"Args: {args}") err(f"Traceback: {traceback.format_exc()}") return [] @@ -868,7 +864,6 @@ class APIConfig(BaseModel): await conn.close() - async def get_table_columns(self, conn, table_name: str) -> List[str]: query = """ diff --git a/sijapi/routers/weather.py b/sijapi/routers/weather.py index 1dbbc18..d2ff867 100644 --- a/sijapi/routers/weather.py +++ b/sijapi/routers/weather.py @@ -139,11 +139,9 @@ async def get_weather(date_time: dt_datetime, latitude: float, longitude: float, async def store_weather_to_db(date_time: dt_datetime, weather_data: dict): - debug(f"Starting store_weather_to_db for datetime: {date_time.strftime('%Y-%m-%d %H:%M:%S')}") try: day_data = weather_data.get('days', [{}])[0] - debug(f"RAW DAY_DATA: {day_data}") - + preciptype_array = day_data.get('preciptype', []) or [] stations_array = day_data.get('stations', []) or [] @@ -212,7 +210,7 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict): $29, $30, $31, $32, $33, $34, $35, $36, $37, $38 ) RETURNING id ''' - + daily_weather_result = await API.execute_write_query(daily_weather_query, *daily_weather_params, table_name="dailyweather") if not daily_weather_result: