diff.py
changeset 2 efee419b7a2d
equal deleted inserted replaced
0:eaae9539e910 2:efee419b7a2d
       
     1 #!/usr/bin/env python3.2
       
     2 
       
     3 import sys
       
     4 import argparse
       
     5 
       
     6 from pgtools import pgmanager, pgbrowser, pgdiff
       
     7 from common import config
       
     8 
       
     9 parser = argparse.ArgumentParser(description='Database schema diff.')
       
    10 parser.add_argument('source', metavar='source', type=str, help='Source database')
       
    11 parser.add_argument('destination', metavar='destination', type=str, help='Destination database')
       
    12 parser.add_argument('-s', dest='schema', nargs='*', help='Schema filter')
       
    13 parser.add_argument('-t', dest='table', nargs='*', help='Table filter')
       
    14 args = parser.parse_args()
       
    15 
       
    16 config = config.ConfigParser()
       
    17 config.add_argument('databases', type=dict)
       
    18 config.add_argument('meta_db')
       
    19 config.add_argument('meta_query')
       
    20 config.load('pgtoolkit.conf')
       
    21 
       
    22 pgm = pgmanager.get_instance()
       
    23 
       
    24 pgm.create_conn(name='meta', dns=config.meta_db)
       
    25 
       
    26 def build_conn(name, targetname):
       
    27     with pgm.cursor('meta') as curs:
       
    28         curs.execute(config.meta_query, [targetname])
       
    29         row = curs.fetchone_dict()
       
    30         curs.connection.commit()
       
    31         
       
    32         pgm.create_conn(name=name, **row)
       
    33 
       
    34 build_conn('src', args.source)
       
    35 build_conn('dst', args.destination)
       
    36 
       
    37 pgm.close_conn('meta')
       
    38 
       
    39 srcbrowser = pgbrowser.PgBrowser(pgm.get_conn('src'))
       
    40 dstbrowser = pgbrowser.PgBrowser(pgm.get_conn('dst'))
       
    41 
       
    42 pgd = pgdiff.PgDiff(srcbrowser, dstbrowser)
       
    43 
       
    44 if args.schema:
       
    45     pgd.filter_schemas(include=args.schema)
       
    46 
       
    47 if args.table:
       
    48     pgd.filter_tables(include=args.table)
       
    49 
       
    50 pgd.print_diff()