-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmigrations.py
136 lines (119 loc) · 3.68 KB
/
migrations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from lnbits.db import Connection
from lnbits.helpers import urlsafe_short_hash
async def m001_initial(db: Connection):
"""
Initial split payment table.
"""
await db.execute(
"""
CREATE TABLE splitpayments.targets (
wallet TEXT NOT NULL,
source TEXT NOT NULL,
percent INTEGER NOT NULL CHECK (percent >= 0 AND percent <= 100),
alias TEXT,
UNIQUE (source, wallet)
);
"""
)
async def m002_float_percent(db: Connection):
"""
alter percent to be float.
"""
await db.execute("ALTER TABLE splitpayments.targets RENAME TO splitpayments_m001")
await db.execute(
"""
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):
"""
Add id, tag and migrates the existing data.
"""
await db.execute("ALTER TABLE splitpayments.targets RENAME TO splitpayments_m002")
await db.execute(
"""
CREATE TABLE splitpayments.targets (
id TEXT PRIMARY KEY,
wallet TEXT NOT NULL,
source TEXT NOT NULL,
percent REAL NOT NULL CHECK (percent >= 0 AND percent <= 100),
tag TEXT NOT NULL,
alias TEXT,
UNIQUE (source, wallet)
);
"""
)
result = await db.execute("SELECT * FROM splitpayments.splitpayments_m002")
rows = result.mappings().all()
for row in rows:
await db.execute(
"""
INSERT INTO splitpayments.targets (
id,
wallet,
source,
percent,
tag,
alias
)
VALUES (:id, :wallet, :source, :percent, :tag, :alias)
""",
{
"id": urlsafe_short_hash(),
"wallet": row["wallet"],
"source": row["source"],
"percent": row["percent"],
"tag": row["tag"],
"alias": row["alias"],
},
)
await db.execute("DROP TABLE splitpayments.splitpayments_m002")
async def m004_remove_tag(db: Connection):
"""
This removes tag
"""
keys = "id,wallet,source,percent,alias"
new_db = "splitpayments.targets"
old_db = "splitpayments.targets_m003"
await db.execute(f"ALTER TABLE {new_db} RENAME TO targets_m003")
await db.execute(
f"""
CREATE TABLE {new_db} (
id TEXT PRIMARY KEY,
wallet TEXT NOT NULL,
source TEXT NOT NULL,
percent REAL NOT NULL CHECK (percent >= 0 AND percent <= 100),
alias TEXT,
UNIQUE (source, wallet)
);
"""
)
await db.execute(f"INSERT INTO {new_db} ({keys}) SELECT {keys} FROM {old_db}")
await db.execute(f"DROP TABLE {old_db}")