pgtoolkit/pgdiff.py
author Radek Brich <radek.brich@devl.cz>
Fri, 25 Jan 2013 17:44:49 +0100
changeset 60 bb6b20106ff5
parent 59 65efd0c6919f
child 61 703bba757605
permissions -rw-r--r--
PgDiff: Update patch for table column changed.
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
9
2fcc8ef0b97d Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
    28
from pgtoolkit.highlight import *
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
class DiffBase:
6
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    32
    COLORS = {
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    33
        '+' : BOLD | GREEN,
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    34
        '-' : BOLD | RED,
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    35
        '*' : BOLD | YELLOW,
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    36
    }
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
    37
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    38
    COMMANDS = {
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    39
        '+' : 'CREATE',
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    40
        '-' : 'DROP',
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    41
        '*' : 'ALTER',
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    42
    }
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
    43
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
    def __init__(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
        self.changes = None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
    def format(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
        out = ['  ' * self.level]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
6
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    50
        out.append(highlight(1, self.COLORS[self.change]))
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
        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
    52
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
        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
    54
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
        if self.changes:
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    56
            out += [highlight(1, WHITE), ' (', self._formatchanges(), ')', highlight(0)]
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
        return ''.join(out)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    60
    def _formatnotnull(self, notnull):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
        if notnull:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
            return 'NOT NULL'
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
        else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
            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
    65
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    66
    def _formatchanges(self):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
        res = []
60
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    68
        for type, a, b in self.changes:
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
            if type == 'notnull':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
                type = ''
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    71
                a = self._formatnotnull(a)
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    72
                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
    73
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
            if a and b:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
                s = ''.join(['Changed ', type, ' from ',
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
                    highlight(1,15), a, highlight(0), ' to ',
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
                    highlight(1,15), b, highlight(0), '.'])
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
            elif a and not b:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
                l = ['Removed ']
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    80
                if type:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
                    l += [type, ' ']
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
                l += [highlight(1,15), a, highlight(0), '.']
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
                s = ''.join(l)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
            elif b and not a:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
                l = ['Added ']
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
                if type:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    87
                    l += [type, ' ']
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
                l += [highlight(1,15), b, highlight(0), '.']
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
                s = ''.join(l)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
            res.append(s)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
        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
    92
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    93
    def format_patch(self):
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    94
        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
    95
            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
    96
        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
    97
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
class DiffSchema(DiffBase):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
    def __init__(self, change, schema):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
        DiffBase.__init__(self)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
        self.level = 0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
        self.type = 'schema'
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
        self.change = change
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
        self.schema = schema
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   106
        self.name = schema
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   107
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   108
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   109
class DiffTable(DiffBase):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   110
    def __init__(self, change, schema, table):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   111
        DiffBase.__init__(self)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   112
        self.level = 1
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   113
        self.type = 'table'
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   114
        self.change = change
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   115
        self.schema = schema
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   116
        self.table = table
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   117
        self.name = table
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   118
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   119
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
   120
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
   121
    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
   122
        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
   123
        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
   124
        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
   125
        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
   126
        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
   127
        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
   128
        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
   129
        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
   130
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
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   132
class DiffFunction(DiffBase):
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   133
    def __init__(self, change, schema, function):
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   134
        DiffBase.__init__(self)
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   135
        self.level = 1
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   136
        self.type = 'function'
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   137
        self.change = change
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   138
        self.schema = schema
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   139
        self.function = function
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   140
        self.name = function
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   141
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   142
    def _formatchanges(self):
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   143
        res = []
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   144
        for x in self.changes:
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   145
            type, a, b = x
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   146
            if type == 'source':
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   147
                s = 'Changed source.'
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   148
            else:
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   149
                s = ''.join(['Changed ', type, ' from ',
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   150
                    highlight(1,15), a, highlight(0), ' to ',
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   151
                    highlight(1,15), b, highlight(0), '.'])
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   152
            res.append(s)
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   153
        return ' '.join(res)
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   154
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   155
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   156
class DiffColumn(DiffBase):
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   157
    ALTER_COMMANDS = {
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   158
        '+' : 'ADD',
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   159
        '-' : 'DROP',
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   160
        '*' : 'ALTER',
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   161
    }
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
   162
60
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   163
    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
   164
        DiffBase.__init__(self)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   165
        self.level = 2
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   166
        self.type = 'column'
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   167
        self.change = change
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   168
        self.schema = schema
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   169
        self.table = table
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   170
        self.column = column
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   171
        self.columntype = columntype
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   172
        self.columndefault = columndefault
60
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   173
        self.columnnotnull = columnnotnull
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   174
        self.name = column
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   175
        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
   176
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   177
    def format_patch(self):
60
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   178
        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
   179
            self.schema,
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   180
            self.table,
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   181
            self.ALTER_COMMANDS[self.change],
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   182
            self.name,
60
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   183
        )
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   184
        out = []
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   185
        if self.change == '-':
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   186
            out.append('%s;' % alter_table);
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   187
        if self.change == '+':
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   188
            notnull = ''
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   189
            if self.columnnotnull:
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   190
                notnull = ' NOT NULL'
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   191
            default = ''
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   192
            if self.columndefault:
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   193
                default = ' DEFAULT %s' % self.columndefault
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   194
            out.append('%s %s%s%s;'
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   195
                % (alter_table, self.columntype, notnull, default));
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   196
        if self.change == '*':
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   197
            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
   198
                if type == 'type':
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   199
                    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
   200
                if type == 'notnull':
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   201
                    if a and not b:
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   202
                        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
   203
                    if not a and b:
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   204
                        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
   205
                if type == 'default':
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   206
                    if b:
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   207
                        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
   208
                    else:
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   209
                        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
   210
        return out
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   211
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   212
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   213
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
   214
    def __init__(self, change, schema, table, constraint, definition, changes=None):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   215
        DiffBase.__init__(self)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   216
        self.level = 2
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   217
        self.type = 'constraint'
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   218
        self.change = change
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   219
        self.schema = schema
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   220
        self.table = table
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   221
        self.constraint = constraint
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   222
        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
   223
        self.definition = definition
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   224
        self.changes = changes
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   225
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
   226
    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
   227
        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
   228
        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
   229
        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
   230
        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
   231
            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
   232
        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
   233
            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
   234
        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
   235
            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
   236
        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
   237
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   238
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   239
class PgDiff:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   240
    def __init__(self, srcbrowser=None, dstbrowser=None):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   241
        self.allowcolor = False
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   242
        self.src = srcbrowser
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   243
        self.dst = dstbrowser
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   244
        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
   245
        self.exclude_schemas = set()  # exclude these schemas from diff
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   246
        self.include_tables = set()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   247
        self.exclude_tables = set()
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
   248
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   249
    def _test_schema(self, schema):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   250
        if self.include_schemas and schema not in self.include_schemas:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   251
            return False
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   252
        if schema in self.exclude_schemas:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   253
            return False
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   254
        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
   255
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   256
    def _test_table(self, table):
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   257
        if self.include_tables and table not in self.include_tables:
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   258
            return False
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   259
        if table in self.exclude_tables:
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   260
            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
   261
        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
   262
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   263
    def _diff_names(self, src, dst):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   264
        for x in src:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   265
            if x in dst:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   266
                yield ('*', x)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   267
            else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   268
                yield ('-', x)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   269
        for x in dst:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   270
            if x not in src:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   271
                yield ('+', x)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   272
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   273
    def _compare_columns(self, a, b):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   274
        diff = []
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   275
        if a.type != b.type:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   276
            diff.append(('type', a.type, b.type))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   277
        if a.notnull != b.notnull:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   278
            diff.append(('notnull', a.notnull, b.notnull))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   279
        if a.default != b.default:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   280
            diff.append(('default', a.default, b.default))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   281
        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
   282
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   283
    def _compare_constraints(self, a, b):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   284
        diff = []
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   285
        if a.type != b.type:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   286
            diff.append(('type', a.type, b.type))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   287
        if a.definition != b.definition:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   288
            diff.append(('definition', a.definition, b.definition))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   289
        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
   290
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
   291
    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
   292
        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
   293
        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
   294
            diff.append(('result', 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
   295
        if a.source != b.source:
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
   296
            diff.append(('source', a.source, b.source))
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
   297
        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
   298
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
   299
    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
   300
        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
   301
        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
   302
            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
   303
        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
   304
            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
   305
        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
   306
            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
   307
        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
   308
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   309
    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
   310
        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
   311
            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
   312
                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
   313
                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
   314
                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
   315
            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
   316
                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
   317
                dst_default = None
60
bb6b20106ff5 PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
   318
                dst_notnull = None
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   319
            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
   320
                columntype=dst_type, columndefault=dst_default, columnnotnull=dst_notnull)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   321
            if nd[0] == '*':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   322
                a = src_columns[nd[1]]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   323
                b = dst_columns[nd[1]]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   324
                cdo.changes = self._compare_columns(a, b)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   325
                if cdo.changes:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   326
                    yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   327
            else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   328
                yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   329
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   330
    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
   331
        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
   332
            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
   333
                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
   334
            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
   335
                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
   336
            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
   337
                definition=dst_definition)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   338
            if nd[0] == '*':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   339
                a = src_constraints[nd[1]]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   340
                b = dst_constraints[nd[1]]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   341
                cdo.changes = self._compare_constraints(a, b)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   342
                if cdo.changes:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   343
                    yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   344
            else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   345
                yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   346
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   347
    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
   348
        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
   349
            if not self._test_table(nd[1]):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   350
                continue
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   351
            tdo = DiffTable(change=nd[0], schema=schema, table=nd[1])
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   352
            if nd[0] == '*':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   353
                # columns
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   354
                src_columns = src_tables[nd[1]].columns
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   355
                dst_columns = dst_tables[nd[1]].columns
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   356
                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
   357
                    if tdo:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   358
                        yield tdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   359
                        tdo = None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   360
                    yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   361
                # constraints
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   362
                src_constraints = src_tables[nd[1]].constraints
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   363
                dst_constraints = dst_tables[nd[1]].constraints
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   364
                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
   365
                    if tdo:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   366
                        yield tdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   367
                        tdo = None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   368
                    yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   369
            else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   370
                yield tdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   371
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
   372
    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
   373
        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
   374
            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
   375
            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
   376
                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
   377
                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
   378
                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
   379
                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
   380
                    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
   381
            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
   382
                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
   383
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   384
    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
   385
        for nd in self._diff_names(src_functions, dst_functions):
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   386
            fdo = DiffFunction(change=nd[0], schema=schema, function=nd[1])
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   387
            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
   388
                # compare function body and result
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   389
                a = src_functions[nd[1]]
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   390
                b = dst_functions[nd[1]]
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   391
                fdo.changes = self._compare_functions(a, b)
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   392
                if fdo.changes:
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   393
                    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
   394
                    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
   395
                # 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
   396
                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
   397
                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
   398
                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
   399
                    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
   400
                        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
   401
                        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
   402
                    yield ado
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   403
            else:
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   404
                yield fdo
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   405
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   406
    def iter_diff(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   407
        '''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
   408
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   409
        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
   410
        iherited from DiffBase. This object contains all information
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   411
        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
   412
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   413
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   414
        src_schemas = self.src.schemas
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   415
        dst_schemas = self.dst.schemas
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   416
        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
   417
        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
   418
        for nd in self._diff_names(src, dst):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   419
            sdo = DiffSchema(change=nd[0], schema=nd[1])
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   420
            if nd[0] == '*':
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   421
                # tables
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   422
                src_tables = src_schemas[nd[1]].tables
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   423
                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
   424
                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
   425
                    if sdo:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   426
                        yield sdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   427
                        sdo = None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   428
                    yield tdo
58
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   429
                # functions
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   430
                src_functions = src_schemas[nd[1]].functions
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   431
                dst_functions = dst_schemas[nd[1]].functions
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   432
                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
   433
                    if sdo:
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   434
                        yield sdo
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   435
                        sdo = None
0bcc13460dae PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   436
                    yield fdo
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   437
            else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   438
                yield sdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   439
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   440
    def print_diff(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   441
        '''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
   442
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   443
        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
   444
4a049a5af657 Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents: 47
diff changeset
   445
        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
   446
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   447
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   448
        for ln in self.iter_diff():
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   449
            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
   450
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   451
    def print_patch(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   452
        '''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
   453
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   454
        Supports table drop, add, column drop, add and following
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   455
        changes of columns:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   456
          - type
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   457
          - set/remove not null
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   458
          - 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
   459
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   460
        This is experimental, not tested very much.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   461
        Do not use without checking the commands.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   462
        Even if it works as intended, it can cause table lock ups
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   463
        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
   464
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   465
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   466
        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
   467
            patch = ln.format_patch()
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   468
            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
   469
                print('\n'.join(patch))
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   470
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   471
    def filter_schemas(self, include=[], exclude=[]):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   472
        '''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
   473
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   474
        include (list) -- if not empty, consider only these schemas for diff
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   475
        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
   476
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   477
        Order: include, exclude
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   478
        include=[] means include everything
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   479
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   480
        self.include_schemas.clear()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   481
        self.include_schemas.update(include)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   482
        self.exclude_schemas.clear()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   483
        self.exclude_schemas.update(exclude)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   484
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   485
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   486
    def filter_tables(self, include=[], exclude=[]):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   487
        self.include_tables.clear()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   488
        self.include_tables.update(include)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   489
        self.exclude_tables.clear()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   490
        self.exclude_tables.update(exclude)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   491