author | Radek Brich <radek.brich@devl.cz> |
Fri, 25 Jan 2013 18:04:17 +0100 | |
changeset 61 | 703bba757605 |
parent 60 | bb6b20106ff5 |
child 63 | 8c7f0a51ba50 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
# |
|
3 |
# PgDiff - capture differences of database metadata |
|
4 |
# |
|
5 |
# Depends on PgBrowser |
|
6 |
# |
|
7 |
# Copyright (c) 2011 Radek Brich <radek.brich@devl.cz> |
|
8 |
# |
|
9 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
10 |
# of this software and associated documentation files (the "Software"), to deal |
|
11 |
# in the Software without restriction, including without limitation the rights |
|
12 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
13 |
# copies of the Software, and to permit persons to whom the Software is |
|
14 |
# furnished to do so, subject to the following conditions: |
|
15 |
# |
|
16 |
# The above copyright notice and this permission notice shall be included in |
|
17 |
# all copies or substantial portions of the Software. |
|
18 |
# |
|
19 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
20 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
21 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
22 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
23 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
24 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
25 |
# THE SOFTWARE. |
|
26 |
||
27 |
||
9
2fcc8ef0b97d
Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
28 |
from pgtoolkit.highlight import * |
0 | 29 |
|
30 |
||
61
703bba757605
PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
31 |
class PgDiffError(Exception): |
703bba757605
PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
32 |
pass |
703bba757605
PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
33 |
|
703bba757605
PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
34 |
|
0 | 35 |
class DiffBase: |
6 | 36 |
COLORS = { |
37 |
'+' : BOLD | GREEN, |
|
38 |
'-' : BOLD | RED, |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
39 |
'*' : BOLD | YELLOW, |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
40 |
} |
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
|
41 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
42 |
COMMANDS = { |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
43 |
'+' : 'CREATE', |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
44 |
'-' : 'DROP', |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
45 |
'*' : 'ALTER', |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
46 |
} |
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
|
47 |
|
0 | 48 |
def __init__(self): |
49 |
self.changes = None |
|
50 |
||
51 |
def format(self): |
|
52 |
out = [' ' * self.level] |
|
53 |
||
6 | 54 |
out.append(highlight(1, self.COLORS[self.change])) |
0 | 55 |
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
|
56 |
|
0 | 57 |
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
|
58 |
|
0 | 59 |
if self.changes: |
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
60 |
out += [highlight(1, WHITE), ' (', self._formatchanges(), ')', highlight(0)] |
0 | 61 |
|
62 |
return ''.join(out) |
|
63 |
||
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
64 |
def _formatnotnull(self, notnull): |
0 | 65 |
if notnull: |
66 |
return 'NOT NULL' |
|
67 |
else: |
|
68 |
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
|
69 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
70 |
def _formatchanges(self): |
0 | 71 |
res = [] |
60
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
72 |
for type, a, b in self.changes: |
0 | 73 |
if type == 'notnull': |
74 |
type = '' |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
75 |
a = self._formatnotnull(a) |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
76 |
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
|
77 |
|
0 | 78 |
if a and b: |
79 |
s = ''.join(['Changed ', type, ' from ', |
|
80 |
highlight(1,15), a, highlight(0), ' to ', |
|
81 |
highlight(1,15), b, highlight(0), '.']) |
|
82 |
elif a and not b: |
|
83 |
l = ['Removed '] |
|
84 |
if type: |
|
85 |
l += [type, ' '] |
|
86 |
l += [highlight(1,15), a, highlight(0), '.'] |
|
87 |
s = ''.join(l) |
|
88 |
elif b and not a: |
|
89 |
l = ['Added '] |
|
90 |
if type: |
|
91 |
l += [type, ' '] |
|
92 |
l += [highlight(1,15), b, highlight(0), '.'] |
|
93 |
s = ''.join(l) |
|
94 |
res.append(s) |
|
95 |
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
|
96 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
97 |
def format_patch(self): |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
98 |
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
|
99 |
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
|
100 |
return ['%s %s %s;' % (self.COMMANDS[self.change], self.type.upper(), self.name)] |
0 | 101 |
|
102 |
||
103 |
class DiffSchema(DiffBase): |
|
104 |
def __init__(self, change, schema): |
|
105 |
DiffBase.__init__(self) |
|
106 |
self.level = 0 |
|
107 |
self.type = 'schema' |
|
108 |
self.change = change |
|
109 |
self.schema = schema |
|
110 |
self.name = schema |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
111 |
|
0 | 112 |
|
113 |
class DiffTable(DiffBase): |
|
114 |
def __init__(self, change, schema, table): |
|
115 |
DiffBase.__init__(self) |
|
116 |
self.level = 1 |
|
117 |
self.type = 'table' |
|
118 |
self.change = change |
|
119 |
self.schema = schema |
|
120 |
self.table = table |
|
121 |
self.name = table |
|
122 |
||
123 |
||
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
|
124 |
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
|
125 |
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
|
126 |
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
|
127 |
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
|
128 |
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
|
129 |
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
|
130 |
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
|
131 |
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
|
132 |
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
|
133 |
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
|
134 |
|
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
135 |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
136 |
class DiffFunction(DiffBase): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
137 |
def __init__(self, change, schema, function): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
138 |
DiffBase.__init__(self) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
139 |
self.level = 1 |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
140 |
self.type = 'function' |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
141 |
self.change = change |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
142 |
self.schema = schema |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
143 |
self.function = function |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
144 |
self.name = function |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
145 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
146 |
def _formatchanges(self): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
147 |
res = [] |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
148 |
for x in self.changes: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
149 |
type, a, b = x |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
150 |
if type == 'source': |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
151 |
s = 'Changed source.' |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
152 |
else: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
153 |
s = ''.join(['Changed ', type, ' from ', |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
154 |
highlight(1,15), a, highlight(0), ' to ', |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
155 |
highlight(1,15), b, highlight(0), '.']) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
156 |
res.append(s) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
157 |
return ' '.join(res) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
158 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
159 |
|
0 | 160 |
class DiffColumn(DiffBase): |
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
161 |
ALTER_COMMANDS = { |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
162 |
'+' : 'ADD', |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
163 |
'-' : 'DROP', |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
164 |
'*' : 'ALTER', |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
165 |
} |
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
|
166 |
|
60
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
167 |
def __init__(self, change, schema, table, column, columntype, columndefault, columnnotnull, changes=None): |
0 | 168 |
DiffBase.__init__(self) |
169 |
self.level = 2 |
|
170 |
self.type = 'column' |
|
171 |
self.change = change |
|
172 |
self.schema = schema |
|
173 |
self.table = table |
|
174 |
self.column = column |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
175 |
self.columntype = columntype |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
176 |
self.columndefault = columndefault |
60
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
177 |
self.columnnotnull = columnnotnull |
0 | 178 |
self.name = column |
179 |
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
|
180 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
181 |
def format_patch(self): |
60
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
182 |
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
|
183 |
self.schema, |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
184 |
self.table, |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
185 |
self.ALTER_COMMANDS[self.change], |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
186 |
self.name, |
60
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
187 |
) |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
188 |
out = [] |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
189 |
if self.change == '-': |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
190 |
out.append('%s;' % alter_table); |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
191 |
if self.change == '+': |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
192 |
notnull = '' |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
193 |
if self.columnnotnull: |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
194 |
notnull = ' NOT NULL' |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
195 |
default = '' |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
196 |
if self.columndefault: |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
197 |
default = ' DEFAULT %s' % self.columndefault |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
198 |
out.append('%s %s%s%s;' |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
199 |
% (alter_table, self.columntype, notnull, default)); |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
200 |
if self.change == '*': |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
201 |
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
|
202 |
if type == 'type': |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
203 |
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
|
204 |
if type == 'notnull': |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
205 |
if a and not b: |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
206 |
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
|
207 |
if not a and b: |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
208 |
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
|
209 |
if type == 'default': |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
210 |
if b: |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
211 |
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
|
212 |
else: |
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
213 |
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
|
214 |
return out |
0 | 215 |
|
216 |
||
217 |
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
|
218 |
def __init__(self, change, schema, table, constraint, definition, changes=None): |
0 | 219 |
DiffBase.__init__(self) |
220 |
self.level = 2 |
|
221 |
self.type = 'constraint' |
|
222 |
self.change = change |
|
223 |
self.schema = schema |
|
224 |
self.table = table |
|
225 |
self.constraint = constraint |
|
226 |
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
|
227 |
self.definition = definition |
0 | 228 |
self.changes = changes |
229 |
||
53
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
230 |
def 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
|
231 |
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
|
232 |
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
|
233 |
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
|
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, 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
|
236 |
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
|
237 |
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
|
238 |
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
|
239 |
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
|
240 |
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
|
241 |
|
0 | 242 |
|
243 |
class PgDiff: |
|
244 |
def __init__(self, srcbrowser=None, dstbrowser=None): |
|
245 |
self.allowcolor = False |
|
246 |
self.src = srcbrowser |
|
247 |
self.dst = dstbrowser |
|
248 |
self.include_schemas = set() # if not empty, consider only these schemas for diff |
|
249 |
self.exclude_schemas = set() # exclude these schemas from diff |
|
250 |
self.include_tables = set() |
|
251 |
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
|
252 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
253 |
def _test_schema(self, schema): |
0 | 254 |
if self.include_schemas and schema not in self.include_schemas: |
255 |
return False |
|
256 |
if schema in self.exclude_schemas: |
|
257 |
return False |
|
258 |
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
|
259 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
260 |
def _test_table(self, table): |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
261 |
if self.include_tables and table not in self.include_tables: |
0 | 262 |
return False |
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
263 |
if table in self.exclude_tables: |
0 | 264 |
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
|
265 |
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
|
266 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
267 |
def _diff_names(self, src, dst): |
0 | 268 |
for x in src: |
269 |
if x in dst: |
|
270 |
yield ('*', x) |
|
271 |
else: |
|
272 |
yield ('-', x) |
|
273 |
for x in dst: |
|
274 |
if x not in src: |
|
275 |
yield ('+', x) |
|
276 |
||
277 |
def _compare_columns(self, a, b): |
|
278 |
diff = [] |
|
279 |
if a.type != b.type: |
|
280 |
diff.append(('type', a.type, b.type)) |
|
281 |
if a.notnull != b.notnull: |
|
282 |
diff.append(('notnull', a.notnull, b.notnull)) |
|
283 |
if a.default != b.default: |
|
284 |
diff.append(('default', a.default, b.default)) |
|
285 |
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
|
286 |
|
0 | 287 |
def _compare_constraints(self, a, b): |
288 |
diff = [] |
|
289 |
if a.type != b.type: |
|
290 |
diff.append(('type', a.type, b.type)) |
|
291 |
if a.definition != b.definition: |
|
292 |
diff.append(('definition', a.definition, b.definition)) |
|
293 |
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
|
294 |
|
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
|
295 |
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
|
296 |
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
|
297 |
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
|
298 |
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
|
299 |
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
|
300 |
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
|
301 |
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
|
302 |
|
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 |
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
|
304 |
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
|
305 |
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
|
306 |
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
|
307 |
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
|
308 |
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
|
309 |
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
|
310 |
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
|
311 |
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
|
312 |
|
0 | 313 |
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
|
314 |
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
|
315 |
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
|
316 |
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
|
317 |
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
|
318 |
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
|
319 |
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
|
320 |
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
|
321 |
dst_default = None |
60
bb6b20106ff5
PgDiff: Update patch for table column changed.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
322 |
dst_notnull = None |
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
323 |
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
|
324 |
columntype=dst_type, columndefault=dst_default, columnnotnull=dst_notnull) |
0 | 325 |
if nd[0] == '*': |
326 |
a = src_columns[nd[1]] |
|
327 |
b = dst_columns[nd[1]] |
|
328 |
cdo.changes = self._compare_columns(a, b) |
|
329 |
if cdo.changes: |
|
330 |
yield cdo |
|
331 |
else: |
|
332 |
yield cdo |
|
333 |
||
334 |
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
|
335 |
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
|
336 |
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
|
337 |
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
|
338 |
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
|
339 |
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
|
340 |
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
|
341 |
definition=dst_definition) |
0 | 342 |
if nd[0] == '*': |
343 |
a = src_constraints[nd[1]] |
|
344 |
b = dst_constraints[nd[1]] |
|
345 |
cdo.changes = self._compare_constraints(a, b) |
|
346 |
if cdo.changes: |
|
347 |
yield cdo |
|
348 |
else: |
|
349 |
yield cdo |
|
350 |
||
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
351 |
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
|
352 |
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
|
353 |
if not self._test_table(nd[1]): |
0 | 354 |
continue |
355 |
tdo = DiffTable(change=nd[0], schema=schema, table=nd[1]) |
|
356 |
if nd[0] == '*': |
|
357 |
# columns |
|
358 |
src_columns = src_tables[nd[1]].columns |
|
359 |
dst_columns = dst_tables[nd[1]].columns |
|
360 |
for cdo in self._diff_columns(schema, nd[1], src_columns, dst_columns): |
|
361 |
if tdo: |
|
362 |
yield tdo |
|
363 |
tdo = None |
|
364 |
yield cdo |
|
365 |
# constraints |
|
366 |
src_constraints = src_tables[nd[1]].constraints |
|
367 |
dst_constraints = dst_tables[nd[1]].constraints |
|
368 |
for cdo in self._diff_constraints(schema, nd[1], src_constraints, dst_constraints): |
|
369 |
if tdo: |
|
370 |
yield tdo |
|
371 |
tdo = None |
|
372 |
yield cdo |
|
373 |
else: |
|
374 |
yield tdo |
|
375 |
||
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
|
376 |
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
|
377 |
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
|
378 |
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
|
379 |
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
|
380 |
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
|
381 |
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
|
382 |
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
|
383 |
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
|
384 |
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
|
385 |
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
|
386 |
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
|
387 |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
388 |
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
|
389 |
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
|
390 |
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
|
391 |
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
|
392 |
# compare function body and result |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
393 |
a = src_functions[nd[1]] |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
394 |
b = dst_functions[nd[1]] |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
395 |
fdo.changes = self._compare_functions(a, b) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
396 |
if fdo.changes: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
397 |
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
|
398 |
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
|
399 |
# 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
|
400 |
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
|
401 |
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
|
402 |
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
|
403 |
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
|
404 |
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
|
405 |
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
|
406 |
yield ado |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
407 |
else: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
408 |
yield fdo |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
409 |
|
0 | 410 |
def iter_diff(self): |
411 |
'''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
|
412 |
|
0 | 413 |
Yields one line at the time. Each line is in form of object |
414 |
iherited from DiffBase. This object contains all information |
|
415 |
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
|
416 |
|
0 | 417 |
''' |
418 |
src_schemas = self.src.schemas |
|
419 |
dst_schemas = self.dst.schemas |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
420 |
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
|
421 |
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
|
422 |
for nd in self._diff_names(src, dst): |
0 | 423 |
sdo = DiffSchema(change=nd[0], schema=nd[1]) |
424 |
if nd[0] == '*': |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
425 |
# tables |
0 | 426 |
src_tables = src_schemas[nd[1]].tables |
427 |
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
|
428 |
for tdo in self._diff_tables(nd[1], src_tables, dst_tables): |
0 | 429 |
if sdo: |
430 |
yield sdo |
|
431 |
sdo = None |
|
432 |
yield tdo |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
433 |
# functions |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
434 |
src_functions = src_schemas[nd[1]].functions |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
435 |
dst_functions = dst_schemas[nd[1]].functions |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
436 |
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
|
437 |
if sdo: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
438 |
yield sdo |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
439 |
sdo = None |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
440 |
yield fdo |
0 | 441 |
else: |
442 |
yield sdo |
|
443 |
||
444 |
def print_diff(self): |
|
445 |
'''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
|
446 |
|
0 | 447 |
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
|
448 |
|
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
449 |
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
|
450 |
|
0 | 451 |
''' |
452 |
for ln in self.iter_diff(): |
|
453 |
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
|
454 |
|
0 | 455 |
def print_patch(self): |
456 |
'''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
|
457 |
|
0 | 458 |
Supports table drop, add, column drop, add and following |
459 |
changes of columns: |
|
460 |
- type |
|
461 |
- set/remove not null |
|
462 |
- 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
|
463 |
|
0 | 464 |
This is experimental, not tested very much. |
465 |
Do not use without checking the commands. |
|
466 |
Even if it works as intended, it can cause table lock ups |
|
467 |
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
|
468 |
|
0 | 469 |
''' |
470 |
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
|
471 |
patch = ln.format_patch() |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
472 |
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
|
473 |
print('\n'.join(patch)) |
0 | 474 |
|
475 |
def filter_schemas(self, include=[], exclude=[]): |
|
476 |
'''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
|
477 |
|
0 | 478 |
include (list) -- if not empty, consider only these schemas for diff |
479 |
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
|
480 |
|
0 | 481 |
Order: include, exclude |
482 |
include=[] means include everything |
|
61
703bba757605
PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
483 |
|
703bba757605
PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
484 |
Raises: |
703bba757605
PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
485 |
PgDiffError: when schema from include list is not found in src db |
703bba757605
PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
486 |
|
0 | 487 |
''' |
61
703bba757605
PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
488 |
for schema in include: |
703bba757605
PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
489 |
self._check_schema_exist(schema) |
0 | 490 |
self.include_schemas.clear() |
491 |
self.include_schemas.update(include) |
|
492 |
self.exclude_schemas.clear() |
|
493 |
self.exclude_schemas.update(exclude) |
|
494 |
||
495 |
||
496 |
def filter_tables(self, include=[], exclude=[]): |
|
497 |
self.include_tables.clear() |
|
498 |
self.include_tables.update(include) |
|
499 |
self.exclude_tables.clear() |
|
500 |
self.exclude_tables.update(exclude) |
|
501 |
||
61
703bba757605
PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
502 |
|
703bba757605
PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
503 |
def _check_schema_exist(self, schema): |
703bba757605
PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
504 |
if not schema in self.src.schemas: |
703bba757605
PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
505 |
raise PgDiffError('Schema "%s" not found in source database.' % schema) |
703bba757605
PgDiff: check schema existance.
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
506 |