From e1a8219eb1ac51beb69ec295fdfb8ecddf1f1806 Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Tue, 25 Jan 2022 18:09:03 +0000 Subject: [PATCH] DB Migration to allow auditing api key id when cancelling broadcast via API. --- app/models.py | 9 +++-- .../versions/0363_cancelled_by_api_key.py | 39 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 migrations/versions/0363_cancelled_by_api_key.py diff --git a/app/models.py b/app/models.py index 2486a4045..ddba7e158 100644 --- a/app/models.py +++ b/app/models.py @@ -2313,15 +2313,18 @@ class BroadcastMessage(db.Model): approved_by = db.relationship('User', foreign_keys=[approved_by_id]) cancelled_by = db.relationship('User', foreign_keys=[cancelled_by_id]) - api_key_id = db.Column(UUID(as_uuid=True), db.ForeignKey('api_keys.id'), nullable=True) - api_key = db.relationship('ApiKey') + created_by_api_key_id = db.Column(UUID(as_uuid=True), db.ForeignKey('api_keys.id'), nullable=True) + cancelled_by_api_key_id = db.Column(UUID(as_uuid=True), db.ForeignKey('api_keys.id'), nullable=True) + created_by_api_key = db.relationship('ApiKey', foreign_keys=[created_by_api_key_id]) + cancelled_by_api_key = db.relationship('ApiKey', foreign_keys=[cancelled_by_api_key_id]) reference = db.Column(db.String(255), nullable=True) cap_event = db.Column(db.String(255), nullable=True) stubbed = db.Column(db.Boolean, nullable=False) - CheckConstraint("created_by_id is not null or api_key_id is not null") + CheckConstraint("created_by_id is not null or created_by_api_key_id is not null") + CheckConstraint("cancelled_by_id is not null or cancelled_by_api_key_id is not null") @property def personalisation(self): diff --git a/migrations/versions/0363_cancelled_by_api_key.py b/migrations/versions/0363_cancelled_by_api_key.py new file mode 100644 index 000000000..b9611e875 --- /dev/null +++ b/migrations/versions/0363_cancelled_by_api_key.py @@ -0,0 +1,39 @@ +""" + +Revision ID: 0363_cancelled_by_api_key +Revises: 0362_broadcast_msg_event +Create Date: 2022-01-25 18:05:27.750234 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +revision = '0363_cancelled_by_api_key' +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.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.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') + # ### end Alembic commands ### + + +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('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']) + op.get_bind() + op.execute("UPDATE broadcast_message SET api_key_id=created_by_api_key_id") # move data over + op.drop_column('broadcast_message', 'cancelled_by_api_key_id') + op.drop_column('broadcast_message', 'created_by_api_key_id') + # ### end Alembic commands ###