114 self.type = 'table' |
114 self.type = 'table' |
115 self.change = change |
115 self.change = change |
116 self.schema = schema |
116 self.schema = schema |
117 self.table = table |
117 self.table = table |
118 self.name = table |
118 self.name = table |
|
119 |
|
120 |
|
121 class DiffFunction(DiffBase): |
|
122 def __init__(self, change, schema, function): |
|
123 DiffBase.__init__(self) |
|
124 self.level = 1 |
|
125 self.type = 'function' |
|
126 self.change = change |
|
127 self.schema = schema |
|
128 self.function = function |
|
129 self.name = function |
|
130 |
|
131 def _formatchanges(self): |
|
132 res = [] |
|
133 for x in self.changes: |
|
134 type, a, b = x |
|
135 if type == 'source': |
|
136 s = 'Changed source.' |
|
137 else: |
|
138 s = ''.join(['Changed ', type, ' from ', |
|
139 highlight(1,15), a, highlight(0), ' to ', |
|
140 highlight(1,15), b, highlight(0), '.']) |
|
141 res.append(s) |
|
142 return ' '.join(res) |
119 |
143 |
120 |
144 |
121 class DiffColumn(DiffBase): |
145 class DiffColumn(DiffBase): |
122 ALTER_COMMANDS = { |
146 ALTER_COMMANDS = { |
123 '+' : 'ADD', |
147 '+' : 'ADD', |
221 yield ('-', x) |
245 yield ('-', x) |
222 for x in dst: |
246 for x in dst: |
223 if x not in src: |
247 if x not in src: |
224 yield ('+', x) |
248 yield ('+', x) |
225 |
249 |
|
250 def _compare_functions(self, a, b): |
|
251 diff = [] |
|
252 if a.arguments != b.arguments: |
|
253 diff.append(('args', a.arguments, b.arguments)) |
|
254 if a.result != b.result: |
|
255 diff.append(('result', a.result, b.result)) |
|
256 if a.source != b.source: |
|
257 diff.append(('source', a.source, b.source)) |
|
258 return diff |
|
259 |
226 def _compare_columns(self, a, b): |
260 def _compare_columns(self, a, b): |
227 diff = [] |
261 diff = [] |
228 if a.type != b.type: |
262 if a.type != b.type: |
229 diff.append(('type', a.type, b.type)) |
263 diff.append(('type', a.type, b.type)) |
230 if a.notnull != b.notnull: |
264 if a.notnull != b.notnull: |
275 if cdo.changes: |
309 if cdo.changes: |
276 yield cdo |
310 yield cdo |
277 else: |
311 else: |
278 yield cdo |
312 yield cdo |
279 |
313 |
280 def _difftables(self, schema, src_tables, dst_tables): |
314 def _diff_tables(self, schema, src_tables, dst_tables): |
281 for nd in self._diff_names(src_tables, dst_tables): |
315 for nd in self._diff_names(src_tables, dst_tables): |
282 if not self._test_table(nd[1]): |
316 if not self._test_table(nd[1]): |
283 continue |
317 continue |
284 tdo = DiffTable(change=nd[0], schema=schema, table=nd[1]) |
318 tdo = DiffTable(change=nd[0], schema=schema, table=nd[1]) |
285 if nd[0] == '*': |
319 if nd[0] == '*': |
300 tdo = None |
334 tdo = None |
301 yield cdo |
335 yield cdo |
302 else: |
336 else: |
303 yield tdo |
337 yield tdo |
304 |
338 |
|
339 def _diff_functions(self, schema, src_functions, dst_functions): |
|
340 for nd in self._diff_names(src_functions, dst_functions): |
|
341 fdo = DiffFunction(change=nd[0], schema=schema, function=nd[1]) |
|
342 if nd[0] == '*': |
|
343 # compare function body and arguments |
|
344 a = src_functions[nd[1]] |
|
345 b = dst_functions[nd[1]] |
|
346 fdo.changes = self._compare_functions(a, b) |
|
347 if fdo.changes: |
|
348 yield fdo |
|
349 else: |
|
350 yield fdo |
|
351 |
305 def iter_diff(self): |
352 def iter_diff(self): |
306 '''Return diff between src and dst database schema. |
353 '''Return diff between src and dst database schema. |
307 |
354 |
308 Yields one line at the time. Each line is in form of object |
355 Yields one line at the time. Each line is in form of object |
309 iherited from DiffBase. This object contains all information |
356 iherited from DiffBase. This object contains all information |
315 src = [x.name for x in src_schemas.values() if not x.system and self._test_schema(x.name)] |
362 src = [x.name for x in src_schemas.values() if not x.system and self._test_schema(x.name)] |
316 dst = [x.name for x in dst_schemas.values() if not x.system and self._test_schema(x.name)] |
363 dst = [x.name for x in dst_schemas.values() if not x.system and self._test_schema(x.name)] |
317 for nd in self._diff_names(src, dst): |
364 for nd in self._diff_names(src, dst): |
318 sdo = DiffSchema(change=nd[0], schema=nd[1]) |
365 sdo = DiffSchema(change=nd[0], schema=nd[1]) |
319 if nd[0] == '*': |
366 if nd[0] == '*': |
|
367 # tables |
320 src_tables = src_schemas[nd[1]].tables |
368 src_tables = src_schemas[nd[1]].tables |
321 dst_tables = dst_schemas[nd[1]].tables |
369 dst_tables = dst_schemas[nd[1]].tables |
322 for tdo in self._difftables(nd[1], src_tables, dst_tables): |
370 for tdo in self._diff_tables(nd[1], src_tables, dst_tables): |
323 if sdo: |
371 if sdo: |
324 yield sdo |
372 yield sdo |
325 sdo = None |
373 sdo = None |
326 yield tdo |
374 yield tdo |
|
375 # functions |
|
376 src_functions = src_schemas[nd[1]].functions |
|
377 dst_functions = dst_schemas[nd[1]].functions |
|
378 for fdo in self._diff_functions(nd[1], src_functions, dst_functions): |
|
379 if sdo: |
|
380 yield sdo |
|
381 sdo = None |
|
382 yield fdo |
327 else: |
383 else: |
328 yield sdo |
384 yield sdo |
329 |
385 |
330 def print_diff(self): |
386 def print_diff(self): |
331 '''Print diff between src and dst database schema. |
387 '''Print diff between src and dst database schema. |