drop_table.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 import sys, getopt, psycopg2
4 
5 Usage = """
6 python drop_table.py [options] <database name> <table_name>
7 options:
8  -h <host>
9  -p <port>
10  -U <user>
11  -w <password>
12  -d <detector>
13 
14 """
15 
16 host = None
17 port = None
18 user = None
19 password = None
20 detector = None
21 
22 dbcon = []
23 
24 opts, args = getopt.getopt(sys.argv[1:], 'h:U:w:p:d:')
25 
26 if len(args) < 2 or args[0] == 'help':
27  print Usage
28  sys.exit(0)
29 
30 for opt, val in opts:
31  if opt == '-h': dbcon.append("host=%s" % (val,))
32  elif opt == '-p': dbcon.append("port=%s" % (int(val),))
33  elif opt == '-U': dbcon.append("user=%s" % (val,))
34  elif opt == '-w': dbcon.append("password=%s" % (val,))
35  elif opt == '-d': detector=val
36 
37 dbcon.append("dbname=%s" % (args[0],))
38 tname = args[1]
39 
40 if detector == None:
41  print Usage
42  sys.exit(0)
43 
44 try:
45 # conn = psycopg2.connect(dbcon)
46  conn = psycopg2.connect("dbname=nova_dev host=ifdbdev.fnal.gov port=5433")
47 except:
48  print "I am unable to connect to the database"
49  exit(0)
50 
51 cur = conn.cursor()
52 
53 cur.execute("set role nova_admin")
54 
55 SQL = "drop table %s.%s_tag_snapshot; drop table %s.%s_tag cascade; drop table %s.%s_snapshot_data; drop table %s.%s_update; drop table %s.%s_snapshot cascade;" % (detector,tname,detector,tname,detector,tname,detector,tname,detector,tname)
56 
57 try:
58  cur.execute(SQL)
59 except psycopg2.ProgrammingError:
60  print "Validity tables for %s.%s not found" % (detector,tname)
61  exit(0)
62 
63 SQL = "commit"
64 
65 cur.execute(SQL)
66 
67 print "Validity tables for %s.%s dropped" % (detector,tname)
68 
exit(0)