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