-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathremove_test_dbs.py
44 lines (32 loc) · 902 Bytes
/
remove_test_dbs.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
#!/usr/bin/env python
"""
Remove MySQL databases whose name is more than 10 characters and startswith 'test_'
"""
import sys
import pymysql
pymysql.install_as_MySQLdb()
def show_databases(cur, conn) -> list[str]:
sql = "show databases;"
cur.execute(sql)
conn.commit()
res = cur.fetchall()
return [i[0] for i in res]
def run(cur, conn):
dbs = show_databases(cur, conn)
print(f"{len(dbs) = }")
todo = [i for i in dbs if i.startswith("test_") if len(i) > 10]
if "--show" not in sys.argv:
for i in todo:
command = f"DROP DATABASE {i};"
cur.execute(command)
conn.commit()
print(f"{show_databases(cur, conn) = }")
def main():
import MySQLdb
conn = MySQLdb.connect(user="root", passwd="123456")
cur = conn.cursor()
run(cur, conn)
cur.close()
conn.close()
if __name__ == "__main__":
main()