pgtoolkit/tools/schemadiff.py
changeset 101 2a2d0d5df03b
parent 93 b72591087495
equal deleted inserted replaced
100:d6088dba8fea 101:2a2d0d5df03b
       
     1 from pgtoolkit.toolbase import SrcDstTool
       
     2 from pgtoolkit import pgmanager, pgbrowser, pgdiff, toolbase
       
     3 
       
     4 
       
     5 class SchemaDiffTool(SrcDstTool):
       
     6 
       
     7     """
       
     8     Print differences in database schema.
       
     9 
       
    10     Prints changes from source to destination.
       
    11     SQL patch updates source database schema to destination schema.
       
    12 
       
    13     """
       
    14 
       
    15     def __init__(self):
       
    16         SrcDstTool.__init__(self, name='schemadiff', allow_reverse=True)
       
    17 
       
    18     def specify_args(self):
       
    19         SrcDstTool.specify_args(self)
       
    20         self.parser.add_argument('-s', dest='schema', nargs='*', help='Schema filter')
       
    21         self.parser.add_argument('-t', dest='table', nargs='*', help='Table filter')
       
    22         self.parser.add_argument('-f', dest='function', type=str, help='Function filter (regex)')
       
    23         self.parser.add_argument('--sql', action='store_true', help='Output is SQL script.')
       
    24         self.parser.add_argument('--body', action='store_true', help='Output diff for function bodies.')
       
    25 
       
    26     def main(self):
       
    27         srcbrowser = pgbrowser.PgBrowser(self.pgm.get_conn('src'))
       
    28         dstbrowser = pgbrowser.PgBrowser(self.pgm.get_conn('dst'))
       
    29 
       
    30         pgd = pgdiff.PgDiff(srcbrowser, dstbrowser)
       
    31 
       
    32         try:
       
    33             if self.args.schema:
       
    34                 pgd.filter_schemas(include=self.args.schema)
       
    35             if self.args.table:
       
    36                 pgd.filter_tables(include=self.args.table)
       
    37             if self.args.function:
       
    38                 pgd.filter_functions(self.args.function)
       
    39             if self.args.body:
       
    40                 pgd.function_body_diff = True
       
    41 
       
    42             if self.args.sql:
       
    43                 pgd.print_patch()
       
    44             else:
       
    45                 pgd.print_diff()
       
    46         except pgdiff.PgDiffError as e:
       
    47             print('PgDiff error:', str(e))
       
    48 
       
    49 
       
    50 cls = SchemaDiffTool
       
    51