def change_column_attrs(self, model, mutation, field_name, new_attrs): """Returns the SQL for changing one or more column attributes. This will generate all the statements needed for changing a set of attributes for a column. The resulting AlterTableSQLResult contains all the SQL needed to apply these attributes. """ field = model._meta.get_field(field_name) attrs_sql_result = AlterTableSQLResult(self, model) for attr_name, attr_info in new_attrs.iteritems(): method_name = 'change_column_attr_%s' % attr_name evolve_func = getattr(self, method_name) try: sql_result = evolve_func(model, mutation, field, attr_info['old_value'], attr_info['new_value']) assert not sql_result or isinstance(sql_result, SQLResult) except Exception, e: logging.critical( 'Error running database evolver function %s: %s', method_name, e, exc_info=1) raise attrs_sql_result.add(sql_result)
def change_column_attrs(self, model, mutation, field_name, new_attrs): """Returns the SQL for changing one or more column attributes. This will generate all the statements needed for changing a set of attributes for a column. The resulting AlterTableSQLResult contains all the SQL needed to apply these attributes. """ field = model._meta.get_field(field_name) attrs_sql_result = AlterTableSQLResult(self, model) for attr_name, attr_info in new_attrs.iteritems(): method_name = 'change_column_attr_%s' % attr_name evolve_func = getattr(self, method_name) try: sql_result = evolve_func(model, mutation, field, attr_info['old_value'], attr_info['new_value']) assert not sql_result or isinstance(sql_result, SQLResult) except Exception, e: logging.critical( 'Error running database evolver function %s: %s', method_name, e, exc_info=1) raise attrs_sql_result.add(sql_result)
def delete_column(self, model, f): sql_result = AlterTableSQLResult(self, model) if f.rel: sql_result.add( sql_delete_constraints(self.connection, f.rel.to, {f.rel.to: [(model, f)]})) sql_result.add_sql( super(EvolutionOperations, self).delete_column(model, f)) return sql_result
def delete_column(self, model, f): sql_result = AlterTableSQLResult(self, model) if f.rel: sql_result.add(sql_delete_constraints(self.connection, f.rel.to, {f.rel.to: [(model, f)]})) sql_result.add_sql( super(EvolutionOperations, self).delete_column(model, f)) return sql_result
def delete_column(self, model, f): sql_result = AlterTableSQLResult(self, model) remote_field = get_remote_field(f) if remote_field: remote_field_model = get_remote_field_model(remote_field) sql_result.add( sql_delete_constraints(self.connection, remote_field_model, {remote_field_model: [(model, f)]})) sql_result.add_sql( super(EvolutionOperations, self).delete_column(model, f)) return sql_result
def delete_column(self, model, f): sql_result = AlterTableSQLResult(self, model) if f.rel: creation = self.connection.creation style = color.no_style() sql_result.add(creation.sql_remove_table_constraints( f.rel.to, {f.rel.to: [(model, f)]}, style)) sql_result.add_sql( super(EvolutionOperations, self).delete_column(model, f)) return sql_result
def generate_table_op_sql(self, mutator, op, prev_sql_result, prev_op): """Generates SQL for a single mutation operation. This will call different SQL-generating functions provided by the class, depending on the details of the operation. If two adjacent operations can be merged together (meaning that they can be turned into one ALTER TABLE statement), they'll be placed in the same AlterTableSQLResult. """ model = mutator.create_model() op_type = op['type'] mutation = op['mutation'] if prev_op and self._are_ops_mergeable(prev_op, op): sql_result = prev_sql_result else: sql_result = AlterTableSQLResult(self, model) if op_type == 'add_column': field = op['field'] sql_result.add(self.add_column(model, field, op['initial'])) sql_result.add(self.create_index(model, field)) elif op_type == 'change_column': sql_result.add(self.change_column_attrs(model, mutation, op['field'].name, op['new_attrs'])) elif op_type == 'delete_column': sql_result.add(self.delete_column(model, op['field'])) elif op_type == 'change_meta': evolve_func = getattr(self, 'change_meta_%s' % op['prop_name']) sql_result.add(evolve_func(model, op['old_value'], op['new_value'])) elif op_type == 'sql': sql_result.add(op['sql']) else: raise EvolutionNotImplementedError( 'Unknown mutation operation "%s"' % op_type) mutator.finish_op(op) return sql_result
def generate_table_op_sql(self, mutator, op, prev_sql_result, prev_op): """Generates SQL for a single mutation operation. This will call different SQL-generating functions provided by the class, depending on the details of the operation. If two adjacent operations can be merged together (meaning that they can be turned into one ALTER TABLE statement), they'll be placed in the same AlterTableSQLResult. """ model = mutator.create_model() op_type = op['type'] mutation = op['mutation'] if prev_op and self._are_ops_mergeable(prev_op, op): sql_result = prev_sql_result else: sql_result = AlterTableSQLResult(self, model) if op_type == 'add_column': field = op['field'] sql_result.add(self.add_column(model, field, op['initial'])) sql_result.add(self.create_index(model, field)) elif op_type == 'change_column': sql_result.add( self.change_column_attrs(model, mutation, op['field'].name, op['new_attrs'])) elif op_type == 'delete_column': sql_result.add(self.delete_column(model, op['field'])) elif op_type == 'change_meta': evolve_func = getattr(self, 'change_meta_%s' % op['prop_name']) sql_result.add(evolve_func(model, op['old_value'], op['new_value'])) elif op_type == 'sql': sql_result.add(op['sql']) else: raise EvolutionNotImplementedError( 'Unknown mutation operation "%s"' % op_type) mutator.finish_op(op) return sql_result