Commit e780ad68 authored by Your Name's avatar Your Name

Use UPSERT for consolidation settings to handle missing records

- Changed UPDATE to INSERT...ON DUPLICATE KEY UPDATE (MySQL)
- Changed UPDATE to INSERT...ON CONFLICT DO UPDATE (SQLite)
- Ensures records are created if they don't exist yet
- Fixes issue where UPDATE fails silently on missing records
- Handles both database types correctly
parent 29d64814
...@@ -6565,17 +6565,26 @@ async def update_payment_consolidation_config(request: Request): ...@@ -6565,17 +6565,26 @@ async def update_payment_consolidation_config(request: Request):
for key, crypto_type in crypto_map.items(): for key, crypto_type in crypto_map.items():
if key in body: if key in body:
threshold = float(body[key]) threshold = float(body[key])
cursor.execute(f"""
UPDATE crypto_consolidation_settings # Use UPSERT to handle missing records
SET threshold_amount = {placeholder} if db.db_type == 'sqlite':
WHERE crypto_type = {placeholder} cursor.execute(f"""
""", (threshold, crypto_type)) INSERT INTO crypto_consolidation_settings (crypto_type, threshold_amount, admin_address, is_enabled)
VALUES (?, ?, '', 0)
ON CONFLICT(crypto_type) DO UPDATE SET threshold_amount = ?
""", (crypto_type, threshold, threshold))
else: # MySQL
cursor.execute(f"""
INSERT INTO crypto_consolidation_settings (crypto_type, threshold_amount, admin_address, is_enabled)
VALUES (%s, %s, '', 0)
ON DUPLICATE KEY UPDATE threshold_amount = %s
""", (crypto_type, threshold, threshold))
rows_affected = cursor.rowcount rows_affected = cursor.rowcount
logger.info(f"Updated {crypto_type} threshold to {threshold}, rows affected: {rows_affected}") logger.info(f"Upserted {crypto_type} threshold to {threshold}, rows affected: {rows_affected}")
updated_count += rows_affected updated_count += rows_affected
if updated_count == 0: logger.info(f"Total rows affected: {updated_count}")
logger.warning("No rows were updated - records may not exist in database")
conn.commit() conn.commit()
logger.info("Consolidation settings committed to database") logger.info("Consolidation settings committed to database")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment