Created
April 8, 2016 18:54
-
-
Save rbellamy/e61bbe89abe97afb0fd6150190856f4f to your computer and use it in GitHub Desktop.
Split schema-only PostgreSQL dumps for source control
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
import os, re, sys | |
# Split pg_dump files to schema/type file hierarchy | |
# Use with files produced by pg_dump -s | |
#TODO: Identify dropped objects and delete from tree | |
# Detect object header lines | |
re_obj = re.compile(r'-- Name: ([-\w\s\.]+)(?:\([-\w\s\[\],]*\))?; Type: ([-\w\s]+); Schema: ([-\w]+); Owner: ([-\w]*)(?:; Tablespace: )?([-\w]*)\n', flags=re.IGNORECASE) | |
if len(sys.argv) < 2: | |
print 'Usage: pg_schema_split infile [outdir]' | |
sys.exit() | |
infile = sys.argv[1] | |
outdir = os.path.split(sys.argv[0][0]) | |
if len(sys.argv) > 2 : outdir = sys.argv[2] | |
name, type, schema, owner, tablespace = ['']*5 | |
sql = '' | |
for line in open(infile,'r').readlines(): | |
obj = re_obj.match(line) | |
if obj: | |
ot = re_obj.search(line).group(2) | |
if ot not in ('COMMENT', 'SEQUENCE OWNED BY'): # Keep these in parent script | |
if schema != 'pg_catalog' and name > '': | |
if schema == '-': schema = 'public' | |
type = type.replace(' ','_').lower() | |
#print 'Name: %s; Type: %s; Schema: %s; Owner: %s; Tablespace: %s' % (name, type, schema, owner, tablespace or '(default)') | |
print 'Schema: %s; Type: %s; Name: %s' % (schema, type, name) | |
sqlpath = os.path.join(outdir, schema, type) | |
if not os.path.exists(sqlpath): | |
print '*** mkdir %s' % sqlpath | |
os.makedirs(sqlpath) | |
sqlf = os.path.join(sqlpath, '%s.sql' % name) | |
sql = sql.replace('\n\n','\n') # Cleanup line spacing | |
open(sqlf,'w').write(sql.strip()) | |
name, type, schema, owner, tablespace = re_obj.search(line).groups() | |
sql = '' | |
elif line.strip() != '--': # Skip empty comment lines | |
sql += line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment