pgtoolkit/pgdiff.py
author Radek Brich <radek.brich@devl.cz>
Sat, 29 Sep 2012 14:01:47 +0200
changeset 50 f71d3abbb18f
parent 47 bb8c729ae6ce
child 53 4a049a5af657
permissions -rw-r--r--
Add test_prettysize to tests.py.
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
    }
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    37
    
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
    }
6
4ab077c93b2d Add table diff tool.
Radek Brich <radek.brich@devl.cz>
parents: 0
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)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
        out += [' ', self.type, ' ', self.name, highlight(0)]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
        
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
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
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 = []
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
        for x in self.changes:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
            type, a, b = x
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
            if type == 'notnull':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
                type = ''
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    72
                a = self._formatnotnull(a)
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    73
                b = self._formatnotnull(b)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
                
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
            if a and b:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
                s = ''.join(['Changed ', type, ' from ',
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
                    highlight(1,15), a, highlight(0), ' to ',
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
                    highlight(1,15), b, highlight(0), '.'])
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
            elif a and not b:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    80
                l = ['Removed ']
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
                if type:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
                    l += [type, ' ']
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
                l += [highlight(1,15), a, highlight(0), '.']
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
                s = ''.join(l)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
            elif b and not a:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
                l = ['Added ']
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), b, 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
            res.append(s)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
        return ' '.join(res)
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    93
    
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    94
    def format_patch(self):
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    95
        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
    96
            return None
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    97
        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
    98
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
class DiffSchema(DiffBase):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
    def __init__(self, change, schema):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
        DiffBase.__init__(self)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
        self.level = 0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
        self.type = 'schema'
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
        self.change = change
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   106
        self.schema = schema
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   107
        self.name = schema
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   108
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   109
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   110
class DiffTable(DiffBase):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   111
    def __init__(self, change, schema, table):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   112
        DiffBase.__init__(self)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   113
        self.level = 1
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   114
        self.type = 'table'
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   115
        self.change = change
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   116
        self.schema = schema
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   117
        self.table = table
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   118
        self.name = table
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   119
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   120
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   121
class DiffColumn(DiffBase):
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   122
    ALTER_COMMANDS = {
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   123
        '+' : 'ADD',
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   124
        '-' : 'DROP',
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   125
        '*' : 'ALTER',
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   126
    }
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   127
    
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   128
    def __init__(self, change, schema, table, column, columntype, columndefault, changes=None):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   129
        DiffBase.__init__(self)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   130
        self.level = 2
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   131
        self.type = 'column'
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   132
        self.change = change
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   133
        self.schema = schema
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   134
        self.table = table
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   135
        self.column = column
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   136
        self.columntype = columntype
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   137
        self.columndefault = columndefault
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   138
        self.name = column
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   139
        self.changes = changes
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   140
    
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   141
    def format_patch(self):
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   142
        out = 'ALTER TABLE %s.%s %s COLUMN %s %s' % (
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   143
            self.schema,
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   144
            self.table,
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   145
            self.ALTER_COMMANDS[self.change],
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   146
            self.name,
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   147
            self.columntype
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   148
        )
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   149
        if self.columndefault:
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   150
            out += ' DEFAULT ' + self.columndefault
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   151
        out += ';'
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   152
        return out
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   153
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   154
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   155
class DiffConstraint(DiffBase):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   156
    def __init__(self, change, schema, table, constraint, changes=None):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   157
        DiffBase.__init__(self)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   158
        self.level = 2
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   159
        self.type = 'constraint'
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   160
        self.change = change
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   161
        self.schema = schema
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   162
        self.table = table
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   163
        self.constraint = constraint
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   164
        self.name = constraint
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   165
        self.changes = changes
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   166
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   167
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   168
class PgDiff:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   169
    def __init__(self, srcbrowser=None, dstbrowser=None):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   170
        self.allowcolor = False
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   171
        self.src = srcbrowser
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   172
        self.dst = dstbrowser
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   173
        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
   174
        self.exclude_schemas = set()  # exclude these schemas from diff
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   175
        self.include_tables = set()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   176
        self.exclude_tables = set()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   177
    
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   178
    def _test_schema(self, schema):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   179
        if self.include_schemas and schema not in self.include_schemas:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   180
            return False
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   181
        if schema in self.exclude_schemas:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   182
            return False
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   183
        return True
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   184
    
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   185
    def _test_table(self, table):
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   186
        if self.include_tables and table not in self.include_tables:
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   187
            return False
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   188
        if table in self.exclude_tables:
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   189
            return False
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   190
        return True 
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   191
    
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   192
    def _diff_names(self, src, dst):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   193
        for x in src:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   194
            if x in dst:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   195
                yield ('*', x)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   196
            else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   197
                yield ('-', x)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   198
        for x in dst:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   199
            if x not in src:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   200
                yield ('+', x)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   201
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   202
    def _compare_columns(self, a, b):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   203
        diff = []
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   204
        if a.type != b.type:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   205
            diff.append(('type', a.type, b.type))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   206
        if a.notnull != b.notnull:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   207
            diff.append(('notnull', a.notnull, b.notnull))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   208
        if a.default != b.default:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   209
            diff.append(('default', a.default, b.default))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   210
        return diff
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
    def _compare_constraints(self, a, b):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   213
        diff = []
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   214
        if a.type != b.type:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   215
            diff.append(('type', a.type, b.type))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   216
        if a.definition != b.definition:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   217
            diff.append(('definition', a.definition, b.definition))
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   218
        return diff
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   219
                
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   220
    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
   221
        for nd in self._diff_names(src_columns, dst_columns):
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   222
            cdo = DiffColumn(change=nd[0], schema=schema, table=table, column=nd[1],
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   223
                columntype=dst_columns[nd[1]].type, columndefault=dst_columns[nd[1]].default)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   224
            if nd[0] == '*':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   225
                a = src_columns[nd[1]]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   226
                b = dst_columns[nd[1]]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   227
                cdo.changes = self._compare_columns(a, b)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   228
                if cdo.changes:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   229
                    yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   230
            else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   231
                yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   232
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   233
    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
   234
        for nd in self._diff_names(src_constraints, dst_constraints):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   235
            cdo = DiffConstraint(change=nd[0], schema=schema, table=table, constraint=nd[1])
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   236
            if nd[0] == '*':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   237
                a = src_constraints[nd[1]]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   238
                b = dst_constraints[nd[1]]
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   239
                cdo.changes = self._compare_constraints(a, b)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   240
                if cdo.changes:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   241
                    yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   242
            else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   243
                yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   244
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   245
    def _difftables(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
   246
        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
   247
            if not self._test_table(nd[1]):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   248
                continue
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   249
            tdo = DiffTable(change=nd[0], schema=schema, table=nd[1])
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   250
            if nd[0] == '*':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   251
                # columns
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   252
                src_columns = src_tables[nd[1]].columns
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   253
                dst_columns = dst_tables[nd[1]].columns
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   254
                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
   255
                    if tdo:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   256
                        yield tdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   257
                        tdo = None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   258
                    yield cdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   259
                # constraints
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   260
                src_constraints = src_tables[nd[1]].constraints
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   261
                dst_constraints = dst_tables[nd[1]].constraints
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   262
                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
   263
                    if tdo:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   264
                        yield tdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   265
                        tdo = None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   266
                    yield cdo
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 tdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   269
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   270
    def iter_diff(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   271
        '''Return diff between src and dst database schema.
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   272
        
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   273
        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
   274
        iherited from DiffBase. This object contains all information
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   275
        about changes. See format() method.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   276
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   277
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   278
        src_schemas = self.src.schemas
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   279
        dst_schemas = self.dst.schemas
47
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   280
        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
   281
        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
   282
        for nd in self._diff_names(src, dst):
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   283
            sdo = DiffSchema(change=nd[0], schema=nd[1])
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   284
            if nd[0] == '*':
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   285
                src_tables = src_schemas[nd[1]].tables
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   286
                dst_tables = dst_schemas[nd[1]].tables
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   287
                for tdo in self._difftables(nd[1], src_tables, dst_tables):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   288
                    if sdo:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   289
                        yield sdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   290
                        sdo = None
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   291
                    yield tdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   292
            else:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   293
                yield sdo
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   294
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   295
    def print_diff(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   296
        '''Print diff between src and dst database schema.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   297
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   298
        The output is in human readable form.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   299
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   300
        Set allowcolor=True of PgDiff instance to get colored output. 
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   301
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   302
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   303
        for ln in self.iter_diff():
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   304
            print(ln.format())
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   305
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   306
    def print_patch(self):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   307
        '''Print patch for updating from src schema to dst schema.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   308
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   309
        Supports table drop, add, column drop, add and following
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   310
        changes of columns:
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   311
          - type
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   312
          - set/remove not null
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   313
          - default value
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   314
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   315
        This is experimental, not tested very much.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   316
        Do not use without checking the commands.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   317
        Even if it works as intended, it can cause table lock ups
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   318
        and/or loss of data. You have been warned.
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   319
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   320
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   321
        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
   322
            patch = ln.format_patch()
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   323
            if patch:
bb8c729ae6ce PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   324
                print(patch)
0
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   325
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   326
    def filter_schemas(self, include=[], exclude=[]):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   327
        '''Modify list of schemas which are used for computing diff.
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
        include (list) -- if not empty, consider only these schemas for diff
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   330
        exclude (list) -- exclude these schemas from diff
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   331
        
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   332
        Order: include, exclude
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   333
        include=[] means include everything
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   334
        '''
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   335
        self.include_schemas.clear()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   336
        self.include_schemas.update(include)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   337
        self.exclude_schemas.clear()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   338
        self.exclude_schemas.update(exclude)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   339
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   340
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   341
    def filter_tables(self, include=[], exclude=[]):
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   342
        self.include_tables.clear()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   343
        self.include_tables.update(include)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   344
        self.exclude_tables.clear()
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   345
        self.exclude_tables.update(exclude)
eaae9539e910 Postgres tools.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   346