Files
notifications-api/migrations/versions/0011_uuid_service_id.py

122 lines
5.7 KiB
Python
Raw Normal View History

"""empty message
Revision ID: 0011_uuid_service_id
Revises: 0010_add_queue_name_to_service
Create Date: 2016-02-01 15:47:30.553052
"""
# revision identifiers, used by Alembic.
from sqlalchemy.dialects import postgresql
revision = '0011_uuid_service_id'
down_revision = '0010_add_queue_name_to_service'
from alembic import op
import sqlalchemy as sa
def upgrade():
### commands auto generated by Alembic - please adjust! ###
# op.execute('update jobs set new_service_id = (SELECT queue_name from services where service_id = services.id)')
op.add_column('user_to_service', sa.Column('new_service_id', postgresql.UUID(as_uuid=True), nullable=True))
op.get_bind()
op.execute('update user_to_service '\
'set new_service_id = (SELECT queue_name from services where service_id = services.id)')
op.drop_column('user_to_service', 'service_id')
op.alter_column('user_to_service', 'new_service_id', new_column_name='service_id')
op.add_column('jobs', sa.Column('new_service_id', postgresql.UUID(as_uuid=True), nullable=True))
op.execute('update jobs '\
'set new_service_id = (SELECT queue_name from services where service_id = services.id)')
op.drop_column('jobs', 'service_id')
op.alter_column('jobs', 'new_service_id', new_column_name='service_id', nullable=False)
op.add_column('api_key', sa.Column('new_service_id', postgresql.UUID(as_uuid=True), nullable=True))
op.execute('update api_key '\
'set new_service_id = (SELECT queue_name from services where service_id = services.id)')
op.drop_column('api_key', 'service_id')
op.alter_column('api_key', 'new_service_id', new_column_name='service_id', nullable=False)
op.add_column('templates', sa.Column('new_service_id', postgresql.UUID(as_uuid=True), nullable=True))
op.execute('update templates '\
'set new_service_id = (SELECT queue_name from services where service_id = services.id)')
op.drop_column('templates', 'service_id')
op.alter_column('templates', 'new_service_id', new_column_name='service_id', nullable=False)
op.drop_constraint('services_pkey', 'services')
op.alter_column('services', 'id', new_column_name='old_id')
op.alter_column('services', 'queue_name', new_column_name='id', nullable=False)
op.create_primary_key('services_pkey', 'services', ['id'])
op.create_foreign_key('user_to_service_service_id_fkey', 'user_to_service', 'services', ['service_id'], ['id'])
op.create_foreign_key('api_key_service_id_fkey', 'api_key', 'services', ['service_id'], ['id'])
op.create_foreign_key('jobs_service_id_fkey', 'jobs', 'services', ['service_id'], ['id'])
op.create_foreign_key('templates_service_id_fkey', 'templates', 'services', ['service_id'], ['id'])
op.create_index(op.f('ix_templates_service_id'), 'templates', ['service_id'], unique=False)
op.create_index(op.f('ix_jobs_service_id'), 'jobs', ['service_id'], unique=False)
op.create_index(op.f('ix_api_key_service_id'), 'api_key', ['service_id'], unique=False)
op.create_unique_constraint('uix_service_to_key_name', 'api_key', ['service_id', 'name'])
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column('user_to_service', sa.Column('new_service_id', sa.Integer, nullable=True))
op.get_bind()
op.execute('update user_to_service '\
'set new_service_id = (SELECT old_id from services where service_id = services.id)')
op.drop_column('user_to_service', 'service_id')
op.alter_column('user_to_service', 'new_service_id', new_column_name='service_id', nullable=False)
op.add_column('jobs', sa.Column('new_service_id', sa.Integer, nullable=True))
op.execute('update jobs '\
'set new_service_id = (SELECT old_id from services where service_id = services.id)')
op.drop_column('jobs', 'service_id')
op.alter_column('jobs', 'new_service_id', new_column_name='service_id', nullable=False)
op.add_column('api_key', sa.Column('new_service_id', sa.Integer, nullable=True))
op.execute('update api_key '\
'set new_service_id = (SELECT old_id from services where service_id = services.id)')
op.drop_column('api_key', 'service_id')
op.alter_column('api_key', 'new_service_id', new_column_name='service_id', nullable=False)
op.add_column('templates', sa.Column('new_service_id', sa.Integer, nullable=True))
op.execute('update templates '\
'set new_service_id = (SELECT old_id from services where service_id = services.id)')
op.drop_column('templates', 'service_id')
op.alter_column('templates', 'new_service_id', new_column_name='service_id', nullable=False)
op.drop_constraint('services_pkey', 'services')
op.alter_column('services', 'id', new_column_name='queue_name', nullable=False)
op.alter_column('services', 'old_id', new_column_name='id', nullable=True)
op.create_primary_key('services_pkey', 'services', ['id'])
op.create_foreign_key('user_to_service_service_id_fkey', 'user_to_service', 'services', ['service_id'], ['id'])
op.create_foreign_key('api_key_service_id_fkey', 'api_key', 'services', ['service_id'], ['id'])
op.create_foreign_key('jobs_service_id_fkey', 'jobs', 'services', ['service_id'], ['id'])
op.create_foreign_key('templates_service_id_fkey', 'templates', 'services', ['service_id'], ['id'])
op.create_index(op.f('ix_api_key_service_id'), 'api_key', ['service_id'], unique=False)
op.create_unique_constraint('uix_service_to_key_name', 'api_key', ['service_id', 'name'])
op.create_index(op.f('ix_jobs_service_id'), 'jobs', ['service_id'], unique=False)
op.create_index(op.f('ix_templates_service_id'), 'templates', ['service_id'], unique=False)
### end Alembic commands ###