2 # Database script runner
3 # Copyright 2007 Svenne Krap
7 # - ensure you have a file named DATABASE that contains the name of your target db
8 # ie "echo project_test > DATABASE"
9 # - remember to exclude it from your SCM
10 # ie. "echo DATABASE >> .gitignore" (if using git)
11 # - if you want to run it in normal mode you need a file called PRODMODE
12 # ie. "touch PRODMODE"
14 # Files needs to be called nnnn_XXXXXXX.sql (or use sqledit)
15 # i.e. any amount of digits (0-9) followed by one underscore followed by some
16 # string followed by ".sql"
18 # Run the command as rundb nnnn
24 PAGER = os.environ.get('PAGER', 'more')
28 db = open("DATABASE","r").read().strip()
30 print "Need database name in file DATABASE"
33 files = os.listdir('.')
36 if 'DIAGMODE' in files: mode = "diag"
37 if 'PRODMODE' in files: mode = "prod"
39 print "Unable to determine DIAGMODE or PRODMODE. Please touch appropriate file in run-dir"
52 logfile = "output/run_" + time.strftime("%Y-%m-%d-%H-%M-%S",rt) + ".log"
55 if argc < 2 or argc > 3:
56 print "Syntax: rundb.py <from> [exclude_tags]"
58 print "dbname = name of database"
59 print "from = integer of first command"
60 print "exclude_tags = comma separated list of strings in filename to exclude"
62 print "Log is written to " + logfile
63 os.system("psql -d %s -e -L %s" % (db,logfile) )
64 print "Log was written to " + logfile
68 def db_run_file(db,f,logfile):
69 os.system("echo \"###RESULT### %s\" >>%s 2>&1" % (f,logfile))
70 os.system("psql --single-transaction -d %s -f %s >>%s 2>&1" % (db,f,logfile) )
71 os.system("echo \"###RUN### %s\" >>%s 2>&1" % (f,logfile))
72 os.system("cat %s >>%s 2>&1" % (f,logfile) )
75 exclude_tags = sys.argv[2].strip().split(",")
80 run_from = int(sys.argv[1])
83 if not f.endswith(".sql"): continue
85 if pos == -1: continue
91 if num < run_from: continue
92 for x in exclude_tags:
93 if not f.find(x) == -1:
96 if good == False: continue
97 db_run_file(db,f,logfile)
99 filename = sys.argv[1]
100 if os.path.exists(filename) and os.path.isfile(filename):
101 db_run_file(db,filename,logfile)
103 if os.path.exists(logfile):
104 os.system('%s %s' % (PAGER, logfile))