def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    domains = Table('domains', meta, autoload=True)
    subscribers = Table('subscribers', meta, autoload=True)

    utils.drop_unique_constraint(
        migrate_engine, 'subscribers', 'uniq_subscriber0username0domain',
        'username', 'domain')

    d = Column('domain_id', String(length=255))
    subscribers.create_column(d)

    UniqueConstraint(
        'username', 'domain_id', table=subscribers,
        name='uniq_subscriber0username0domain_id').create()

    Index('uuid', domains.c.uuid, unique=True).create()

    ForeignKeyConstraint(
        columns=[subscribers.c.domain_id],
        refcolumns=[domains.c.uuid]).create()

    domains_list = list(domains.select().execute())

    # If we have a domain, upgrade the subscribers with the correct UUID.
    for domain in domains_list:
        subscribers.update().\
            where(subscribers.c.domain == domain.name).\
            where(subscribers.c.project_id == domain.project_id).\
            values(domain_id=domain.uuid).execute()

    subscribers.drop_column('domain')
def downgrade(migrate_engine):
    if migrate_engine.name == 'sqlite':
        raise NotImplementedError('Downgrade with SQLite not supported.')

    meta = MetaData()
    meta.bind = migrate_engine

    domains = Table('domains', meta, autoload=True)
    subscribers = Table('subscribers', meta, autoload=True)

    ForeignKeyConstraint(
        columns=[subscribers.c.domain_id],
        refcolumns=[domains.c.uuid]).drop()

    Index('uuid', domains.c.uuid, unique=True).drop()

    utils.drop_unique_constraint(
        migrate_engine, 'subscribers', 'uniq_subscriber0username0domain_id',
        'username', 'domain_id')

    d = Column('domain', String(length=64), nullable=True)
    d.create(subscribers)

    UniqueConstraint(
        'username', 'domain', table=subscribers,
        name='uniq_subscriber0username0domain').create()

    domains_list = list(domains.select().execute())

    # If we have a domain, upgrade the subscribers with the correct name.
    for domain in domains_list:
        subscribers.update().\
            where(subscribers.c.domain_id == domain.uuid).\
            where(subscribers.c.project_id == domain.project_id).\
            values(domain=domain.name).execute()

    subscribers.c.domain.alter(nullable=False)
    subscribers.drop_column('domain_id')