author | Radek Brich <radek.brich@devl.cz> |
Thu, 24 Jan 2013 17:11:17 +0100 | |
changeset 58 | 0bcc13460dae |
parent 53 | 4a049a5af657 |
child 59 | 65efd0c6919f |
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 |
||
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
121 |
class DiffFunction(DiffBase): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
122 |
def __init__(self, change, schema, function): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
123 |
DiffBase.__init__(self) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
124 |
self.level = 1 |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
125 |
self.type = 'function' |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
126 |
self.change = change |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
127 |
self.schema = schema |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
128 |
self.function = function |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
129 |
self.name = function |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
130 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
131 |
def _formatchanges(self): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
132 |
res = [] |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
133 |
for x in self.changes: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
134 |
type, a, b = x |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
135 |
if type == 'source': |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
136 |
s = 'Changed source.' |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
137 |
else: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
138 |
s = ''.join(['Changed ', type, ' from ', |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
139 |
highlight(1,15), a, highlight(0), ' to ', |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
140 |
highlight(1,15), b, highlight(0), '.']) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
141 |
res.append(s) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
142 |
return ' '.join(res) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
143 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
144 |
|
0 | 145 |
class DiffColumn(DiffBase): |
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
146 |
ALTER_COMMANDS = { |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
147 |
'+' : 'ADD', |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
148 |
'-' : 'DROP', |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
149 |
'*' : 'ALTER', |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
150 |
} |
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
|
151 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
152 |
def __init__(self, change, schema, table, column, columntype, columndefault, changes=None): |
0 | 153 |
DiffBase.__init__(self) |
154 |
self.level = 2 |
|
155 |
self.type = 'column' |
|
156 |
self.change = change |
|
157 |
self.schema = schema |
|
158 |
self.table = table |
|
159 |
self.column = column |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
160 |
self.columntype = columntype |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
161 |
self.columndefault = columndefault |
0 | 162 |
self.name = column |
163 |
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
|
164 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
165 |
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
|
166 |
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
|
167 |
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
|
168 |
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
|
169 |
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
|
170 |
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
|
171 |
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
|
172 |
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
|
173 |
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
|
174 |
self.schema, |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
175 |
self.table, |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
176 |
self.ALTER_COMMANDS[self.change], |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
177 |
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
|
178 |
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
|
179 |
)] |
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
180 |
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
|
181 |
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
|
182 |
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
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
)] |
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
187 |
return out |
0 | 188 |
|
189 |
||
190 |
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
|
191 |
def __init__(self, change, schema, table, constraint, definition, changes=None): |
0 | 192 |
DiffBase.__init__(self) |
193 |
self.level = 2 |
|
194 |
self.type = 'constraint' |
|
195 |
self.change = change |
|
196 |
self.schema = schema |
|
197 |
self.table = table |
|
198 |
self.constraint = constraint |
|
199 |
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
|
200 |
self.definition = definition |
0 | 201 |
self.changes = changes |
202 |
||
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
|
203 |
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
|
204 |
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
|
205 |
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
|
206 |
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
|
207 |
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
|
208 |
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
|
209 |
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
|
210 |
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
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
|
0 | 215 |
|
216 |
class PgDiff: |
|
217 |
def __init__(self, srcbrowser=None, dstbrowser=None): |
|
218 |
self.allowcolor = False |
|
219 |
self.src = srcbrowser |
|
220 |
self.dst = dstbrowser |
|
221 |
self.include_schemas = set() # if not empty, consider only these schemas for diff |
|
222 |
self.exclude_schemas = set() # exclude these schemas from diff |
|
223 |
self.include_tables = set() |
|
224 |
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
|
225 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
226 |
def _test_schema(self, schema): |
0 | 227 |
if self.include_schemas and schema not in self.include_schemas: |
228 |
return False |
|
229 |
if schema in self.exclude_schemas: |
|
230 |
return False |
|
231 |
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
|
232 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
233 |
def _test_table(self, table): |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
234 |
if self.include_tables and table not in self.include_tables: |
0 | 235 |
return False |
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
236 |
if table in self.exclude_tables: |
0 | 237 |
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
|
238 |
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
|
239 |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
240 |
def _diff_names(self, src, dst): |
0 | 241 |
for x in src: |
242 |
if x in dst: |
|
243 |
yield ('*', x) |
|
244 |
else: |
|
245 |
yield ('-', x) |
|
246 |
for x in dst: |
|
247 |
if x not in src: |
|
248 |
yield ('+', x) |
|
249 |
||
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
250 |
def _compare_functions(self, a, b): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
251 |
diff = [] |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
252 |
if a.arguments != b.arguments: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
253 |
diff.append(('args', a.arguments, b.arguments)) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
254 |
if a.result != b.result: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
255 |
diff.append(('result', a.result, b.result)) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
256 |
if a.source != b.source: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
257 |
diff.append(('source', a.source, b.source)) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
258 |
return diff |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
259 |
|
0 | 260 |
def _compare_columns(self, a, b): |
261 |
diff = [] |
|
262 |
if a.type != b.type: |
|
263 |
diff.append(('type', a.type, b.type)) |
|
264 |
if a.notnull != b.notnull: |
|
265 |
diff.append(('notnull', a.notnull, b.notnull)) |
|
266 |
if a.default != b.default: |
|
267 |
diff.append(('default', a.default, b.default)) |
|
268 |
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
|
269 |
|
0 | 270 |
def _compare_constraints(self, a, b): |
271 |
diff = [] |
|
272 |
if a.type != b.type: |
|
273 |
diff.append(('type', a.type, b.type)) |
|
274 |
if a.definition != b.definition: |
|
275 |
diff.append(('definition', a.definition, b.definition)) |
|
276 |
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
|
277 |
|
0 | 278 |
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
|
279 |
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
|
280 |
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
|
281 |
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
|
282 |
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
|
283 |
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
|
284 |
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
|
285 |
dst_default = None |
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
286 |
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
|
287 |
columntype=dst_type, columndefault=dst_default) |
0 | 288 |
if nd[0] == '*': |
289 |
a = src_columns[nd[1]] |
|
290 |
b = dst_columns[nd[1]] |
|
291 |
cdo.changes = self._compare_columns(a, b) |
|
292 |
if cdo.changes: |
|
293 |
yield cdo |
|
294 |
else: |
|
295 |
yield cdo |
|
296 |
||
297 |
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
|
298 |
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
|
299 |
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
|
300 |
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
|
301 |
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
|
302 |
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
|
303 |
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
|
304 |
definition=dst_definition) |
0 | 305 |
if nd[0] == '*': |
306 |
a = src_constraints[nd[1]] |
|
307 |
b = dst_constraints[nd[1]] |
|
308 |
cdo.changes = self._compare_constraints(a, b) |
|
309 |
if cdo.changes: |
|
310 |
yield cdo |
|
311 |
else: |
|
312 |
yield cdo |
|
313 |
||
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
314 |
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
|
315 |
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
|
316 |
if not self._test_table(nd[1]): |
0 | 317 |
continue |
318 |
tdo = DiffTable(change=nd[0], schema=schema, table=nd[1]) |
|
319 |
if nd[0] == '*': |
|
320 |
# columns |
|
321 |
src_columns = src_tables[nd[1]].columns |
|
322 |
dst_columns = dst_tables[nd[1]].columns |
|
323 |
for cdo in self._diff_columns(schema, nd[1], src_columns, dst_columns): |
|
324 |
if tdo: |
|
325 |
yield tdo |
|
326 |
tdo = None |
|
327 |
yield cdo |
|
328 |
# constraints |
|
329 |
src_constraints = src_tables[nd[1]].constraints |
|
330 |
dst_constraints = dst_tables[nd[1]].constraints |
|
331 |
for cdo in self._diff_constraints(schema, nd[1], src_constraints, dst_constraints): |
|
332 |
if tdo: |
|
333 |
yield tdo |
|
334 |
tdo = None |
|
335 |
yield cdo |
|
336 |
else: |
|
337 |
yield tdo |
|
338 |
||
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
339 |
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
|
340 |
for nd in self._diff_names(src_functions, dst_functions): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
341 |
fdo = DiffFunction(change=nd[0], schema=schema, function=nd[1]) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
342 |
if nd[0] == '*': |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
343 |
# compare function body and arguments |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
344 |
a = src_functions[nd[1]] |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
345 |
b = dst_functions[nd[1]] |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
346 |
fdo.changes = self._compare_functions(a, b) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
347 |
if fdo.changes: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
348 |
yield fdo |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
349 |
else: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
350 |
yield fdo |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
351 |
|
0 | 352 |
def iter_diff(self): |
353 |
'''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
|
354 |
|
0 | 355 |
Yields one line at the time. Each line is in form of object |
356 |
iherited from DiffBase. This object contains all information |
|
357 |
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
|
358 |
|
0 | 359 |
''' |
360 |
src_schemas = self.src.schemas |
|
361 |
dst_schemas = self.dst.schemas |
|
47
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
362 |
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
|
363 |
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
|
364 |
for nd in self._diff_names(src, dst): |
0 | 365 |
sdo = DiffSchema(change=nd[0], schema=nd[1]) |
366 |
if nd[0] == '*': |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
367 |
# tables |
0 | 368 |
src_tables = src_schemas[nd[1]].tables |
369 |
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
|
370 |
for tdo in self._diff_tables(nd[1], src_tables, dst_tables): |
0 | 371 |
if sdo: |
372 |
yield sdo |
|
373 |
sdo = None |
|
374 |
yield tdo |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
375 |
# functions |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
376 |
src_functions = src_schemas[nd[1]].functions |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
377 |
dst_functions = dst_schemas[nd[1]].functions |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
378 |
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
|
379 |
if sdo: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
380 |
yield sdo |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
381 |
sdo = None |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
53
diff
changeset
|
382 |
yield fdo |
0 | 383 |
else: |
384 |
yield sdo |
|
385 |
||
386 |
def print_diff(self): |
|
387 |
'''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
|
388 |
|
0 | 389 |
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
|
390 |
|
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 |
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
|
392 |
|
0 | 393 |
''' |
394 |
for ln in self.iter_diff(): |
|
395 |
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
|
396 |
|
0 | 397 |
def print_patch(self): |
398 |
'''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
|
399 |
|
0 | 400 |
Supports table drop, add, column drop, add and following |
401 |
changes of columns: |
|
402 |
- type |
|
403 |
- set/remove not null |
|
404 |
- 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
|
405 |
|
0 | 406 |
This is experimental, not tested very much. |
407 |
Do not use without checking the commands. |
|
408 |
Even if it works as intended, it can cause table lock ups |
|
409 |
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
|
410 |
|
0 | 411 |
''' |
412 |
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
|
413 |
patch = ln.format_patch() |
bb8c729ae6ce
PgDiff: add partial support for SQL patch.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
414 |
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
|
415 |
print('\n'.join(patch)) |
0 | 416 |
|
417 |
def filter_schemas(self, include=[], exclude=[]): |
|
418 |
'''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
|
419 |
|
0 | 420 |
include (list) -- if not empty, consider only these schemas for diff |
421 |
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
|
422 |
|
0 | 423 |
Order: include, exclude |
424 |
include=[] means include everything |
|
425 |
''' |
|
426 |
self.include_schemas.clear() |
|
427 |
self.include_schemas.update(include) |
|
428 |
self.exclude_schemas.clear() |
|
429 |
self.exclude_schemas.update(exclude) |
|
430 |
||
431 |
||
432 |
def filter_tables(self, include=[], exclude=[]): |
|
433 |
self.include_tables.clear() |
|
434 |
self.include_tables.update(include) |
|
435 |
self.exclude_tables.clear() |
|
436 |
self.exclude_tables.update(exclude) |
|
437 |