Skip to content

Commit

Permalink
drop balance views during migration
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Feb 11, 2024
1 parent ac8e21a commit 156c0e9
Showing 1 changed file with 44 additions and 39 deletions.
83 changes: 44 additions & 39 deletions cashu/mint/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,46 @@ async def m001_initial(db: Database):
""")


async def m002_add_balance_views(db: Database):
async with db.connect() as conn:
await conn.execute(f"""
CREATE VIEW {table_with_schema(db, 'balance_issued')} AS
SELECT COALESCE(SUM(s), 0) AS balance FROM (
SELECT SUM(amount) AS s
FROM {table_with_schema(db, 'promises')}
WHERE amount > 0
) AS balance_issued;
""")
async def drop_balance_views(db: Database, conn: Connection):
await conn.execute(f"DROP VIEW IF EXISTS {table_with_schema(db, 'balance')}")
await conn.execute(f"DROP VIEW IF EXISTS {table_with_schema(db, 'balance_issued')}")
await conn.execute(
f"DROP VIEW IF EXISTS {table_with_schema(db, 'balance_redeemed')}"
)

await conn.execute(f"""
CREATE VIEW {table_with_schema(db, 'balance_redeemed')} AS
SELECT COALESCE(SUM(s), 0) AS balance FROM (
SELECT SUM(amount) AS s
FROM {table_with_schema(db, 'proofs_used')}
WHERE amount > 0
) AS balance_redeemed;
""")

await conn.execute(f"""
CREATE VIEW {table_with_schema(db, 'balance')} AS
SELECT s_issued - s_used FROM (
SELECT bi.balance AS s_issued, bu.balance AS s_used
FROM {table_with_schema(db, 'balance_issued')} bi
CROSS JOIN {table_with_schema(db, 'balance_redeemed')} bu
) AS balance;
""")
async def create_balance_views(db: Database, conn: Connection):
await conn.execute(f"""
CREATE VIEW {table_with_schema(db, 'balance_issued')} AS
SELECT COALESCE(SUM(s), 0) AS balance FROM (
SELECT SUM(amount) AS s
FROM {table_with_schema(db, 'promises')}
WHERE amount > 0
) AS balance_issued;
""")

await conn.execute(f"""
CREATE VIEW {table_with_schema(db, 'balance_redeemed')} AS
SELECT COALESCE(SUM(s), 0) AS balance FROM (
SELECT SUM(amount) AS s
FROM {table_with_schema(db, 'proofs_used')}
WHERE amount > 0
) AS balance_redeemed;
""")

await conn.execute(f"""
CREATE VIEW {table_with_schema(db, 'balance')} AS
SELECT s_issued - s_used FROM (
SELECT bi.balance AS s_issued, bu.balance AS s_used
FROM {table_with_schema(db, 'balance_issued')} bi
CROSS JOIN {table_with_schema(db, 'balance_redeemed')} bu
) AS balance;
""")


async def m002_add_balance_views(db: Database):
async with db.connect() as conn:
await create_balance_views(db, conn)


async def m003_mint_keysets(db: Database):
Expand Down Expand Up @@ -184,15 +196,6 @@ async def m008_promises_dleq(db: Database):
async def m009_add_out_to_invoices(db: Database):
# column in invoices for marking whether the invoice is incoming (out=False) or outgoing (out=True)
async with db.connect() as conn:
# we have to drop the balance views first and recreate them later
await conn.execute(f"DROP VIEW IF EXISTS {table_with_schema(db, 'balance')}")
await conn.execute(
f"DROP VIEW IF EXISTS {table_with_schema(db, 'balance_issued')}"
)
await conn.execute(
f"DROP VIEW IF EXISTS {table_with_schema(db, 'balance_redeemed')}"
)

# rename column pr to bolt11
await conn.execute(
f"ALTER TABLE {table_with_schema(db, 'invoices')} RENAME COLUMN pr TO"
Expand All @@ -203,10 +206,6 @@ async def m009_add_out_to_invoices(db: Database):
f"ALTER TABLE {table_with_schema(db, 'invoices')} RENAME COLUMN hash TO id"
)

# recreate balance views
await m002_add_balance_views(db)

async with db.connect() as conn:
await conn.execute(
f"ALTER TABLE {table_with_schema(db, 'invoices')} ADD COLUMN out BOOL"
)
Expand Down Expand Up @@ -409,6 +408,9 @@ async def m014_proofs_add_Y_column(db: Database):
)
proofs_pending = [Proof(**r) for r in rows]
async with db.connect() as conn:
# we have to drop the balance views first and recreate them later
await drop_balance_views(db, conn)

await conn.execute(
f"ALTER TABLE {table_with_schema(db, 'proofs_used')} ADD COLUMN Y TEXT"
)
Expand Down Expand Up @@ -492,3 +494,6 @@ async def m014_proofs_add_Y_column(db: Database):
)

await conn.execute(f"DROP TABLE {table_with_schema(db, 'proofs_pending_old')}")

# recreate the balance views
await create_balance_views(db, conn)

0 comments on commit 156c0e9

Please sign in to comment.