pgtoolkit/pgdiff.py
changeset 87 3ef717ee9253
parent 85 11a282e23e0d
child 94 a10f553e6f6a
equal deleted inserted replaced
86:b61b54aa9f96 87:3ef717ee9253
   250         if self.change == '-':
   250         if self.change == '-':
   251             out = [q_drop]
   251             out = [q_drop]
   252         return out
   252         return out
   253 
   253 
   254 
   254 
       
   255 class DiffIndex(DiffBase):
       
   256     def __init__(self, change, schema, table, index, definition, changes=None):
       
   257         DiffBase.__init__(self)
       
   258         self.level = 2
       
   259         self.type = 'index'
       
   260         self.change = change
       
   261         self.schema = schema
       
   262         self.table = table
       
   263         self.index = index
       
   264         self.name = index
       
   265         self.definition = definition
       
   266         self.changes = changes
       
   267 
       
   268     def format_patch(self):
       
   269         q_drop = 'DROP INDEX %s;' % (self.index,)
       
   270         q_add = '%s;' % (self.definition,)
       
   271         if self.change == '*':
       
   272             out = [q_drop, q_add]
       
   273         if self.change == '+':
       
   274             out = [q_add]
       
   275         if self.change == '-':
       
   276             out = [q_drop]
       
   277         return out
       
   278 
       
   279 
   255 class DiffType(DiffBase):
   280 class DiffType(DiffBase):
   256     def __init__(self, change, schema, name):
   281     def __init__(self, change, schema, name):
   257         DiffBase.__init__(self)
   282         DiffBase.__init__(self)
   258         self.level = 1
   283         self.level = 1
   259         self.type = 'type'
   284         self.type = 'type'
   317             diff.append(('type', a.type, b.type))
   342             diff.append(('type', a.type, b.type))
   318         if a.definition != b.definition:
   343         if a.definition != b.definition:
   319             diff.append(('definition', a.definition, b.definition))
   344             diff.append(('definition', a.definition, b.definition))
   320         return diff
   345         return diff
   321 
   346 
       
   347     def _compare_indexes(self, a, b):
       
   348         diff = []
       
   349         if a.definition != b.definition:
       
   350             diff.append(('definition', a.definition, b.definition))
       
   351         return diff
       
   352 
   322     def _compare_functions(self, a, b):
   353     def _compare_functions(self, a, b):
   323         diff = []
   354         diff = []
   324         if a.result != b.result:
   355         if a.result != b.result:
   325             diff.append(('result', a.result, b.result))
   356             diff.append(('result', a.result, b.result))
   326         # function source may differ in newlines (\n vs \r\n)
   357         # function source may differ in newlines (\n vs \r\n)
   384                 cdo.changes = self._compare_constraints(a, b)
   415                 cdo.changes = self._compare_constraints(a, b)
   385                 if cdo.changes:
   416                 if cdo.changes:
   386                     yield cdo
   417                     yield cdo
   387             else:
   418             else:
   388                 yield cdo
   419                 yield cdo
       
   420 
       
   421     def _diff_indexes(self, schema, table, src_indexes, dst_indexes):
       
   422         for nd in self._diff_names(src_indexes, dst_indexes):
       
   423             if nd[1] in dst_indexes:
       
   424                 dst_definition = dst_indexes[nd[1]].definition
       
   425             else:
       
   426                 dst_definition = None
       
   427             ido = DiffIndex(change=nd[0], schema=schema, table=table, index=nd[1],
       
   428                 definition=dst_definition)
       
   429             if nd[0] == '*':
       
   430                 a = src_indexes[nd[1]]
       
   431                 b = dst_indexes[nd[1]]
       
   432                 ido.changes = self._compare_indexes(a, b)
       
   433                 if ido.changes:
       
   434                     yield ido
       
   435             else:
       
   436                 yield ido
   389 
   437 
   390     def _diff_tables(self, schema, src_tables, dst_tables):
   438     def _diff_tables(self, schema, src_tables, dst_tables):
   391         for nd in self._diff_names(src_tables, dst_tables):
   439         for nd in self._diff_names(src_tables, dst_tables):
   392             if not self._test_table(nd[1]):
   440             if not self._test_table(nd[1]):
   393                 continue
   441                 continue
   407                 for cdo in self._diff_constraints(schema, nd[1], src_constraints, dst_constraints):
   455                 for cdo in self._diff_constraints(schema, nd[1], src_constraints, dst_constraints):
   408                     if tdo:
   456                     if tdo:
   409                         yield tdo
   457                         yield tdo
   410                         tdo = None
   458                         tdo = None
   411                     yield cdo
   459                     yield cdo
       
   460                 # indexes
       
   461                 src_indexes = src_tables[nd[1]].indexes
       
   462                 dst_indexes = dst_tables[nd[1]].indexes
       
   463                 for ido in self._diff_indexes(schema, nd[1], src_indexes, dst_indexes):
       
   464                     if tdo:
       
   465                         yield tdo
       
   466                         tdo = None
       
   467                     yield ido
   412             else:
   468             else:
   413                 yield tdo
   469                 yield tdo
   414 
   470 
   415     def _diff_arguments(self, schema, function, src_args, dst_args):
   471     def _diff_arguments(self, schema, function, src_args, dst_args):
   416         for nd in self._diff_names(src_args, dst_args):
   472         for nd in self._diff_names(src_args, dst_args):