-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_migrate.py
28 lines (19 loc) · 963 Bytes
/
db_migrate.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
from views import db
from _config import DATABASE_PATH
import sqlite3
from datetime import datetime
with sqlite3.connect(DATABASE_PATH) as connection:
# get a cursor object used to execute SQL commands
c = connection.cursor()
# temporarily change the name of tasks table
c.execute("""ALTER TABLE tasks RENAME to old_tasks""")
# recreate a new tasks table with updated schema
db.create_all()
# retrieve data from old_tasks table
c.execute("""SELECT name, due_date, priority, status FROM old_tasks ORDER BY task_id ASC""")
# save all rows as a list of tuples; set posted_date to now and use_id to 1
data = [(row[0], row[1], row[2], row[3], datetime.now(), 1) for row in c.fetchall()]
# insert data to tasks table
c.executemany("""INSERT INTO tasks (name, due_date, priority, status, posted_date, user_id) VALUES (?, ?, ?, ?, ?, ?)""", data)
# delete old_tasks table
c.execute("DROP TABLE old_tasks")