-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_orphaned_table.py
68 lines (57 loc) · 1.99 KB
/
handle_orphaned_table.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
"""
Functions for fixing orphaned tables,
i.e. tables that no longer exist upstream as we last knew them.
"""
import sys
import util
def autoselect_table():
"""
Find a random orphaned table
"""
util.cursor.execute("""
SELECT * FROM tables
WHERE orphaned ORDER BY RANDOM()
LIMIT 1
""")
row = util.cursor.fetchone()
return row
def handle_orphaned_table():
"""
Select an orphaned table, and walk the user through options for fixing.
"""
orphaned_row = autoselect_table()
if not orphaned_row:
print("No orphaned tables found")
sys.exit(0)
print("Orphaned table: %s" % orphaned_row)
print("What would you like to do?")
while True:
user_response = input("(r=rename, d=delete, t=transfer)")
if user_response in "rdt":
break
if user_response == "r":
new_table_name = input("What's the new table name? ")
# Update record for table
util.cursor.execute("""
UPDATE tables SET table_name = %s
WHERE table_name = %s AND table_schema = %s
""", (new_table_name, orphaned_row["table_name"], orphaned_row["table_schema"]))
# Update records for columns
util.cursor.execute("""
UPDATE columns SET table_name = %s
WHERE table_name = %s AND table_schema = %s
""", (new_table_name, orphaned_row["table_name"], orphaned_row["table_schema"]))
util.db_conn.commit()
util.db_conn.commit()
elif user_response == "d":
# Delete record for table
util.cursor.execute("""
DELETE FROM tables
WHERE table_name = %s AND table_schema = %s
""", (orphaned_row["table_name"], orphaned_row["table_schema"]))
# Delete records for columns
util.cursor.execute("""
DELETE FROM columns
WHERE table_name = %s AND table_schema = %s
""", (orphaned_row["table_name"], orphaned_row["table_schema"]))
util.db_conn.commit()