mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-09 23:02:13 -05:00
Include check constraint in migration script
Add check constraint that created_by_id should not be null, unless created_by_api_key_id is not null to the migration script. It is already in the models file. Also remove check constraint for cancelled_by_id from models, as this field would only be filled for broadcasts with cancelled status. Also add some spacing in that migration script so it is easier to read.
This commit is contained in:
@@ -16,10 +16,29 @@ down_revision = '0362_broadcast_msg_event'
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('broadcast_message', sa.Column('created_by_api_key_id', postgresql.UUID(as_uuid=True), nullable=True))
|
||||
op.add_column('broadcast_message', sa.Column('cancelled_by_api_key_id', postgresql.UUID(as_uuid=True), nullable=True))
|
||||
op.add_column(
|
||||
'broadcast_message', sa.Column('cancelled_by_api_key_id', postgresql.UUID(as_uuid=True), nullable=True)
|
||||
)
|
||||
op.drop_constraint('broadcast_message_api_key_id_fkey', 'broadcast_message', type_='foreignkey')
|
||||
op.create_foreign_key('broadcast_message_created_by_api_key_id_fkey', 'broadcast_message', 'api_keys', ['created_by_api_key_id'], ['id'])
|
||||
op.create_foreign_key('broadcast_message_cancelled_by_api_key_id_fkey', 'broadcast_message', 'api_keys', ['cancelled_by_api_key_id'], ['id'])
|
||||
op.create_foreign_key(
|
||||
'broadcast_message_created_by_api_key_id_fkey',
|
||||
'broadcast_message',
|
||||
'api_keys',
|
||||
['created_by_api_key_id'],
|
||||
['id']
|
||||
)
|
||||
op.create_foreign_key(
|
||||
'broadcast_message_cancelled_by_api_key_id_fkey',
|
||||
'broadcast_message',
|
||||
'api_keys',
|
||||
['cancelled_by_api_key_id'],
|
||||
['id']
|
||||
)
|
||||
op.create_check_constraint(
|
||||
"ck_broadcast_message_created_by_not_null",
|
||||
"broadcast_message",
|
||||
"created_by_id is not null or created_by_api_key_id is not null"
|
||||
)
|
||||
op.get_bind()
|
||||
op.execute("UPDATE broadcast_message SET created_by_api_key_id=api_key_id") # move data over
|
||||
op.drop_column('broadcast_message', 'api_key_id')
|
||||
@@ -29,6 +48,10 @@ def upgrade():
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('broadcast_message', sa.Column('api_key_id', postgresql.UUID(), autoincrement=False, nullable=True))
|
||||
op.drop_constraint(
|
||||
"ck_broadcast_message_created_by_not_null",
|
||||
"broadcast_message"
|
||||
)
|
||||
op.drop_constraint('broadcast_message_created_by_api_key_id_fkey', 'broadcast_message', type_='foreignkey')
|
||||
op.drop_constraint('broadcast_message_cancelled_by_api_key_id_fkey', 'broadcast_message', type_='foreignkey')
|
||||
op.create_foreign_key('broadcast_message_api_key_id_fkey', 'broadcast_message', 'api_keys', ['api_key_id'], ['id'])
|
||||
|
||||
Reference in New Issue
Block a user