From 9f3ed7bb01af308b955e5d024bf734dc1a52bb61 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sat, 16 Mar 2024 10:51:37 +0100 Subject: [PATCH] migrate: only if data exists --- cashu/mint/migrations.py | 80 +++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/cashu/mint/migrations.py b/cashu/mint/migrations.py index e82a2a66..2291288c 100644 --- a/cashu/mint/migrations.py +++ b/cashu/mint/migrations.py @@ -561,44 +561,48 @@ async def m016_recompute_Y_with_new_h2c(db: Database): # Perform batch update in a single transaction async with db.connect() as conn: - # For proofs_used - await conn.execute( - "CREATE TABLE IF NOT EXISTS tmp_proofs_used (Y TEXT, secret TEXT)" - ) - values_placeholder = ", ".join( - f"('{y}', '{secret}')" for y, secret in proofs_used_data - ) - await conn.execute( - f"INSERT INTO tmp_proofs_used (Y, secret) VALUES {values_placeholder}", - ) - await conn.execute( - f""" - UPDATE {table_with_schema(db, 'proofs_used')} - SET Y = tmp_proofs_used.Y - FROM tmp_proofs_used - WHERE {table_with_schema(db, 'proofs_used')}.secret = tmp_proofs_used.secret - """ - ) + if len(proofs_used_data): + # For proofs_used + await conn.execute( + "CREATE TABLE IF NOT EXISTS tmp_proofs_used (Y TEXT, secret TEXT)" + ) + values_placeholder = ", ".join( + f"('{y}', '{secret}')" for y, secret in proofs_used_data + ) + await conn.execute( + f"INSERT INTO tmp_proofs_used (Y, secret) VALUES {values_placeholder}", + ) + await conn.execute( + f""" + UPDATE {table_with_schema(db, 'proofs_used')} + SET Y = tmp_proofs_used.Y + FROM tmp_proofs_used + WHERE {table_with_schema(db, 'proofs_used')}.secret = tmp_proofs_used.secret + """ + ) - # For proofs_pending - await conn.execute( - "CREATE TABLE IF NOT EXISTS tmp_proofs_pending (Y TEXT, secret TEXT)" - ) - values_placeholder = ", ".join( - f"('{y}', '{secret}')" for y, secret in proofs_pending_data - ) - await conn.execute( - f"INSERT INTO tmp_proofs_used (Y, secret) VALUES {values_placeholder}", - ) - await conn.execute( - f""" - UPDATE {table_with_schema(db, 'proofs_pending')} - SET Y = tmp_proofs_pending.Y - FROM tmp_proofs_pending - WHERE {table_with_schema(db, 'proofs_pending')}.secret = tmp_proofs_pending.secret - """ - ) + if len(proofs_pending_data): + # For proofs_pending + await conn.execute( + "CREATE TABLE IF NOT EXISTS tmp_proofs_pending (Y TEXT, secret TEXT)" + ) + values_placeholder = ", ".join( + f"('{y}', '{secret}')" for y, secret in proofs_pending_data + ) + await conn.execute( + f"INSERT INTO tmp_proofs_used (Y, secret) VALUES {values_placeholder}", + ) + await conn.execute( + f""" + UPDATE {table_with_schema(db, 'proofs_pending')} + SET Y = tmp_proofs_pending.Y + FROM tmp_proofs_pending + WHERE {table_with_schema(db, 'proofs_pending')}.secret = tmp_proofs_pending.secret + """ + ) async with db.connect() as conn: - await conn.execute("DROP TABLE tmp_proofs_used") - await conn.execute("DROP TABLE tmp_proofs_pending") + if len(proofs_used_data): + await conn.execute("DROP TABLE tmp_proofs_used") + if len(proofs_pending_data): + await conn.execute("DROP TABLE tmp_proofs_pending")