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