pgtoolkit/pgdiff.py
changeset 60 bb6b20106ff5
parent 59 65efd0c6919f
child 61 703bba757605
equal deleted inserted replaced
59:65efd0c6919f 60:bb6b20106ff5
    63         else:
    63         else:
    64             return None
    64             return None
    65 
    65 
    66     def _formatchanges(self):
    66     def _formatchanges(self):
    67         res = []
    67         res = []
    68         for x in self.changes:
    68         for type, a, b in self.changes:
    69             type, a, b = x
       
    70             if type == 'notnull':
    69             if type == 'notnull':
    71                 type = ''
    70                 type = ''
    72                 a = self._formatnotnull(a)
    71                 a = self._formatnotnull(a)
    73                 b = self._formatnotnull(b)
    72                 b = self._formatnotnull(b)
    74 
    73 
   159         '+' : 'ADD',
   158         '+' : 'ADD',
   160         '-' : 'DROP',
   159         '-' : 'DROP',
   161         '*' : 'ALTER',
   160         '*' : 'ALTER',
   162     }
   161     }
   163 
   162 
   164     def __init__(self, change, schema, table, column, columntype, columndefault, changes=None):
   163     def __init__(self, change, schema, table, column, columntype, columndefault, columnnotnull, changes=None):
   165         DiffBase.__init__(self)
   164         DiffBase.__init__(self)
   166         self.level = 2
   165         self.level = 2
   167         self.type = 'column'
   166         self.type = 'column'
   168         self.change = change
   167         self.change = change
   169         self.schema = schema
   168         self.schema = schema
   170         self.table = table
   169         self.table = table
   171         self.column = column
   170         self.column = column
   172         self.columntype = columntype
   171         self.columntype = columntype
   173         self.columndefault = columndefault
   172         self.columndefault = columndefault
       
   173         self.columnnotnull = columnnotnull
   174         self.name = column
   174         self.name = column
   175         self.changes = changes
   175         self.changes = changes
   176 
   176 
   177     def format_patch(self):
   177     def format_patch(self):
   178         if self.change == '*':
   178         alter_table = 'ALTER TABLE %s.%s %s COLUMN %s' % (
   179             type_statement = ' TYPE'
       
   180         else:
       
   181             type_statement = ''
       
   182         if self.columntype is not None:
       
   183             type_statement += ' ' + self.columntype;
       
   184         out = []
       
   185         out += ['ALTER TABLE %s.%s %s COLUMN %s%s;' % (
       
   186             self.schema,
   179             self.schema,
   187             self.table,
   180             self.table,
   188             self.ALTER_COMMANDS[self.change],
   181             self.ALTER_COMMANDS[self.change],
   189             self.name,
   182             self.name,
   190             type_statement
   183         )
   191         )]
   184         out = []
   192         if self.columndefault:
   185         if self.change == '-':
   193             out += ['ALTER TABLE %s.%s ALTER COLUMN %s SET DEFAULT %s;' % (
   186             out.append('%s;' % alter_table);
   194                 self.schema,
   187         if self.change == '+':
   195                 self.table,
   188             notnull = ''
   196                 self.name,
   189             if self.columnnotnull:
   197                 self.columndefault
   190                 notnull = ' NOT NULL'
   198                 )]
   191             default = ''
       
   192             if self.columndefault:
       
   193                 default = ' DEFAULT %s' % self.columndefault
       
   194             out.append('%s %s%s%s;'
       
   195                 % (alter_table, self.columntype, notnull, default));
       
   196         if self.change == '*':
       
   197             for type, a, b in self.changes:
       
   198                 if type == 'type':
       
   199                     out.append('%s TYPE %s;' % (alter_table, b))
       
   200                 if type == 'notnull':
       
   201                     if a and not b:
       
   202                         out.append('%s DROP NOT NULL;' % alter_table)
       
   203                     if not a and b:
       
   204                         out.append('%s SET NOT NULL;' % alter_table)
       
   205                 if type == 'default':
       
   206                     if b:
       
   207                         out.append('%s SET DEFAULT %s;' % (alter_table, b))
       
   208                     else:
       
   209                         out.append('%s DROP DEFAULT;' % alter_table)
   199         return out
   210         return out
   200 
   211 
   201 
   212 
   202 class DiffConstraint(DiffBase):
   213 class DiffConstraint(DiffBase):
   203     def __init__(self, change, schema, table, constraint, definition, changes=None):
   214     def __init__(self, change, schema, table, constraint, definition, changes=None):
   298     def _diff_columns(self, schema, table, src_columns, dst_columns):
   309     def _diff_columns(self, schema, table, src_columns, dst_columns):
   299         for nd in self._diff_names(src_columns, dst_columns):
   310         for nd in self._diff_names(src_columns, dst_columns):
   300             if nd[1] in dst_columns:
   311             if nd[1] in dst_columns:
   301                 dst_type = dst_columns[nd[1]].type
   312                 dst_type = dst_columns[nd[1]].type
   302                 dst_default = dst_columns[nd[1]].default
   313                 dst_default = dst_columns[nd[1]].default
       
   314                 dst_notnull = dst_columns[nd[1]].notnull
   303             else:
   315             else:
   304                 dst_type = None
   316                 dst_type = None
   305                 dst_default = None
   317                 dst_default = None
       
   318                 dst_notnull = None
   306             cdo = DiffColumn(change=nd[0], schema=schema, table=table, column=nd[1],
   319             cdo = DiffColumn(change=nd[0], schema=schema, table=table, column=nd[1],
   307                 columntype=dst_type, columndefault=dst_default)
   320                 columntype=dst_type, columndefault=dst_default, columnnotnull=dst_notnull)
   308             if nd[0] == '*':
   321             if nd[0] == '*':
   309                 a = src_columns[nd[1]]
   322                 a = src_columns[nd[1]]
   310                 b = dst_columns[nd[1]]
   323                 b = dst_columns[nd[1]]
   311                 cdo.changes = self._compare_columns(a, b)
   324                 cdo.changes = self._compare_columns(a, b)
   312                 if cdo.changes:
   325                 if cdo.changes: