author | Radek Brich <radek.brich@devl.cz> |
Thu, 13 Dec 2012 16:07:13 +0100 | |
changeset 54 | 291473ab847c |
parent 53 | 4a049a5af657 |
child 58 | 0bcc13460dae |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
# |
|
3 |
# PgDiff - capture differences of database metadata |
|
4 |
# |
|
5 |
# Depends on PgBrowser |
|
6 |
# |
|
7 |
# Copyright (c) 2011 Radek Brich <radek.brich@devl.cz> |
|
8 |
# |
|
9 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
10 |
# of this software and associated documentation files (the "Software"), to deal |
|
11 |
# in the Software without restriction, including without limitation the rights |
|
12 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
13 |
# copies of the Software, and to permit persons to whom the Software is |
|
14 |
# furnished to do so, subject to the following conditions: |
|
15 |
# |
|
16 |
# The above copyright notice and this permission notice shall be included in |
|
17 |
# all copies or substantial portions of the Software. |
|
18 |
# |
|
19 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
20 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
21 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
22 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
23 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
24 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
25 |
# THE SOFTWARE. |
|
26 |
||
27 |
||
9
2fcc8ef0b97d
Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
28 |
from pgtoolkit.highlight import * |
0 | 29 |
|
30 |
||
31 |
class DiffBase: |
|
6 | 32 |
COLORS = { |
33 |
'+' : BOLD | GREEN, |
|
34 |
'-' : BOLD | RED, |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
35 |
'*' : BOLD | YELLOW, |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
36 |
} |
53
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
37 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
38 |
COMMANDS = { |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
39 |
'+' : 'CREATE', |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
40 |
'-' : 'DROP', |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
41 |
'*' : 'ALTER', |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
42 |
} |
53
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
43 |
|
0 | 44 |
def __init__(self): |
45 |
self.changes = None |
|
46 |
||
47 |
def format(self): |
|
48 |
out = [' ' * self.level] |
|
49 |
||
6 | 50 |
out.append(highlight(1, self.COLORS[self.change])) |
0 | 51 |
out.append(self.change) |
53
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
52 |
|
0 | 53 |
out += [' ', self.type, ' ', self.name, highlight(0)] |
53
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
54 |
|
0 | 55 |
if self.changes: |
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
56 |
out += [highlight(1, WHITE), ' (', self._formatchanges(), ')', highlight(0)] |
0 | 57 |
|
58 |
return ''.join(out) |
|
59 |
||
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
60 |
def _formatnotnull(self, notnull): |
0 | 61 |
if notnull: |
62 |
return 'NOT NULL' |
|
63 |
else: |
|
64 |
return None |
|
53
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
65 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
66 |
def _formatchanges(self): |
0 | 67 |
res = [] |
68 |
for x in self.changes: |
|
69 |
type, a, b = x |
|
70 |
if type == 'notnull': |
|
71 |
type = '' |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
72 |
a = self._formatnotnull(a) |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
73 |
b = self._formatnotnull(b) |
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
|
74 |
|
0 | 75 |
if a and b: |
76 |
s = ''.join(['Changed ', type, ' from ', |
|
77 |
highlight(1,15), a, highlight(0), ' to ', |
|
78 |
highlight(1,15), b, highlight(0), '.']) |
|
79 |
elif a and not b: |
|
80 |
l = ['Removed '] |
|
81 |
if type: |
|
82 |
l += [type, ' '] |
|
83 |
l += [highlight(1,15), a, highlight(0), '.'] |
|
84 |
s = ''.join(l) |
|
85 |
elif b and not a: |
|
86 |
l = ['Added '] |
|
87 |
if type: |
|
88 |
l += [type, ' '] |
|
89 |
l += [highlight(1,15), b, highlight(0), '.'] |
|
90 |
s = ''.join(l) |
|
91 |
res.append(s) |
|
92 |
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
|
93 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
94 |
def format_patch(self): |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
95 |
if self.change == '*' and self.type in ('schema', 'table'): |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
96 |
return None |
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
|
97 |
return ['%s %s %s;' % (self.COMMANDS[self.change], self.type.upper(), self.name)] |
0 | 98 |
|
99 |
||
100 |
class DiffSchema(DiffBase): |
|
101 |
def __init__(self, change, schema): |
|
102 |
DiffBase.__init__(self) |
|
103 |
self.level = 0 |
|
104 |
self.type = 'schema' |
|
105 |
self.change = change |
|
106 |
self.schema = schema |
|
107 |
self.name = schema |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
108 |
|
0 | 109 |
|
110 |
class DiffTable(DiffBase): |
|
111 |
def __init__(self, change, schema, table): |
|
112 |
DiffBase.__init__(self) |
|
113 |
self.level = 1 |
|
114 |
self.type = 'table' |
|
115 |
self.change = change |
|
116 |
self.schema = schema |
|
117 |
self.table = table |
|
118 |
self.name = table |
|
119 |
||
120 |
||
121 |
class DiffColumn(DiffBase): |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
122 |
ALTER_COMMANDS = { |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
123 |
'+' : 'ADD', |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
124 |
'-' : 'DROP', |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
125 |
'*' : 'ALTER', |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
126 |
} |
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
|
127 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
128 |
def __init__(self, change, schema, table, column, columntype, columndefault, changes=None): |
0 | 129 |
DiffBase.__init__(self) |
130 |
self.level = 2 |
|
131 |
self.type = 'column' |
|
132 |
self.change = change |
|
133 |
self.schema = schema |
|
134 |
self.table = table |
|
135 |
self.column = column |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
136 |
self.columntype = columntype |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
137 |
self.columndefault = columndefault |
0 | 138 |
self.name = column |
139 |
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
|
140 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
141 |
def format_patch(self): |
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
|
142 |
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
|
143 |
type_statement = ' 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
|
144 |
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
|
145 |
type_statement = '' |
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
146 |
if self.columntype is not 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
|
147 |
type_statement += ' ' + self.columntype; |
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
148 |
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
|
149 |
out += ['ALTER TABLE %s.%s %s COLUMN %s%s;' % ( |
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
150 |
self.schema, |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
151 |
self.table, |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
152 |
self.ALTER_COMMANDS[self.change], |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
153 |
self.name, |
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
|
154 |
type_statement |
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
155 |
)] |
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
156 |
if self.columndefault: |
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
|
157 |
out += ['ALTER TABLE %s.%s ALTER COLUMN %s SET DEFAULT %s;' % ( |
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
158 |
self.schema, |
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
159 |
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
|
160 |
self.name, |
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
161 |
self.columndefault |
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
162 |
)] |
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
163 |
return out |
0 | 164 |
|
165 |
||
166 |
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
|
167 |
def __init__(self, change, schema, table, constraint, definition, changes=None): |
0 | 168 |
DiffBase.__init__(self) |
169 |
self.level = 2 |
|
170 |
self.type = 'constraint' |
|
171 |
self.change = change |
|
172 |
self.schema = schema |
|
173 |
self.table = table |
|
174 |
self.constraint = constraint |
|
175 |
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
|
176 |
self.definition = definition |
0 | 177 |
self.changes = changes |
178 |
||
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
|
179 |
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
|
180 |
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
|
181 |
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
|
182 |
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
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
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
|
187 |
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
|
188 |
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
|
189 |
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
|
190 |
|
0 | 191 |
|
192 |
class PgDiff: |
|
193 |
def __init__(self, srcbrowser=None, dstbrowser=None): |
|
194 |
self.allowcolor = False |
|
195 |
self.src = srcbrowser |
|
196 |
self.dst = dstbrowser |
|
197 |
self.include_schemas = set() # if not empty, consider only these schemas for diff |
|
198 |
self.exclude_schemas = set() # exclude these schemas from diff |
|
199 |
self.include_tables = set() |
|
200 |
self.exclude_tables = set() |
|
53
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
201 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
202 |
def _test_schema(self, schema): |
0 | 203 |
if self.include_schemas and schema not in self.include_schemas: |
204 |
return False |
|
205 |
if schema in self.exclude_schemas: |
|
206 |
return False |
|
207 |
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
|
208 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
209 |
def _test_table(self, table): |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
210 |
if self.include_tables and table not in self.include_tables: |
0 | 211 |
return False |
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
212 |
if table in self.exclude_tables: |
0 | 213 |
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
|
214 |
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
|
215 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
216 |
def _diff_names(self, src, dst): |
0 | 217 |
for x in src: |
218 |
if x in dst: |
|
219 |
yield ('*', x) |
|
220 |
else: |
|
221 |
yield ('-', x) |
|
222 |
for x in dst: |
|
223 |
if x not in src: |
|
224 |
yield ('+', x) |
|
225 |
||
226 |
def _compare_columns(self, a, b): |
|
227 |
diff = [] |
|
228 |
if a.type != b.type: |
|
229 |
diff.append(('type', a.type, b.type)) |
|
230 |
if a.notnull != b.notnull: |
|
231 |
diff.append(('notnull', a.notnull, b.notnull)) |
|
232 |
if a.default != b.default: |
|
233 |
diff.append(('default', a.default, b.default)) |
|
234 |
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
|
235 |
|
0 | 236 |
def _compare_constraints(self, a, b): |
237 |
diff = [] |
|
238 |
if a.type != b.type: |
|
239 |
diff.append(('type', a.type, b.type)) |
|
240 |
if a.definition != b.definition: |
|
241 |
diff.append(('definition', a.definition, b.definition)) |
|
242 |
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
|
243 |
|
0 | 244 |
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
|
245 |
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
|
246 |
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
|
247 |
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
|
248 |
dst_default = dst_columns[nd[1]].default |
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 |
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
|
250 |
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
|
251 |
dst_default = None |
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
252 |
cdo = DiffColumn(change=nd[0], schema=schema, table=table, column=nd[1], |
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
|
253 |
columntype=dst_type, columndefault=dst_default) |
0 | 254 |
if nd[0] == '*': |
255 |
a = src_columns[nd[1]] |
|
256 |
b = dst_columns[nd[1]] |
|
257 |
cdo.changes = self._compare_columns(a, b) |
|
258 |
if cdo.changes: |
|
259 |
yield cdo |
|
260 |
else: |
|
261 |
yield cdo |
|
262 |
||
263 |
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
|
264 |
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
|
265 |
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
|
266 |
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
|
267 |
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
|
268 |
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
|
269 |
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
|
270 |
definition=dst_definition) |
0 | 271 |
if nd[0] == '*': |
272 |
a = src_constraints[nd[1]] |
|
273 |
b = dst_constraints[nd[1]] |
|
274 |
cdo.changes = self._compare_constraints(a, b) |
|
275 |
if cdo.changes: |
|
276 |
yield cdo |
|
277 |
else: |
|
278 |
yield cdo |
|
279 |
||
280 |
def _difftables(self, schema, src_tables, dst_tables): |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
281 |
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
|
282 |
if not self._test_table(nd[1]): |
0 | 283 |
continue |
284 |
tdo = DiffTable(change=nd[0], schema=schema, table=nd[1]) |
|
285 |
if nd[0] == '*': |
|
286 |
# columns |
|
287 |
src_columns = src_tables[nd[1]].columns |
|
288 |
dst_columns = dst_tables[nd[1]].columns |
|
289 |
for cdo in self._diff_columns(schema, nd[1], src_columns, dst_columns): |
|
290 |
if tdo: |
|
291 |
yield tdo |
|
292 |
tdo = None |
|
293 |
yield cdo |
|
294 |
# constraints |
|
295 |
src_constraints = src_tables[nd[1]].constraints |
|
296 |
dst_constraints = dst_tables[nd[1]].constraints |
|
297 |
for cdo in self._diff_constraints(schema, nd[1], src_constraints, dst_constraints): |
|
298 |
if tdo: |
|
299 |
yield tdo |
|
300 |
tdo = None |
|
301 |
yield cdo |
|
302 |
else: |
|
303 |
yield tdo |
|
304 |
||
305 |
def iter_diff(self): |
|
306 |
'''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
|
307 |
|
0 | 308 |
Yields one line at the time. Each line is in form of object |
309 |
iherited from DiffBase. This object contains all information |
|
310 |
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
|
311 |
|
0 | 312 |
''' |
313 |
src_schemas = self.src.schemas |
|
314 |
dst_schemas = self.dst.schemas |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
315 |
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
|
316 |
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
|
317 |
for nd in self._diff_names(src, dst): |
0 | 318 |
sdo = DiffSchema(change=nd[0], schema=nd[1]) |
319 |
if nd[0] == '*': |
|
320 |
src_tables = src_schemas[nd[1]].tables |
|
321 |
dst_tables = dst_schemas[nd[1]].tables |
|
322 |
for tdo in self._difftables(nd[1], src_tables, dst_tables): |
|
323 |
if sdo: |
|
324 |
yield sdo |
|
325 |
sdo = None |
|
326 |
yield tdo |
|
327 |
else: |
|
328 |
yield sdo |
|
329 |
||
330 |
def print_diff(self): |
|
331 |
'''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
|
332 |
|
0 | 333 |
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
|
334 |
|
4a049a5af657
Update PgDiff: Support SQL patch for constraints. Fix changes of column default value.
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
335 |
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
|
336 |
|
0 | 337 |
''' |
338 |
for ln in self.iter_diff(): |
|
339 |
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
|
340 |
|
0 | 341 |
def print_patch(self): |
342 |
'''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
|
343 |
|
0 | 344 |
Supports table drop, add, column drop, add and following |
345 |
changes of columns: |
|
346 |
- type |
|
347 |
- set/remove not null |
|
348 |
- 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
|
349 |
|
0 | 350 |
This is experimental, not tested very much. |
351 |
Do not use without checking the commands. |
|
352 |
Even if it works as intended, it can cause table lock ups |
|
353 |
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
|
354 |
|
0 | 355 |
''' |
356 |
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
|
357 |
patch = ln.format_patch() |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
358 |
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
|
359 |
print('\n'.join(patch)) |
0 | 360 |
|
361 |
def filter_schemas(self, include=[], exclude=[]): |
|
362 |
'''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
|
363 |
|
0 | 364 |
include (list) -- if not empty, consider only these schemas for diff |
365 |
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
|
366 |
|
0 | 367 |
Order: include, exclude |
368 |
include=[] means include everything |
|
369 |
''' |
|
370 |
self.include_schemas.clear() |
|
371 |
self.include_schemas.update(include) |
|
372 |
self.exclude_schemas.clear() |
|
373 |
self.exclude_schemas.update(exclude) |
|
374 |
||
375 |
||
376 |
def filter_tables(self, include=[], exclude=[]): |
|
377 |
self.include_tables.clear() |
|
378 |
self.include_tables.update(include) |
|
379 |
self.exclude_tables.clear() |
|
380 |
self.exclude_tables.update(exclude) |
|
381 |