def upgrade(migrate_engine): meta = MetaData(bind=migrate_engine) t = Table('chassis', meta, autoload=True) # NOTE: new name convention for UC uc = UniqueConstraint('uuid', table=t, name='uniq_chassis0uuid') uc.create()
def downgrade(migrate_engine): meta = MetaData(bind=migrate_engine) table = Table(TABLE_NAME, meta, autoload=True) utils.drop_unique_constraint(migrate_engine, TABLE_NAME, NEW_NAME, *COLUMNS) uc_old = UniqueConstraint(*COLUMNS, table=table, name=OLD_NAME) uc_old.create()
def upgrade(migrate_engine): meta = MetaData(bind=migrate_engine) t = Table(TABLE_NAME, meta, autoload=True) utils.drop_old_duplicate_entries_from_table(migrate_engine, TABLE_NAME, True, *COLUMNS) uc = UniqueConstraint(*COLUMNS, table=t, name=UC_NAME) uc.create()
def upgrade(migrate_engine): meta = MetaData(bind=migrate_engine) t = Table(TABLE_NAME, meta, autoload=True) utils.drop_old_duplicate_entries_from_table(migrate_engine, TABLE_NAME, True, *COLUMNS) uc = UniqueConstraint(*COLUMNS, table=t, name=UC_NAME) uc.create()
def upgrade(migrate_engine): utils.drop_unique_constraint(migrate_engine, TABLE_NAME, OLD_UC_NAME, OLD_COLUMN) meta = MetaData(bind=migrate_engine) t = Table(TABLE_NAME, meta, autoload=True) if migrate_engine.name == "mysql": index = Index(OLD_COLUMN, t.c[OLD_COLUMN], unique=True) index.drop() uc = UniqueConstraint(*COLUMNS, table=t, name=UC_NAME) uc.create()
def restore_unique_constraint(table): # NOTE(Vek): So, sqlite doesn't really support dropping columns, # and so it gets implemented by dropping and recreating # the table...which of course means we completely lose # the unique constraint. We re-create it here to work # around this issue. uc_name = 'uniq_cell_name0deleted' columns = ('name', 'deleted') uc = UniqueConstraint(*columns, table=table, name=uc_name) uc.create()
def restore_unique_constraint(table): # NOTE(Vek): So, sqlite doesn't really support dropping columns, # and so it gets implemented by dropping and recreating # the table...which of course means we completely lose # the unique constraint. We re-create it here to work # around this issue. uc_name = 'uniq_cell_name0deleted' columns = ('name', 'deleted') uc = UniqueConstraint(*columns, table=table, name=uc_name) uc.create()
def upgrade(migrate_engine): utils.drop_unique_constraint(migrate_engine, TABLE_NAME, OLD_UC_NAME, OLD_COLUMN) meta = MetaData(bind=migrate_engine) t = Table(TABLE_NAME, meta, autoload=True) if migrate_engine.name == "mysql": index = Index(OLD_COLUMN, t.c[OLD_COLUMN], unique=True) index.drop() uc = UniqueConstraint(*COLUMNS, table=t, name=UC_NAME) uc.create()
def downgrade(migrate_engine): utils.drop_unique_constraint(migrate_engine, TABLE_NAME, UC_NAME, *COLUMNS) meta = MetaData(bind=migrate_engine) t = Table(TABLE_NAME, meta, autoload=True) delete_statement = t.delete().where(t.c.deleted != 0) migrate_engine.execute(delete_statement) uc = UniqueConstraint(OLD_COLUMN, table=t, name=OLD_UC_NAME) uc.create() if migrate_engine.name == "mysql": index = Index(OLD_COLUMN, t.c[OLD_COLUMN], unique=True) index.create()
def downgrade(migrate_engine): utils.drop_unique_constraint(migrate_engine, TABLE_NAME, UC_NAME, *COLUMNS) meta = MetaData(bind=migrate_engine) t = Table(TABLE_NAME, meta, autoload=True) delete_statement = t.delete().where(t.c.deleted != 0) migrate_engine.execute(delete_statement) uc = UniqueConstraint(OLD_COLUMN, table=t, name=OLD_UC_NAME) uc.create() if migrate_engine.name == "mysql": index = Index(OLD_COLUMN, t.c[OLD_COLUMN], unique=True) index.create()
def upgrade(migrate_engine): meta = MetaData(bind=migrate_engine) key_pairs = Table(TABLE_NAME, meta, autoload=True) utils.drop_old_duplicate_entries_from_table(migrate_engine, TABLE_NAME, True, *UC_COLUMNS) old_idx = None #Drop old index because the new UniqueConstraint can be used instead. for index in key_pairs.indexes: if index.name == OLD_IDX_NAME: index.drop() old_idx = index #index.drop() in SQLAlchemy-migrate will issue a DROP INDEX statement to #the DB but WILL NOT update the table metadata to remove the `Index` #object. This can cause subsequent calls like drop or create constraint #on that table to fail.The solution is to update the table metadata to #reflect the now dropped column. if old_idx: key_pairs.indexes.remove(old_idx) uc = UniqueConstraint(*(UC_COLUMNS), table=key_pairs, name=UC_NAME) uc.create()
def upgrade(migrate_engine): meta = MetaData(bind=migrate_engine) key_pairs = Table(TABLE_NAME, meta, autoload=True) utils.drop_old_duplicate_entries_from_table(migrate_engine, TABLE_NAME, True, *UC_COLUMNS) old_idx = None #Drop old index because the new UniqueConstraint can be used instead. for index in key_pairs.indexes: if index.name == OLD_IDX_NAME: index.drop() old_idx = index #index.drop() in SQLAlchemy-migrate will issue a DROP INDEX statement to #the DB but WILL NOT update the table metadata to remove the `Index` #object. This can cause subsequent calls like drop or create constraint #on that table to fail.The solution is to update the table metadata to #reflect the now dropped column. if old_idx: key_pairs.indexes.remove(old_idx) uc = UniqueConstraint(*(UC_COLUMNS), table=key_pairs, name=UC_NAME) uc.create()
def upgrade(migrate_engine): meta = MetaData(bind=migrate_engine) conductor = Table( "conductors", meta, Column("id", Integer, primary_key=True, nullable=False), Column("hostname", String(length=255), nullable=False), Column("drivers", Text), Column("created_at", DateTime), Column("updated_at", DateTime), mysql_engine=ENGINE, mysql_charset=CHARSET, ) try: conductor.create() except Exception: LOG.info(repr(conductor)) LOG.exception(_("Exception while creating table.")) raise uc = UniqueConstraint("hostname", table=conductor, name="uniq_conductors0hostname") uc.create()
def upgrade(migrate_engine): meta = MetaData(bind=migrate_engine) domain_quota = Table('domain_quotas', meta, Column('id', Integer, primary_key=True, nullable=False), Column('created_at', DateTime), Column('updated_at', DateTime), Column('deleted_at', DateTime), Column('deleted', Integer), Column('domain_id', String(255)), Column('resource', String(255), nullable=False), Column('hard_limit', Integer()), mysql_engine='InnoDB', mysql_charset='utf8') domain_quota_usage = Table('domain_quota_usages', meta, Column('id', Integer, primary_key=True, nullable=False), Column('created_at', DateTime), Column('updated_at', DateTime), Column('deleted_at', DateTime), Column('deleted', Integer), Column('domain_id', String(255)), Column('resource', String(255), nullable=False), Column('in_use', Integer, nullable=False), Column('reserved', Integer, nullable=False), Column('until_refresh', Integer), mysql_engine='InnoDB', mysql_charset='utf8') domain_reservation = Table('domain_reservations', meta, Column('id', Integer, primary_key=True, nullable=False), Column('created_at', DateTime), Column('updated_at', DateTime), Column('deleted_at', DateTime), Column('deleted', Integer), Column('uuid', String(length=36), nullable=False), Column('domain_id', String(255)), Column('usage_id', Integer, nullable=False), Column('resource', String(length=255)), Column('delta', Integer, nullable=False), Column('expire', DateTime), mysql_engine='InnoDB', mysql_charset='utf8' ) tables = [domain_quota, domain_quota_usage, domain_reservation] for table in tables: try: table.create() utils.create_shadow_table(migrate_engine, table=table) except Exception: LOG.info(repr(table)) LOG.exception(_('Exception while creating table.')) raise indexes = [ Index('domain_quotas_domain_id_deleted_idx', 'domain_id', 'deleted'), #DomainQuotaUsages Index('ix_domain_quota_usages_domain_id', 'domain_id'), #DomainReservation Index('ix_domain_reservations_id', 'domain_id'), Index('domain_reservations_uuid_idx', 'uuid'), ] # Common indexes # for index in indexes: # print "<<<<<<<<<<<<<<<INDEXES>>>>>>>>>>>>>>>>>>>" # print index # index.create(migrate_engine) fkeys = [ [[domain_reservation.c.usage_id], [domain_quota_usage.c.id], 'domain_reservations_ibfk_1'] ] for fkey_pair in fkeys: if migrate_engine.name == 'mysql': # For MySQL we name our fkeys explicitly so they match Folsom fkey = ForeignKeyConstraint(columns=fkey_pair[0], refcolumns=fkey_pair[1], name=fkey_pair[2]) fkey.create() elif migrate_engine.name == 'postgresql': # PostgreSQL names things like it wants (correct and compatible!) fkey = ForeignKeyConstraint(columns=fkey_pair[0], refcolumns=fkey_pair[1]) fkey.create() uniq_name = "uniq_domain_quotas0domain_id0resource0deleted" uc_domain_quota = UniqueConstraint("domain_id", "resource", "deleted", table=domain_quota, name=uniq_name) uc_domain_quota.create()