-
Notifications
You must be signed in to change notification settings - Fork 2
/
dbmigration.py
51 lines (36 loc) · 1.03 KB
/
dbmigration.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
#!/usr/bin/python
import config
import sys
import psycopg2
try:
conn = psycopg2.connect(database=config.db_name,
user=config.db_user,
password=config.db_password,
host='postgresql',
)
dumpfile = open("dumpfile.sql", "r")
conn.set_session(autocommit=True)
cur = conn.cursor()
count = 0
while True:
count += 1
line = dumpfile.readline()
if not line:
break
try:
cur.execute(line)
except Exception as e:
print(f"Failed to insert line {count}: {line}: {e}",
file=sys.stderr)
conn.rollback()
break
# cur.execute(open("dumpfile.sql", "r").read())
conn.commit()
print("Successfully executed migration")
except Exception as e:
conn.rollback()
print(f"Something went wrong: {e}", file=sys.stderr)
sys.exit("Database failure")
finally:
conn.close()
dumpfile.close()