Auto-update: Tue Aug 6 23:11:38 PDT 2024

This commit is contained in:
sanj 2024-08-06 23:11:38 -07:00
parent 53177d249d
commit a24dd829de
2 changed files with 9 additions and 18 deletions

View file

@ -849,15 +849,18 @@ class APIConfig(BaseModel):
if table_name in self.SPECIAL_TABLES:
result = await self._execute_special_table_write(conn, query, *args, table_name=table_name)
else:
# Remove newlines and extra spaces from the query
query = query.replace('\n', ' ').replace(' ', ' ').strip()
# Prepare the INSERT ... ON CONFLICT ... query
insert_cols = ', '.join(f'"{col}"' for col in query.split('(')[1].split(')')[0].split(','))
update_cols = ', '.join([f'"{col}" = EXCLUDED."{col}"' for col in query.split('(')[1].split(')')[0].split(',') if col.strip() not in ['id', 'version', 'server_id']])
insert_cols = ', '.join(col.strip() for col in query.split('(')[1].split(')')[0].split(','))
update_cols = ', '.join([f'{col.strip()} = EXCLUDED.{col.strip()}' for col in query.split('(')[1].split(')')[0].split(',') if col.strip() not in ['id', 'version', 'server_id']])
modified_query = f"""
WITH new_version AS (
SELECT COALESCE(MAX(version), 0) + 1 as next_version
FROM {table_name}
WHERE id = (SELECT id FROM {table_name} WHERE {insert_cols.split(',')[0]} = $1 FOR UPDATE)
WHERE id = (SELECT id FROM {table_name} WHERE {insert_cols.split(',')[0].strip()} = $1 FOR UPDATE)
)
INSERT INTO {table_name} ({insert_cols}, version, server_id)
VALUES ({', '.join(f'${i+1}' for i in range(len(args)))}, (SELECT next_version FROM new_version), '{local_ts_id}')

View file

@ -198,20 +198,8 @@ async def store_weather_to_db(date_time: dt_datetime, weather_data: dict):
]
daily_weather_query = '''
INSERT INTO dailyweather (
location, sunrise, sunriseepoch, sunset, sunsetepoch, description,
tempmax, tempmin, uvindex, winddir, windspeed, icon, last_updated,
datetime, datetimeepoch, temp, feelslikemax, feelslikemin, feelslike,
dew, humidity, precip, precipprob, precipcover, preciptype,
snow, snowdepth, windgust, pressure, cloudcover, visibility,
solarradiation, solarenergy, severerisk, moonphase, conditions,
stations, source
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15,
$16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28,
$29, $30, $31, $32, $33, $34, $35, $36, $37, $38
) RETURNING id
'''
INSERT INTO dailyweather (location, sunrise, sunriseepoch, sunset, sunsetepoch, description, tempmax, tempmin, uvindex, winddir, windspeed, icon, last_updated, datetime, datetimeepoch, temp, feelslikemax, feelslikemin, feelslike, dew, humidity, precip, precipprob, precipcover, preciptype, snow, snowdepth, windgust, pressure, cloudcover, visibility, solarradiation, solarenergy, severerisk, moonphase, conditions, stations, source) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37, $38) RETURNING id
'''.replace('\n', ' ').replace(' ', ' ').strip()
daily_weather_result = await API.execute_write_query(daily_weather_query, *daily_weather_params, table_name="dailyweather")