mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 01:41:05 -05:00
Added version history to api keys. This needed a bit of change
to create history to handle foreign keys better. There may yet be a better way of doing this that I have not found yet in sqlalchemy docs.
This commit is contained in:
64
migrations/versions/0006_api_keys_history.py
Normal file
64
migrations/versions/0006_api_keys_history.py
Normal file
@@ -0,0 +1,64 @@
|
||||
"""empty message
|
||||
|
||||
Revision ID: 0006_api_keys_history
|
||||
Revises: 0005_add_provider_stats
|
||||
Create Date: 2016-04-20 17:21:38.541766
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '0006_api_keys_history'
|
||||
down_revision = '0005_add_provider_stats'
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
def upgrade():
|
||||
### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('api_keys_history',
|
||||
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('name', sa.String(length=255), nullable=False),
|
||||
sa.Column('secret', sa.String(length=255), nullable=False),
|
||||
sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('expiry_date', sa.DateTime(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('created_by_id', postgresql.UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('version', sa.Integer(), autoincrement=False, nullable=False),
|
||||
sa.PrimaryKeyConstraint('id', 'version')
|
||||
)
|
||||
|
||||
op.create_index(op.f('ix_api_keys_history_created_by_id'), 'api_keys_history', ['created_by_id'], unique=False)
|
||||
op.create_index(op.f('ix_api_keys_history_service_id'), 'api_keys_history', ['service_id'], unique=False)
|
||||
op.add_column('api_keys', sa.Column('created_at', sa.DateTime(), nullable=True))
|
||||
op.add_column('api_keys', sa.Column('created_by_id', postgresql.UUID(as_uuid=True), nullable=True))
|
||||
op.add_column('api_keys', sa.Column('updated_at', sa.DateTime(), nullable=True))
|
||||
op.add_column('api_keys', sa.Column('version', sa.Integer(), nullable=True))
|
||||
|
||||
op.get_bind()
|
||||
op.execute('UPDATE api_keys SET created_by_id = (SELECT user_id FROM user_to_service WHERE api_keys.service_id = user_to_service.service_id LIMIT 1)')
|
||||
op.execute('UPDATE api_keys SET version = 1, created_at = now()')
|
||||
op.execute('INSERT INTO api_keys_history (id, name, secret, service_id, expiry_date, created_at, updated_at, created_by_id, version) SELECT id, name, secret, service_id, expiry_date, created_at, updated_at, created_by_id, version FROM api_keys')
|
||||
|
||||
op.alter_column('api_keys', 'created_at', nullable=False)
|
||||
op.alter_column('api_keys', 'created_by_id', nullable=False)
|
||||
op.alter_column('api_keys', 'version', nullable=False)
|
||||
|
||||
op.create_index(op.f('ix_api_keys_created_by_id'), 'api_keys', ['created_by_id'], unique=False)
|
||||
op.create_foreign_key('fk_api_keys_created_by_id', 'api_keys', 'users', ['created_by_id'], ['id'])
|
||||
### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_constraint('fk_api_keys_created_by_id', 'api_keys', type_='foreignkey')
|
||||
op.drop_index(op.f('ix_api_keys_created_by_id'), table_name='api_keys')
|
||||
op.drop_column('api_keys', 'version')
|
||||
op.drop_column('api_keys', 'updated_at')
|
||||
op.drop_column('api_keys', 'created_by_id')
|
||||
op.drop_column('api_keys', 'created_at')
|
||||
op.drop_index(op.f('ix_api_keys_history_service_id'), table_name='api_keys_history')
|
||||
op.drop_index(op.f('ix_api_keys_history_created_by_id'), table_name='api_keys_history')
|
||||
op.drop_table('api_keys_history')
|
||||
### end Alembic commands ###
|
||||
Reference in New Issue
Block a user