--- /dev/null
+#!/bin/bash
+# Postgresql schema comparer
+# Copyright 2007 Svenne Krap
+# Released under ASL 2.0
+
+if [ $# -ne 2 ]; then
+ echo "DB compare <db1> <db2>"
+ exit
+fi
+DB1=$1
+DB2=$2
+File1=`mktemp`
+File2=`mktemp`
+echo "Compare PostgreSQL database $DB1 and $DB2"
+echo "No lines below this means database structure is equal. Data is not checked."
+pg_dump --schema-only $DB1 > $File1
+pg_dump --schema-only $DB2 > $File2
+diff -u $File1 $File2
+rm $File1
+rm $File2
+
--- /dev/null
+Copyright 2007 Svenne Krap
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
--- /dev/null
+# Copyright 2012 Svenne Krap
+#
+# this script renumbers files in the format nnnn_XXXXXXX.sql ,
+# where nnnn is a series of digits (0-9) and XXXX is any string
+#
+# first parameter is the first number to renumber
+# second paramter is the amount to renumber it (negatives work too)
+# third parameter decides between dry-run (0) and actual doing work (1)
+#
+# License: ASL 2.0
+
+import os
+import sys
+if len(sys.argv) < 4:
+ print "Renumber.py, Svenne Krap 2012"
+ print
+ print "renumber.py <first-renumber> <rename-offset> <do-actual-work>"
+ sys.exit()
+
+renumber_from = int(sys.argv[1])
+renumber_amount = int(sys.argv[2])
+doit = int(sys.argv[3])
+
+
+files = os.listdir('.')
+files.sort()
+if renumber_amount > 0: files.reverse()
+
+for file in files:
+ if not file.endswith(".sql"): continue
+ if file.find("_") == -1: continue
+ numstr = file.split("_")[0]
+ digits = len(numstr)
+ numint = int(numstr)
+ if numint < renumber_from: continue
+ newname = file.replace(numstr + "_", (("%0" + str(digits) + "i") % (numint + renumber_amount)) + "_" )
+ if doit == 1:
+ os.rename(file,newname)
+ else:
+ print file, "=>", newname
--- /dev/null
+#!/bin/bash
+grep "create table" *.sql | cut -d: -f2 | sed 's/create/drop/g' | sed 's/(/ cascade;/g' | psql
--- /dev/null
+#!/usr/bin/python
+# Database script runner
+# Copyright 2007 Svenne Krap
+# License ASL 2.0
+#
+# Usage:
+# - ensure you have a file named DATABASE that contains the name of your target db
+# ie "echo project_test > DATABASE"
+# - remember to exclude it from your SCM
+# ie. "echo DATABASE >> .gitignore" (if using git)
+# - if you want to run it in normal mode you need a file called PRODMODE
+# ie. "touch PRODMODE"
+#
+# Files needs to be called nnnn_XXXXXXX.sql (or use sqledit)
+# i.e. any amount of digits (0-9) followed by one underscore followed by some
+# string followed by ".sql"
+#
+# Run the command as rundb nnnn
+#
+import sys
+import os
+import time
+
+PAGER = os.environ.get('PAGER', 'more')
+argc = len(sys.argv)
+
+try:
+ db = open("DATABASE","r").read().strip()
+except:
+ print "Need database name in file DATABASE"
+ sys.exit(1)
+
+files = os.listdir('.')
+files.sort()
+mode = "unknown"
+if 'DIAGMODE' in files: mode = "diag"
+if 'PRODMODE' in files: mode = "prod"
+if mode=="unknown":
+ print "Unable to determine DIAGMODE or PRODMODE. Please touch appropriate file in run-dir"
+ sys.exit(0);
+
+try:
+ os.chdir("output")
+except:
+ os.mkdir("output")
+ os.chdir("output")
+os.chdir("..")
+
+
+
+rt = time.localtime()
+logfile = "output/run_" + time.strftime("%Y-%m-%d-%H-%M-%S",rt) + ".log"
+
+
+if argc < 2 or argc > 3:
+ print "Syntax: rundb.py <from> [exclude_tags]"
+ print
+ print "dbname = name of database"
+ print "from = integer of first command"
+ print "exclude_tags = comma separated list of strings in filename to exclude"
+ if argc == 1:
+ print "Log is written to " + logfile
+ os.system("psql -d %s -e -L %s" % (db,logfile) )
+ print "Log was written to " + logfile
+ sys.exit(0)
+ sys.exit(1)
+
+def db_run_file(db,f,logfile):
+ os.system("echo \"###RESULT### %s\" >>%s 2>&1" % (f,logfile))
+ os.system("psql --single-transaction -d %s -f %s >>%s 2>&1" % (db,f,logfile) )
+ os.system("echo \"###RUN### %s\" >>%s 2>&1" % (f,logfile))
+ os.system("cat %s >>%s 2>&1" % (f,logfile) )
+
+if argc >= 3:
+ exclude_tags = sys.argv[2].strip().split(",")
+else:
+ exclude_tags = []
+
+if mode=='prod':
+ run_from = int(sys.argv[1])
+ for f in files:
+ good = True
+ if not f.endswith(".sql"): continue
+ pos = f.find("_")
+ if pos == -1: continue
+ first = f[:pos]
+ try:
+ num = int(first)
+ except:
+ num = int(first[:-1])
+ if num < run_from: continue
+ for x in exclude_tags:
+ if not f.find(x) == -1:
+ good = False
+ break
+ if good == False: continue
+ db_run_file(db,f,logfile)
+elif mode=='diag':
+ filename = sys.argv[1]
+ if os.path.exists(filename) and os.path.isfile(filename):
+ db_run_file(db,filename,logfile)
+
+if os.path.exists(logfile):
+ os.system('%s %s' % (PAGER, logfile))
--- /dev/null
+#!/usr/bin/python
+import sys
+import os
+import md5
+import os.path
+
+EDITOR = os.environ.get('EDITOR', 'vim')
+def md5file(filename):
+ try:
+ return md5.new(file(filename,'r').read()).hexdigest()
+ except:
+ return ""
+if len(sys.argv) < 2:
+ print "need command"
+ sys.exit(1)
+
+mode = "unknown"
+if os.path.exists('DIAGMODE') : mode = "diag"
+if os.path.exists('PRODMODE'): mode = "prod"
+if os.path.exists('.hg'):
+ merc = 1
+else :
+ merc = 0
+
+if mode=="unknown":
+ print "Unable to determine DIAGMODE or PRODMODE. Please touch appropriate file in run-dir"
+ sys.exit(0);
+
+cmd = sys.argv[0]
+files = os.listdir('.')
+files.sort()
+lastfile=""
+new = 0
+
+for i in range(1,len(files) + 1):
+ j = -1 * i
+ f = files[j]
+ if f.endswith(".sql"):
+ lastfile = f
+ break;
+
+if sys.argv[1] in ["current","c"]:
+ # no last file!
+ if len(lastfile) == 0:
+ print "No last file"
+ sys.exit(0)
+ editfile =lastfile
+elif sys.argv[1] in ["next","n"]:
+ new = 1
+ if len(sys.argv) == 3:
+ input = sys.argv[2]
+ else:
+ print "I need description as third argument"
+ sys.exit(1)
+
+ if len(input):
+ if len(lastfile):
+ first = lastfile[:lastfile.find("_")]
+ try:
+ number = int(first)
+ except:
+ number = int(first[:-1])
+ number += 1
+ else:
+ number = 1
+ editfile = "%05u_%s.sql" % (number,input)
+else:
+ editfile = sys.argv[1].strip()
+ if not (os.path.exists(editfile) and os.path.isfile(editfile) ):
+ print "no real file"
+ editfile = ""
+
+if editfile.find("_") == -1:
+ print "Malformed filename"
+ editfile = ""
+else:
+ first = editfile[:editfile.find("_")]
+ try:
+ number = int(first)
+ except:
+ try:
+ number = int(first[:-1])
+ except:
+ number = -1
+print number
+if number < 1:
+ editfile = ""
+ print "No number"
+
+if len(editfile) ==0: sys.exit(0)
+
+before_md5 = md5file(editfile)
+exitcode = os.system('%s %s' % (EDITOR, editfile))
+after_md5 = md5file(editfile)
+
+if (after_md5 != "" and before_md5 != after_md5):
+ if mode == 'prod':
+ os.system("rundb %s" % (number,))
+ elif mode == 'diag':
+ os.system("rundb %s" % (editfile,))
+ if merc == 1:
+ if new:
+ os.system("hg add " + editfile)
+ print "Run mercurial ? (Yes [A]uto, Yes [M]anual, No)"
+ c = ""
+ while c not in ['a','m','n']:
+ c = sys.stdin.read(1).lower()
+ if c == 'n':
+ sys.exit()
+ if c == 'a':
+ os.system("hg ci -m `date +auto%Y%m%d%H%M%S`")
+ sys.exit()
+ if c == 'm':
+ os.system("hg ci")
+ sys.exit()
+