-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrate_db.py
40 lines (29 loc) · 1013 Bytes
/
migrate_db.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
# This script updates the database to the latest version.
import os
from app import app
from db import get_db, init_db
def v1(db):
"""Update the database from v0 to v1"""
db.execute("ALTER TABLE member RENAME COLUMN receive_email TO subscribed")
return 1
def v2(db):
"""Update the database from v1 to v2"""
db.execute("DROP TABLE token")
return 2
# Dict of functions that migrate the database.
# The functions return the resulting version number, allowing us to reach the
# latest version by chaining upgrades together.
migrations = {
0: v1,
1: v2,
}
if __name__ == "__main__":
with app.app_context():
if not os.path.isfile(app.config["DATABASE_PATH"]):
init_db()
db = get_db()
version = db.execute("SELECT version FROM version").fetchone()[0]
while version != app.config["DATABASE_VERSION"]:
version = migrations[version](db)
db.execute("UPDATE version SET version=?", (version,))
db.commit()