Skip to content

Commit

Permalink
fix: migration m002
Browse files Browse the repository at this point in the history
  • Loading branch information
dni committed Nov 28, 2024
1 parent 5042d40 commit 980b9ea
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,44 @@ async def m001_initial(db: Connection):

async def m002_float_percent(db: Connection):
"""
Add float percent and migrates the existing data.
alter percent to be float.
"""
await db.execute("ALTER TABLE splitpayments.targets RENAME TO splitpayments_m001")

await db.execute(
"""
ALTER TABLE splitpayments.targets
ADD COLUMN percent REAL NOT NULL CHECK (percent >= 0 AND percent <= 100)
"""
CREATE TABLE splitpayments.targets (
wallet TEXT NOT NULL,
source TEXT NOT NULL,
percent REAL NOT NULL CHECK (percent >= 0 AND percent <= 100),
alias TEXT,
UNIQUE (source, wallet)
);
"""
)
result = await db.execute("SELECT * FROM splitpayments.splitpayments_m001")
rows = result.mappings().all()
for row in rows:
await db.execute(
"""
INSERT INTO splitpayments.targets (
wallet,
source,
percent,
alias
)
VALUES (:wallet, :source, :percent, :alias)
""",
{
"wallet": row["wallet"],
"source": row["source"],
"percent": row["percent"],
"alias": row["alias"],
},
)

await db.execute("DROP TABLE splitpayments.splitpayments_m001")


async def m003_add_id_and_tag(db: Connection):
Expand Down

0 comments on commit 980b9ea

Please sign in to comment.