**** CubicPower OpenStack Study ****
def _migrate_to_new_region_table(meta, migrate_engine, region_table):
# Create a temporary region table to hold data while we recreate the
# new region table without a unique constraint on the description column
session = sessionmaker(bind=migrate_engine)()
temp_region_table = sql.Table(
_TEMP_REGION_TABLE_NAME,
meta,
sql.Column('id', sql.String(64), primary_key=True),
sql.Column('description', sql.String(255), nullable=False),
sql.Column('parent_region_id', sql.String(64), nullable=True),
sql.Column('extra', sql.Text()))
temp_region_table.create(migrate_engine, checkfirst=True)
# Migrate the data
for region in list(session.query(region_table)):
session.execute(temp_region_table.insert().values(
id=region.id,
description=region.description,
parent_region_id=region.parent_region_id,
extra=region.extra))
session.commit()
session.close()
# Drop the old region table
region_table.drop(checkfirst=True)
migrate.rename_table(temp_region_table, _REGION_TABLE_NAME, meta.bind)
**** CubicPower OpenStack Study ****
def upgrade(migrate_engine):
meta = sql.MetaData()
meta.bind = migrate_engine
region_table = sql.Table(_REGION_TABLE_NAME, meta, autoload=True)
for idx in region_table.indexes:
if ((idx.columns.get('description') == region_table.c.description) and
len(idx.columns) is 1):
# Constraint was found, do the migration.
_migrate_to_new_region_table(meta, migrate_engine, region_table)
break
**** CubicPower OpenStack Study ****
def downgrade(migrate_engine):
# There is no downgrade option. The unique constraint should not have
# existed and therefore does not need to be re-added. The previous
# migration has been modified to not contain the unique constraint.
pass