diff.py
changeset 2 efee419b7a2d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/diff.py	Mon Jul 18 17:39:37 2011 +0200
@@ -0,0 +1,50 @@
+#!/usr/bin/env python3.2
+
+import sys
+import argparse
+
+from pgtools import pgmanager, pgbrowser, pgdiff
+from common import config
+
+parser = argparse.ArgumentParser(description='Database schema diff.')
+parser.add_argument('source', metavar='source', type=str, help='Source database')
+parser.add_argument('destination', metavar='destination', type=str, help='Destination database')
+parser.add_argument('-s', dest='schema', nargs='*', help='Schema filter')
+parser.add_argument('-t', dest='table', nargs='*', help='Table filter')
+args = parser.parse_args()
+
+config = config.ConfigParser()
+config.add_argument('databases', type=dict)
+config.add_argument('meta_db')
+config.add_argument('meta_query')
+config.load('pgtoolkit.conf')
+
+pgm = pgmanager.get_instance()
+
+pgm.create_conn(name='meta', dns=config.meta_db)
+
+def build_conn(name, targetname):
+    with pgm.cursor('meta') as curs:
+        curs.execute(config.meta_query, [targetname])
+        row = curs.fetchone_dict()
+        curs.connection.commit()
+        
+        pgm.create_conn(name=name, **row)
+
+build_conn('src', args.source)
+build_conn('dst', args.destination)
+
+pgm.close_conn('meta')
+
+srcbrowser = pgbrowser.PgBrowser(pgm.get_conn('src'))
+dstbrowser = pgbrowser.PgBrowser(pgm.get_conn('dst'))
+
+pgd = pgdiff.PgDiff(srcbrowser, dstbrowser)
+
+if args.schema:
+    pgd.filter_schemas(include=args.schema)
+
+if args.table:
+    pgd.filter_tables(include=args.table)
+
+pgd.print_diff()