Logo Search packages:      
Sourcecode: ibid version File versions  Download package

def ibid::db::versioned_schema::VersionedSchema::alter_column (   self,
  col,
  old_name = None,
  force_rebuild = False 
)

Change a column (possibly renaming from old_name) to Column col.

Definition at line 348 of file versioned_schema.py.

00348                                                                    :
        """Change a column (possibly renaming from old_name) to Column col."""

        session = self.upgrade_session
        table = self._get_reflected_model()

        log.debug(u"Altering column %s in table %s", col.name, table.name)

        sg = session.bind.dialect.schemagenerator(session.bind.dialect,
                session.bind)
        description = sg.get_column_specification(col)
        old_col = table.c[old_name or col.name]

        # SQLite doesn't enforce value length restrictions
        # only type changes have a real effect
        if session.bind.engine.name == 'sqlite':
            if not force_rebuild and (
                (isinstance(col.type, (IbidUnicodeText, IbidUnicode))
                    and isinstance(old_col.type, (IbidUnicodeText, IbidUnicode)
                ) or (isinstance(col.type, Integer)
                    and isinstance(old_col.type, Integer)))):
                return

            self._rebuild_sqlite(
                    {old_name is None and col.name or old_name: col})

        elif session.bind.engine.name == 'mysql':
            # Special handling for columns of TEXT type, because SQLAlchemy
            # can't create indexes for them
            recreate = []
            if isinstance(col.type, IbidUnicodeText) \
                    or isinstance(old_col.type, IbidUnicodeText):
                for type in (table.constraints, table.indexes):
                    for constraint in list(type):
                        if any(True for column in constraint.columns
                                if old_col.name == column.name):

                            self.drop_index(constraint)

                            constraint.columns = [
                                (old_col.name == column.name) and col or column
                                for column in constraint.columns
                            ]
                            recreate.append((
                                isinstance(constraint, UniqueConstraint),
                                self._mysql_constraint_createstring(constraint)
                            ))

            session.execute('ALTER TABLE "%s" CHANGE "%s" %s;' %
                (table.name, old_col.name, description))

            for unique, columnspec in recreate:
                session.execute('ALTER TABLE "%s" ADD %s INDEX (%s);' %
                    (self.table.name, unique and 'UNIQUE' or '', columnspec))

        else:
            if old_name is not None:
                self.rename_column(col, old_name)

            session.execute('ALTER TABLE "%s" ALTER COLUMN "%s" TYPE %s;' %
                (table.name, col.name, description.split(" ", 3)[1]))

            if old_col.nullable != col.nullable:
                session.execute(
                    'ALTER TABLE "%s" ALTER COLUMN "%s" %s NOT NULL;'
                    % (table.name, col.name, col.nullable and 'DROP' or 'SET')
                )

    def _rebuild_sqlite(self, colmap):


Generated by  Doxygen 1.6.0   Back to index