Skip to content

Commit

Permalink
migrate: only if data exists
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Mar 16, 2024
1 parent 8823ee4 commit 9f3ed7b
Showing 1 changed file with 42 additions and 38 deletions.
80 changes: 42 additions & 38 deletions cashu/mint/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 9f3ed7b

Please sign in to comment.