pgtoolkit/tools/schemadiff.py
author Radek Brich <radek.brich@devl.cz>
Mon, 26 May 2014 18:18:21 +0200
changeset 103 24e94a3da209
parent 101 2a2d0d5df03b
permissions -rw-r--r--
Update bigtables tool: Sort by size with indexes, not just data.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
     1
from pgtoolkit.toolbase import SrcDstTool
9
2fcc8ef0b97d Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
     2
from pgtoolkit import pgmanager, pgbrowser, pgdiff, toolbase
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
     5
class SchemaDiffTool(SrcDstTool):
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
     6
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
     7
    """
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
     8
    Print differences in database schema.
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
     9
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    10
    Prints changes from source to destination.
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    11
    SQL patch updates source database schema to destination schema.
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    12
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    13
    """
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    14
5
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    15
    def __init__(self):
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    16
        SrcDstTool.__init__(self, name='schemadiff', allow_reverse=True)
56
94e091c23ebb Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    17
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    18
    def specify_args(self):
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    19
        SrcDstTool.specify_args(self)
5
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    20
        self.parser.add_argument('-s', dest='schema', nargs='*', help='Schema filter')
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    21
        self.parser.add_argument('-t', dest='table', nargs='*', help='Table filter')
63
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
    22
        self.parser.add_argument('-f', dest='function', type=str, help='Function filter (regex)')
16
cb7e13711a99 MyManager - create_conn() - allow same parameters as for PgManager.
Radek Brich <brich.radek@ifortuna.cz>
parents: 9
diff changeset
    23
        self.parser.add_argument('--sql', action='store_true', help='Output is SQL script.')
63
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
    24
        self.parser.add_argument('--body', action='store_true', help='Output diff for function bodies.')
56
94e091c23ebb Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    25
5
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    26
    def main(self):
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    27
        srcbrowser = pgbrowser.PgBrowser(self.pgm.get_conn('src'))
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    28
        dstbrowser = pgbrowser.PgBrowser(self.pgm.get_conn('dst'))
56
94e091c23ebb Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    29
5
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    30
        pgd = pgdiff.PgDiff(srcbrowser, dstbrowser)
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
61
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 56
diff changeset
    32
        try:
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 56
diff changeset
    33
            if self.args.schema:
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 56
diff changeset
    34
                pgd.filter_schemas(include=self.args.schema)
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 56
diff changeset
    35
            if self.args.table:
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 56
diff changeset
    36
                pgd.filter_tables(include=self.args.table)
63
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
    37
            if self.args.function:
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
    38
                pgd.filter_functions(self.args.function)
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
    39
            if self.args.body:
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
    40
                pgd.function_body_diff = True
56
94e091c23ebb Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    41
61
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 56
diff changeset
    42
            if self.args.sql:
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 56
diff changeset
    43
                pgd.print_patch()
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 56
diff changeset
    44
            else:
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 56
diff changeset
    45
                pgd.print_diff()
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 56
diff changeset
    46
        except pgdiff.PgDiffError as e:
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 56
diff changeset
    47
            print('PgDiff error:', str(e))
5
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    48
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
101
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    50
cls = SchemaDiffTool
2a2d0d5df03b Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents: 93
diff changeset
    51