-
Notifications
You must be signed in to change notification settings - Fork 1
/
database_cleaner.py
executable file
·45 lines (39 loc) · 1.17 KB
/
database_cleaner.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
import dj_database_url
import psycopg2
import sys
D = dj_database_url.config()
#set databae variables
print """export PGUSER=%s
export PGPASSWORD=%s
export PGHOST=%s
export PGDATABASE=%s
"""%(D['USER'],
D['PASSWORD'],
D['HOST'],
D['NAME'])
# Drop all tables from a given database
if False:
try:
conn = psycopg2.connect("""dbname='%s'
user='%s'
password='%s'
host='%s'"""%(D['NAME'],
D['USER'],
D['PASSWORD'],
D['HOST']
)
)
conn.set_isolation_level(0)
except:
print "Unable to connect to the database."
cur = conn.cursor()
try:
cur.execute("SELECT table_schema,table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_schema,table_name")
rows = cur.fetchall()
for row in rows:
print "dropping table: ", row[1]
cur.execute("drop table " + row[1] + " cascade")
cur.close()
conn.close()
except:
print "Error: ", sys.exc_info()[1]