pgtoolkit/pgdiff.py
author Radek Brich <radek.brich@devl.cz>
Tue, 24 Sep 2013 13:34:17 +0200
changeset 88 b8b2d28a7f35
parent 87 3ef717ee9253
child 94 a10f553e6f6a
permissions -rw-r--r--
Update analyzeall tool: Allow combination of vacuum and reindex.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
#
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
# PgDiff - capture differences of database metadata
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
#
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
# Depends on PgBrowser
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
#
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
# Copyright (c) 2011  Radek Brich <radek.brich@devl.cz>
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
#
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
# Permission is hereby granted, free of charge, to any person obtaining a copy
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
# of this software and associated documentation files (the "Software"), to deal
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
# in the Software without restriction, including without limitation the rights
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
# copies of the Software, and to permit persons to whom the Software is
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
# furnished to do so, subject to the following conditions:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
#
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
# The above copyright notice and this permission notice shall be included in
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
# all copies or substantial portions of the Software.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
#
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
# THE SOFTWARE.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
83
515fadd3d286 Add dependency on pycolib. Move common modules to pycolib. Add example table schema for meta DB.
Radek Brich <radek.brich@devl.cz>
parents: 64
diff changeset
    28
from pycolib.ansicolor import *
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
    29
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
    30
import re
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
    31
import difflib
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
61
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
    34
class PgDiffError(Exception):
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
    35
    pass
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
    36
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
    37
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
class DiffBase:
6
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    39
    COLORS = {
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    40
        '+' : BOLD | GREEN,
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    41
        '-' : BOLD | RED,
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    42
        '*' : BOLD | YELLOW,
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    43
    }
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
    44
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    45
    COMMANDS = {
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    46
        '+' : 'CREATE',
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    47
        '-' : 'DROP',
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    48
        '*' : 'ALTER',
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    49
    }
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
    50
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
    def __init__(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
        self.changes = None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
    def format(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
        out = ['  ' * self.level]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
6
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    57
        out.append(highlight(1, self.COLORS[self.change]))
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
        out.append(self.change)
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
    59
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
        out += [' ', self.type, ' ', self.name, highlight(0)]
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
    61
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
        if self.changes:
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    63
            out += [highlight(1, WHITE), ' (', self._formatchanges(), ')', highlight(0)]
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
        return ''.join(out)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    67
    def _formatnotnull(self, notnull):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
        if notnull:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
            return 'NOT NULL'
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
        else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
            return None
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
    72
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    73
    def _formatchanges(self):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
        res = []
60
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    75
        for type, a, b in self.changes:
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
            if type == 'notnull':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
                type = ''
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    78
                a = self._formatnotnull(a)
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    79
                b = self._formatnotnull(b)
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
    80
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
            if a and b:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
                s = ''.join(['Changed ', type, ' from ',
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
                    highlight(1,15), a, highlight(0), ' to ',
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
                    highlight(1,15), b, highlight(0), '.'])
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
            elif a and not b:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
                l = ['Removed ']
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    87
                if type:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
                    l += [type, ' ']
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
                l += [highlight(1,15), a, highlight(0), '.']
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
                s = ''.join(l)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
            elif b and not a:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
                l = ['Added ']
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
                if type:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
                    l += [type, ' ']
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
                l += [highlight(1,15), b, highlight(0), '.']
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
                s = ''.join(l)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
            res.append(s)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
        return ' '.join(res)
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
    99
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   100
    def format_patch(self):
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   101
        if self.change == '*' and self.type in ('schema', 'table'):
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   102
            return None
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   103
        return ['%s %s %s;' % (self.COMMANDS[self.change], self.type.upper(), self.name)]
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   106
class DiffSchema(DiffBase):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   107
    def __init__(self, change, schema):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   108
        DiffBase.__init__(self)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   109
        self.level = 0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   110
        self.type = 'schema'
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   111
        self.change = change
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   112
        self.schema = schema
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   113
        self.name = schema
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   114
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   115
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   116
class DiffTable(DiffBase):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   117
    def __init__(self, change, schema, table):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   118
        DiffBase.__init__(self)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   119
        self.level = 1
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   120
        self.type = 'table'
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   121
        self.change = change
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   122
        self.schema = schema
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   123
        self.table = table
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   124
        self.name = table
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   125
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   126
59
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   127
class DiffArgument(DiffBase):
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   128
    def __init__(self, change, schema, function, argument):
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   129
        DiffBase.__init__(self)
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   130
        self.level = 2
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   131
        self.type = 'argument'
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   132
        self.change = change
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   133
        self.schema = schema
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   134
        self.function = function
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   135
        self.argument = argument
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   136
        self.name = argument
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   137
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   138
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   139
class DiffFunction(DiffBase):
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
   140
    def __init__(self, change, schema, function, show_body_diff=False):
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   141
        DiffBase.__init__(self)
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   142
        self.level = 1
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   143
        self.type = 'function'
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   144
        self.change = change
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   145
        self.schema = schema
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   146
        self.function = function
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   147
        self.name = function
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
   148
        self.show_body_diff = show_body_diff
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   149
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   150
    def _formatchanges(self):
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   151
        res = []
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   152
        for x in self.changes:
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   153
            type, a, b = x
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   154
            if type == 'source':
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
   155
                if self.show_body_diff:
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
   156
                    lines = ['Source differs:\n']
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
   157
                    for line in difflib.unified_diff(a, b, lineterm=''):
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
   158
                        if line[:3] in ('---', '+++'):
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
   159
                            continue
64
687e18e5ca93 Drop colordiff, implement it in PgDiff directly.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
   160
                        color = {' ': WHITE, '-': YELLOW, '+': GREEN, '@': WHITE|BOLD}[line[0]]
687e18e5ca93 Drop colordiff, implement it in PgDiff directly.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
   161
                        lines.append(highlight(1, color) + line + highlight(0) + '\n')
687e18e5ca93 Drop colordiff, implement it in PgDiff directly.
Radek Brich <radek.brich@devl.cz>
parents: 63
diff changeset
   162
                    res.append(''.join(lines))
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
   163
                else:
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
   164
                    res.append('Source differs.')
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   165
            else:
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
   166
                res.append(''.join(['Changed ', type, ' from ',
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   167
                    highlight(1,15), a, highlight(0), ' to ',
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
   168
                    highlight(1,15), b, highlight(0), '.']))
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   169
        return ' '.join(res)
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   170
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   171
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   172
class DiffColumn(DiffBase):
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   173
    ALTER_COMMANDS = {
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   174
        '+' : 'ADD',
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   175
        '-' : 'DROP',
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   176
        '*' : 'ALTER',
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   177
    }
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   178
60
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   179
    def __init__(self, change, schema, table, column, columntype, columndefault, columnnotnull, changes=None):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   180
        DiffBase.__init__(self)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   181
        self.level = 2
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   182
        self.type = 'column'
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   183
        self.change = change
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   184
        self.schema = schema
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   185
        self.table = table
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   186
        self.column = column
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   187
        self.columntype = columntype
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   188
        self.columndefault = columndefault
60
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   189
        self.columnnotnull = columnnotnull
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   190
        self.name = column
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   191
        self.changes = changes
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   192
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   193
    def format_patch(self):
60
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   194
        alter_table = 'ALTER TABLE %s.%s %s COLUMN %s' % (
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   195
            self.schema,
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   196
            self.table,
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   197
            self.ALTER_COMMANDS[self.change],
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   198
            self.name,
60
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   199
        )
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   200
        out = []
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   201
        if self.change == '-':
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   202
            out.append('%s;' % alter_table);
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   203
        if self.change == '+':
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   204
            notnull = ''
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   205
            if self.columnnotnull:
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   206
                notnull = ' NOT NULL'
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   207
            default = ''
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   208
            if self.columndefault:
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   209
                default = ' DEFAULT %s' % self.columndefault
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   210
            out.append('%s %s%s%s;'
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   211
                % (alter_table, self.columntype, notnull, default));
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   212
        if self.change == '*':
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   213
            for type, a, b in self.changes:
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   214
                if type == 'type':
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   215
                    out.append('%s TYPE %s;' % (alter_table, b))
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   216
                if type == 'notnull':
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   217
                    if a and not b:
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   218
                        out.append('%s DROP NOT NULL;' % alter_table)
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   219
                    if not a and b:
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   220
                        out.append('%s SET NOT NULL;' % alter_table)
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   221
                if type == 'default':
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   222
                    if b:
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   223
                        out.append('%s SET DEFAULT %s;' % (alter_table, b))
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   224
                    else:
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   225
                        out.append('%s DROP DEFAULT;' % alter_table)
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   226
        return out
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   227
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   228
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   229
class DiffConstraint(DiffBase):
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   230
    def __init__(self, change, schema, table, constraint, definition, changes=None):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   231
        DiffBase.__init__(self)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   232
        self.level = 2
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   233
        self.type = 'constraint'
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   234
        self.change = change
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   235
        self.schema = schema
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   236
        self.table = table
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   237
        self.constraint = constraint
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   238
        self.name = constraint
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   239
        self.definition = definition
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   240
        self.changes = changes
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   241
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   242
    def format_patch(self):
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   243
        q_alter = 'ALTER TABLE %s.%s' % (self.schema, self.table)
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   244
        q_drop = '%s DROP CONSTRAINT %s;' % (q_alter, self.constraint)
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   245
        q_add = '%s ADD CONSTRAINT %s %s;' % (q_alter, self.constraint, self.definition)
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   246
        if self.change == '*':
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   247
            out = [q_drop, q_add]
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   248
        if self.change == '+':
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   249
            out = [q_add]
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   250
        if self.change == '-':
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   251
            out = [q_drop]
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   252
        return out
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   253
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   254
87
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   255
class DiffIndex(DiffBase):
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   256
    def __init__(self, change, schema, table, index, definition, changes=None):
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   257
        DiffBase.__init__(self)
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   258
        self.level = 2
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   259
        self.type = 'index'
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   260
        self.change = change
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   261
        self.schema = schema
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   262
        self.table = table
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   263
        self.index = index
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   264
        self.name = index
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   265
        self.definition = definition
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   266
        self.changes = changes
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   267
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   268
    def format_patch(self):
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   269
        q_drop = 'DROP INDEX %s;' % (self.index,)
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   270
        q_add = '%s;' % (self.definition,)
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   271
        if self.change == '*':
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   272
            out = [q_drop, q_add]
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   273
        if self.change == '+':
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   274
            out = [q_add]
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   275
        if self.change == '-':
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   276
            out = [q_drop]
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   277
        return out
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   278
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   279
85
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   280
class DiffType(DiffBase):
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   281
    def __init__(self, change, schema, name):
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   282
        DiffBase.__init__(self)
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   283
        self.level = 1
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   284
        self.type = 'type'
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   285
        self.change = change
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   286
        self.schema = schema
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   287
        self.name = name
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   288
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   289
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   290
class PgDiff:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   291
    def __init__(self, srcbrowser=None, dstbrowser=None):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   292
        self.allowcolor = False
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   293
        self.src = srcbrowser
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   294
        self.dst = dstbrowser
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   295
        self.include_schemas = set()  # if not empty, consider only these schemas for diff
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   296
        self.exclude_schemas = set()  # exclude these schemas from diff
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   297
        self.include_tables = set()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   298
        self.exclude_tables = set()
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
   299
        self.function_regex = re.compile(r"")
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
   300
        self.function_body_diff = False
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   301
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   302
    def _test_schema(self, schema):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   303
        if self.include_schemas and schema not in self.include_schemas:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   304
            return False
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   305
        if schema in self.exclude_schemas:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   306
            return False
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   307
        return True
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   308
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   309
    def _test_table(self, table):
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   310
        if self.include_tables and table not in self.include_tables:
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   311
            return False
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   312
        if table in self.exclude_tables:
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   313
            return False
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   314
        return True
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   315
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
   316
    def _test_function(self, 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
   317
        return bool(self.function_regex.match(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
   318
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   319
    def _diff_names(self, src, dst):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   320
        for x in src:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   321
            if x in dst:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   322
                yield ('*', x)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   323
            else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   324
                yield ('-', x)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   325
        for x in dst:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   326
            if x not in src:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   327
                yield ('+', x)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   328
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   329
    def _compare_columns(self, a, b):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   330
        diff = []
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   331
        if a.type != b.type:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   332
            diff.append(('type', a.type, b.type))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   333
        if a.notnull != b.notnull:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   334
            diff.append(('notnull', a.notnull, b.notnull))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   335
        if a.default != b.default:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   336
            diff.append(('default', a.default, b.default))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   337
        return diff
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   338
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   339
    def _compare_constraints(self, a, b):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   340
        diff = []
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   341
        if a.type != b.type:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   342
            diff.append(('type', a.type, b.type))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   343
        if a.definition != b.definition:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   344
            diff.append(('definition', a.definition, b.definition))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   345
        return diff
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   346
87
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   347
    def _compare_indexes(self, a, b):
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   348
        diff = []
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   349
        if a.definition != b.definition:
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   350
            diff.append(('definition', a.definition, b.definition))
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   351
        return diff
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   352
59
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   353
    def _compare_functions(self, a, b):
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   354
        diff = []
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   355
        if a.result != b.result:
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   356
            diff.append(('result', a.result, b.result))
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
   357
        # function source may differ in newlines (\n vs \r\n)
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
   358
        # split lines before comparison, so that these differencies are ignored
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
   359
        a_source = a.source.splitlines()
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
   360
        b_source = b.source.splitlines()
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
   361
        if a_source != b_source:
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
   362
            diff.append(('source', a_source, b_source))
59
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   363
        return diff
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   364
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   365
    def _compare_arguments(self, a, b):
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   366
        diff = []
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   367
        if a.type != b.type:
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   368
            diff.append(('type', a.type, b.type))
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   369
        if a.mode != b.mode:
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   370
            diff.append(('mode', a.mode, b.mode))
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   371
        if a.default != b.default:
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   372
            diff.append(('default', a.default, b.default))
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   373
        return diff
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   374
85
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   375
    def _compare_types(self, a, b):
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   376
        diff = []
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   377
        if a.type != b.type:
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   378
            diff.append(('type', a.type, b.type))
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   379
        if a.elements != b.elements:
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   380
            diff.append(('elements', repr(a.elements), repr(b.elements)))
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   381
        return diff
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   382
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   383
    def _diff_columns(self, schema, table, src_columns, dst_columns):
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   384
        for nd in self._diff_names(src_columns, dst_columns):
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   385
            if nd[1] in dst_columns:
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   386
                dst_type = dst_columns[nd[1]].type
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   387
                dst_default = dst_columns[nd[1]].default
60
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   388
                dst_notnull = dst_columns[nd[1]].notnull
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   389
            else:
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   390
                dst_type = None
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   391
                dst_default = None
60
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   392
                dst_notnull = None
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   393
            cdo = DiffColumn(change=nd[0], schema=schema, table=table, column=nd[1],
60
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   394
                columntype=dst_type, columndefault=dst_default, columnnotnull=dst_notnull)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   395
            if nd[0] == '*':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   396
                a = src_columns[nd[1]]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   397
                b = dst_columns[nd[1]]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   398
                cdo.changes = self._compare_columns(a, b)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   399
                if cdo.changes:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   400
                    yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   401
            else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   402
                yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   403
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   404
    def _diff_constraints(self, schema, table, src_constraints, dst_constraints):
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   405
        for nd in self._diff_names(src_constraints, dst_constraints):
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   406
            if nd[1] in dst_constraints:
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   407
                dst_definition = dst_constraints[nd[1]].definition
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   408
            else:
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   409
                dst_definition = None
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   410
            cdo = DiffConstraint(change=nd[0], schema=schema, table=table, constraint=nd[1],
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   411
                definition=dst_definition)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   412
            if nd[0] == '*':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   413
                a = src_constraints[nd[1]]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   414
                b = dst_constraints[nd[1]]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   415
                cdo.changes = self._compare_constraints(a, b)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   416
                if cdo.changes:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   417
                    yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   418
            else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   419
                yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   420
87
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   421
    def _diff_indexes(self, schema, table, src_indexes, dst_indexes):
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   422
        for nd in self._diff_names(src_indexes, dst_indexes):
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   423
            if nd[1] in dst_indexes:
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   424
                dst_definition = dst_indexes[nd[1]].definition
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   425
            else:
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   426
                dst_definition = None
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   427
            ido = DiffIndex(change=nd[0], schema=schema, table=table, index=nd[1],
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   428
                definition=dst_definition)
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   429
            if nd[0] == '*':
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   430
                a = src_indexes[nd[1]]
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   431
                b = dst_indexes[nd[1]]
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   432
                ido.changes = self._compare_indexes(a, b)
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   433
                if ido.changes:
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   434
                    yield ido
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   435
            else:
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   436
                yield ido
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   437
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   438
    def _diff_tables(self, schema, src_tables, dst_tables):
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   439
        for nd in self._diff_names(src_tables, dst_tables):
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   440
            if not self._test_table(nd[1]):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   441
                continue
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   442
            tdo = DiffTable(change=nd[0], schema=schema, table=nd[1])
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   443
            if nd[0] == '*':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   444
                # columns
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   445
                src_columns = src_tables[nd[1]].columns
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   446
                dst_columns = dst_tables[nd[1]].columns
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   447
                for cdo in self._diff_columns(schema, nd[1], src_columns, dst_columns):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   448
                    if tdo:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   449
                        yield tdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   450
                        tdo = None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   451
                    yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   452
                # constraints
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   453
                src_constraints = src_tables[nd[1]].constraints
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   454
                dst_constraints = dst_tables[nd[1]].constraints
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   455
                for cdo in self._diff_constraints(schema, nd[1], src_constraints, dst_constraints):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   456
                    if tdo:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   457
                        yield tdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   458
                        tdo = None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   459
                    yield cdo
87
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   460
                # indexes
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   461
                src_indexes = src_tables[nd[1]].indexes
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   462
                dst_indexes = dst_tables[nd[1]].indexes
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   463
                for ido in self._diff_indexes(schema, nd[1], src_indexes, dst_indexes):
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   464
                    if tdo:
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   465
                        yield tdo
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   466
                        tdo = None
3ef717ee9253 PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   467
                    yield ido
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   468
            else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   469
                yield tdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   470
59
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   471
    def _diff_arguments(self, schema, function, src_args, dst_args):
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   472
        for nd in self._diff_names(src_args, dst_args):
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   473
            ado = DiffArgument(change=nd[0], schema=schema, function=function, argument=nd[1])
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   474
            if nd[0] == '*':
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   475
                a = src_args[nd[1]]
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   476
                b = dst_args[nd[1]]
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   477
                ado.changes = self._compare_arguments(a, b)
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   478
                if ado.changes:
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   479
                    yield ado
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   480
            else:
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   481
                yield ado
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   482
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   483
    def _diff_functions(self, schema, src_functions, dst_functions):
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   484
        for nd in self._diff_names(src_functions, dst_functions):
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
   485
            if not self._test_function(nd[1]):
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
   486
                continue
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
   487
            fdo = DiffFunction(change=nd[0], schema=schema, function=nd[1], show_body_diff=self.function_body_diff)
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   488
            if nd[0] == '*':
59
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   489
                # compare function body and result
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   490
                a = src_functions[nd[1]]
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   491
                b = dst_functions[nd[1]]
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   492
                fdo.changes = self._compare_functions(a, b)
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   493
                if fdo.changes:
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   494
                    yield fdo
59
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   495
                    fdo = None
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   496
                # arguments
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   497
                src_args = src_functions[nd[1]].arguments
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   498
                dst_args = dst_functions[nd[1]].arguments
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   499
                for ado in self._diff_arguments(schema, nd[1], src_args, dst_args):
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   500
                    if fdo:
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   501
                        yield fdo
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   502
                        fdo = None
65efd0c6919f PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   503
                    yield ado
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   504
            else:
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   505
                yield fdo
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   506
85
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   507
    def _diff_types(self, schema, src_types, dst_types):
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   508
        for nd in self._diff_names(src_types, dst_types):
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   509
            tdo = DiffType(change=nd[0], schema=schema, name=nd[1])
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   510
            if nd[0] == '*':
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   511
                a = src_types[nd[1]]
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   512
                b = dst_types[nd[1]]
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   513
                tdo.changes = self._compare_types(a, b)
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   514
                if tdo.changes:
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   515
                    yield tdo
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   516
            else:
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   517
                yield tdo
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   518
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   519
    def iter_diff(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   520
        '''Return diff between src and dst database schema.
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   521
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   522
        Yields one line at the time. Each line is in form of object
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   523
        iherited from DiffBase. This object contains all information
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   524
        about changes. See format() method.
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   525
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   526
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   527
        src_schemas = self.src.schemas
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   528
        dst_schemas = self.dst.schemas
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   529
        src = [x.name for x in src_schemas.values() if not x.system and self._test_schema(x.name)]
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   530
        dst = [x.name for x in dst_schemas.values() if not x.system and self._test_schema(x.name)]
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   531
        for nd in self._diff_names(src, dst):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   532
            sdo = DiffSchema(change=nd[0], schema=nd[1])
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   533
            if nd[0] == '*':
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   534
                # tables
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   535
                src_tables = src_schemas[nd[1]].tables
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   536
                dst_tables = dst_schemas[nd[1]].tables
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   537
                for tdo in self._diff_tables(nd[1], src_tables, dst_tables):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   538
                    if sdo:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   539
                        yield sdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   540
                        sdo = None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   541
                    yield tdo
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   542
                # functions
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   543
                src_functions = src_schemas[nd[1]].functions
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   544
                dst_functions = dst_schemas[nd[1]].functions
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   545
                for fdo in self._diff_functions(nd[1], src_functions, dst_functions):
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   546
                    if sdo:
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   547
                        yield sdo
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   548
                        sdo = None
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   549
                    yield fdo
85
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   550
                # types
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   551
                src_types = src_schemas[nd[1]].types
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   552
                dst_types = dst_schemas[nd[1]].types
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   553
                for tdo in self._diff_types(nd[1], src_types, dst_types):
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   554
                    if sdo:
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   555
                        yield sdo
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   556
                        sdo = None
11a282e23e0d Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents: 83
diff changeset
   557
                    yield tdo
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   558
            else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   559
                yield sdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   560
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   561
    def print_diff(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   562
        '''Print diff between src and dst database schema.
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   563
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   564
        The output is in human readable form.
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   565
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   566
        Set allowcolor=True of PgDiff instance to get colored output.
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   567
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   568
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   569
        for ln in self.iter_diff():
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   570
            print(ln.format())
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   571
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   572
    def print_patch(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   573
        '''Print patch for updating from src schema to dst schema.
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   574
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   575
        Supports table drop, add, column drop, add and following
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   576
        changes of columns:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   577
          - type
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   578
          - set/remove not null
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   579
          - default value
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   580
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   581
        This is experimental, not tested very much.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   582
        Do not use without checking the commands.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   583
        Even if it works as intended, it can cause table lock ups
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   584
        and/or loss of data. You have been warned.
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   585
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   586
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   587
        for ln in self.iter_diff():
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   588
            patch = ln.format_patch()
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   589
            if patch:
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   590
                print('\n'.join(patch))
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   591
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   592
    def filter_schemas(self, include=[], exclude=[]):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   593
        '''Modify list of schemas which are used for computing diff.
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   594
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   595
        include (list) -- if not empty, consider only these schemas for diff
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   596
        exclude (list) -- exclude these schemas from diff
53
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   597
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   598
        Order: include, exclude
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   599
        include=[] means include everything
61
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
   600
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
   601
        Raises:
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
   602
            PgDiffError: when schema from include list is not found in src db
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
   603
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   604
        '''
61
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
   605
        for schema in include:
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
   606
            self._check_schema_exist(schema)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   607
        self.include_schemas.clear()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   608
        self.include_schemas.update(include)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   609
        self.exclude_schemas.clear()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   610
        self.exclude_schemas.update(exclude)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   611
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   612
    def filter_tables(self, include=[], exclude=[]):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   613
        self.include_tables.clear()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   614
        self.include_tables.update(include)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   615
        self.exclude_tables.clear()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   616
        self.exclude_tables.update(exclude)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   617
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
   618
    def filter_functions(self, regex=''):
8c7f0a51ba50 PgDiff, schemadiff.py: Add function filter. Add --body parameter to diff function source.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
   619
        self.function_regex = re.compile(regex)
61
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
   620
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
   621
    def _check_schema_exist(self, schema):
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
   622
        if not schema in self.src.schemas:
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
   623
            raise PgDiffError('Schema "%s" not found in source database.' % schema)
703bba757605 PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
   624