示例#1
0
    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
示例#2
0
    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
示例#3
0
    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
示例#4
0
    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
示例#5
0
    def add_column(self, model, f, initial):
        qn = self.connection.ops.quote_name
        sql_result = AlterTableSQLResult(self, model)

        if f.rel:
            # it is a foreign key field
            # NOT NULL REFERENCES "django_evolution_addbasemodel"
            # ("id") DEFERRABLE INITIALLY DEFERRED

            # ALTER TABLE <tablename> ADD COLUMN <column name> NULL
            # REFERENCES <tablename1> ("<colname>") DEFERRABLE INITIALLY
            # DEFERRED
            related_model = f.rel.to
            related_table = related_model._meta.db_table
            related_pk_col = related_model._meta.pk.name
            constraints = ['%sNULL' % (not f.null and 'NOT ' or '')]

            if f.unique or f.primary_key:
                constraints.append('UNIQUE')

            sql_result.add_alter_table([
                {
                    'op': 'ADD COLUMN',
                    'column': f.column,
                    'db_type': f.db_type(connection=self.connection),
                    'params': constraints + [
                        'REFERENCES',
                        qn(related_table),
                        '(%s)' % qn(related_pk_col),
                        self.connection.ops.deferrable_sql(),
                    ]
                }
            ])
        else:
            null_constraints = '%sNULL' % (not f.null and 'NOT ' or '')

            if f.unique or f.primary_key:
                unique_constraints = 'UNIQUE'
            else:
                unique_constraints = ''

            # At this point, initial can only be None if null=True,
            # otherwise it is a user callable or the default
            # AddFieldInitialCallback which will shortly raise an exception.
            if initial is not None:
                if callable(initial):
                    sql_result.add_alter_table([
                        {
                            'op': 'ADD COLUMN',
                            'column': f.column,
                            'db_type': f.db_type(connection=self.connection),
                            'params': [unique_constraints],
                        }
                    ])

                    sql_result.add_sql([
                        'UPDATE %s SET %s = %s WHERE %s IS NULL;'
                        % (qn(model._meta.db_table), qn(f.column),
                           initial(), qn(f.column))
                    ])

                    if not f.null:
                        # Only put this sql statement if the column cannot
                        # be null.
                        sql_result.add_sql(
                            self.set_field_null(model, f, f.null))
                else:
                    sql_result.add_alter_table([
                        {
                            'op': 'ADD COLUMN',
                            'column': f.column,
                            'db_type': f.db_type(connection=self.connection),
                            'params': [
                                null_constraints,
                                unique_constraints,
                                'DEFAULT',
                                '%s',
                            ],
                            'sql_params': [initial]
                        }
                    ])

                    # Django doesn't generate default columns, so now that
                    # we've added one to get default values for existing
                    # tables, drop that default.
                    sql_result.add_post_sql([
                        'ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;'
                        % (qn(model._meta.db_table), qn(f.column))
                    ])
            else:
                sql_result.add_alter_table([
                    {
                        'op': 'ADD COLUMN',
                        'column': f.column,
                        'db_type': f.db_type(connection=self.connection),
                        'params': [null_constraints, unique_constraints],
                    }
                ])

        if f.unique or f.primary_key:
            self.record_index(model, [f], use_constraint_name=True,
                              unique=True)

        return sql_result
示例#6
0
    def add_column(self, model, f, initial):
        qn = self.connection.ops.quote_name
        sql_result = AlterTableSQLResult(self, model)

        if f.rel:
            # it is a foreign key field
            # NOT NULL REFERENCES "django_evolution_addbasemodel"
            # ("id") DEFERRABLE INITIALLY DEFERRED

            # ALTER TABLE <tablename> ADD COLUMN <column name> NULL
            # REFERENCES <tablename1> ("<colname>") DEFERRABLE INITIALLY
            # DEFERRED
            related_model = f.rel.to
            related_table = related_model._meta.db_table
            related_pk_col = related_model._meta.pk.name
            constraints = ['%sNULL' % (not f.null and 'NOT ' or '')]

            if f.unique or f.primary_key:
                constraints.append('UNIQUE')

            sql_result.add_alter_table([{
                'op':
                'ADD COLUMN',
                'column':
                f.column,
                'db_type':
                f.db_type(connection=self.connection),
                'params':
                constraints + [
                    'REFERENCES',
                    qn(related_table),
                    '(%s)' % qn(related_pk_col),
                    self.connection.ops.deferrable_sql(),
                ]
            }])
        else:
            null_constraints = '%sNULL' % (not f.null and 'NOT ' or '')

            if f.unique or f.primary_key:
                unique_constraints = 'UNIQUE'
            else:
                unique_constraints = ''

            # At this point, initial can only be None if null=True,
            # otherwise it is a user callable or the default
            # AddFieldInitialCallback which will shortly raise an exception.
            if initial is not None:
                if callable(initial):
                    sql_result.add_alter_table([{
                        'op':
                        'ADD COLUMN',
                        'column':
                        f.column,
                        'db_type':
                        f.db_type(connection=self.connection),
                        'params': [unique_constraints],
                    }])

                    sql_result.add_sql([
                        'UPDATE %s SET %s = %s WHERE %s IS NULL;' %
                        (qn(model._meta.db_table), qn(
                            f.column), initial(), qn(f.column))
                    ])

                    if not f.null:
                        # Only put this sql statement if the column cannot
                        # be null.
                        sql_result.add_sql(
                            self.set_field_null(model, f, f.null))
                else:
                    sql_result.add_alter_table([{
                        'op':
                        'ADD COLUMN',
                        'column':
                        f.column,
                        'db_type':
                        f.db_type(connection=self.connection),
                        'params': [
                            null_constraints,
                            unique_constraints,
                            'DEFAULT',
                            '%s',
                        ],
                        'sql_params': [initial]
                    }])

                    # Django doesn't generate default columns, so now that
                    # we've added one to get default values for existing
                    # tables, drop that default.
                    sql_result.add_post_sql([
                        'ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;' %
                        (qn(model._meta.db_table), qn(f.column))
                    ])
            else:
                sql_result.add_alter_table([{
                    'op':
                    'ADD COLUMN',
                    'column':
                    f.column,
                    'db_type':
                    f.db_type(connection=self.connection),
                    'params': [null_constraints, unique_constraints],
                }])

        if f.unique or f.primary_key:
            self.record_index(model, [f],
                              use_constraint_name=True,
                              unique=True)

        return sql_result