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

def ibid::db::versioned_schema::VersionedSchema::_rebuild_sqlite (   self,
  colmap 
) [private]

SQLite doesn't support modification of table schema - must rebuild the
table.
colmap maps old column names to new Columns
(or None for column deletion).
Only modified columns need to be listed, unchaged columns are carried
over automatically.
Specify table in case name has changed in a more recent version.

Definition at line 416 of file versioned_schema.py.

00416                                      :
        """
        SQLite doesn't support modification of table schema - must rebuild the
        table.
        colmap maps old column names to new Columns
        (or None for column deletion).
        Only modified columns need to be listed, unchaged columns are carried
        over automatically.
        Specify table in case name has changed in a more recent version.
        """

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

        log.debug(u"Rebuilding SQLite table %s", table.name)

        fullcolmap = {}
        for col in table.c:
            if col.name in colmap:
                if colmap[col.name] is not None:
                    fullcolmap[col.name] = colmap[col.name].name
            else:
                fullcolmap[col.name] = col.name

        for old, col in colmap.iteritems():
            del table.c[old]
            if col is not None:
                table.append_column(col)

        session.execute('ALTER TABLE "%s" RENAME TO "%s_old";'
                % (table.name, table.name))

        # SQLAlchemy indexes aren't attached to tables, they must be dropped
        # around now or we'll get a clash
        for constraint in table.indexes:
            try:
                constraint.drop()
            except OperationalError:
                pass

        table.create()

        session.execute('INSERT INTO "%s" ("%s") SELECT "%s" FROM "%s_old";'
            % (
                table.name,
                '", "'.join(fullcolmap.values()),
                '", "'.join(fullcolmap.keys()),
                table.name
        ))

        session.execute('DROP TABLE "%s_old";' % table.name)

        # SQLAlchemy doesn't pick up all the indexes in the reflected table.
        # It's ok to use indexes that may be further in the future than this
        # upgrade because either we can already support them or we'll be
        # rebuilding again soon
        for constraint in self.table.indexes:
            try:
                constraint.create(bind=session.bind)
            except OperationalError:
                pass


class SchemaVersionException(Exception):


Generated by  Doxygen 1.6.0   Back to index