-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb_migration_1.py
executable file
·77 lines (65 loc) · 1.57 KB
/
db_migration_1.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
#!/usr/bin/python
import sys
import logging
from config import *
import sqlite3
'''
migrates signatures table
adds: FK loc_id to locations
removes: location_other
'''
print "blahhhhho"
# Connect to db
conn = sqlite3.connect(database)
c = conn.cursor()
# start logging
logger = logging.getLogger('main.db_migration_1')
logger.info("renaming signatures to signatures_bu . . .")
try:
c.execute("ALTER TABLE signatures RENAME to signatures_bu")
except Exception as e:
logger.exception(e)
sys.exit(1)
try:
logger.info("making new signatures table")
c.execute('''
CREATE TABLE signatures
(sig_id INTEGER PRIMARY KEY ASC
,page TEXT
,sig_num INTEGER
,first_name TEXT
,last_initial TEXT
,sig_date DATE
,location_city TEXT
,location_state TEXT
,loc_id INTEGER
,time_added DATETIME
,FOREIGN KEY (loc_id) REFERENCES loc_id) ''')
except Exception as e:
logger.exception(e)
c.execute("ALTER TABLE signatures_bu RENAME to signatures")
logger.info("renaming signatures_bu to signatures")
sys.exit(1)
try:
logger.info("pulling data out of signatures_bu")
c.execute('''
INSERT INTO signatures SELECT
NULL AS sig_id,
s.page,
s.sig_num,
s.first_name,
s.last_initial,
s.sig_date,
s.location_city,
s.location_state,
NULL as loc_id,
s.time_added
FROM signatures_bu AS s''')
conn.commit()
except Exception as e:
logger.exception(e)
c.execute("DROP TABLE signatures")
c.execute("ALTER TABLE signatures_bu RENAME to signatures")
logger.info("renaming signatures_bu to signatures")
sys.exit(1)
c.close()