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