Files
notifications-api/migrations/versions/0011_uuid_service_id.py
Rebecca Law b5c662eca8 Change services.id to a UUID
Ideally all the primary keys in the db would be UUID in order to guarantee unique ids across distributed dbs.
This updates the services.id to a UUID. All the tables with a foreign key to the services.id are also updated.
The endpoints no longer state a data type of the <service_id> path param.
All the tests are updated to reflect this update.

The thing to pay attention to is the 0011_uuid_service_id.py migration script.
This commit must go with a commit on the notifications_admin app to keep things working.
There will be a small outage until both deploys have happened.
2016-02-02 14:22:22 +00:00

122 lines
5.7 KiB
Python

"""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 ###