#!/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()