From 6356a5320a07b5c8b5fcebbad041a9936c8552f3 Mon Sep 17 00:00:00 2001 From: Richard Chapman Date: Wed, 27 Sep 2017 10:36:25 +0100 Subject: [PATCH 1/3] Updated model with a new table notification_to_email_sender and created db migration script --- app/models.py | 21 +++++++++++++ .../versions/0123_add_noti_to_email_sender.py | 31 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 migrations/versions/0123_add_noti_to_email_sender.py diff --git a/app/models.py b/app/models.py index be6d54cf1..e0c56f36a 100644 --- a/app/models.py +++ b/app/models.py @@ -1414,3 +1414,24 @@ class ServiceLetterContact(db.Model): 'created_at': self.created_at.strftime(DATETIME_FORMAT), 'updated_at': self.updated_at.strftime(DATETIME_FORMAT) if self.updated_at else None } + + +class NotificationSmsSender(db.Model): + __tablename__ = "notification_to_email_sender" + + notification_id = db.Column( + UUID(as_uuid=True), + db.ForeignKey('notifications.id'), + unique=False, + index=True, + nullable=False, + primary_key=True + ) + service_email_reply_to_id = db.Column( + UUID(as_uuid=True), + db.ForeignKey('service_email_reply_to.id'), + unique=False, + index=True, + nullable=False, + primary_key=True + ) diff --git a/migrations/versions/0123_add_noti_to_email_sender.py b/migrations/versions/0123_add_noti_to_email_sender.py new file mode 100644 index 000000000..2a49df3da --- /dev/null +++ b/migrations/versions/0123_add_noti_to_email_sender.py @@ -0,0 +1,31 @@ +""" + +Revision ID: 0123_add_noti_to_email_sender +Revises: 0122_add_service_letter_contact +Create Date: 2017-09-27 09:42:39.412731 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +revision = '0123_add_noti_to_email_sender' +down_revision = '0122_add_service_letter_contact' + + +def upgrade(): + op.create_table('notification_to_email_sender', + sa.Column('notification_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('service_email_reply_to_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.ForeignKeyConstraint(['notification_id'], ['notifications.id'], ), + sa.ForeignKeyConstraint(['service_email_reply_to_id'], ['service_email_reply_to.id'], ), + sa.PrimaryKeyConstraint('notification_id', 'service_email_reply_to_id') + ) + op.create_index(op.f('ix_notification_to_email_sender_notification_id'), 'notification_to_email_sender', ['notification_id'], unique=False) + op.create_index(op.f('ix_notification_to_email_sender_service_email_reply_to_id'), 'notification_to_email_sender', ['service_email_reply_to_id'], unique=False) + + +def downgrade(): + op.drop_index(op.f('ix_notification_to_email_sender_service_email_reply_to_id'), table_name='notification_to_email_sender') + op.drop_index(op.f('ix_notification_to_email_sender_notification_id'), table_name='notification_to_email_sender') + op.drop_table('notification_to_email_sender') From fdc4d4c24f1011d7f261a3ab28f0a5bb2aca4ccf Mon Sep 17 00:00:00 2001 From: Richard Chapman Date: Thu, 28 Sep 2017 11:11:41 +0100 Subject: [PATCH 2/3] Reanmed the link table to be a bteer description of what the table is for as it was previously confusing. Updated the migration script to reflect those changes --- app/models.py | 4 ++-- ...sender.py => 0123_add_noti_to_email_reply.py} | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) rename migrations/versions/{0123_add_noti_to_email_sender.py => 0123_add_noti_to_email_reply.py} (65%) diff --git a/app/models.py b/app/models.py index e0c56f36a..1103ce4f7 100644 --- a/app/models.py +++ b/app/models.py @@ -1416,8 +1416,8 @@ class ServiceLetterContact(db.Model): } -class NotificationSmsSender(db.Model): - __tablename__ = "notification_to_email_sender" +class NotificationEmailReplyTo(db.Model): + __tablename__ = "notification_to_email_reply_to" notification_id = db.Column( UUID(as_uuid=True), diff --git a/migrations/versions/0123_add_noti_to_email_sender.py b/migrations/versions/0123_add_noti_to_email_reply.py similarity index 65% rename from migrations/versions/0123_add_noti_to_email_sender.py rename to migrations/versions/0123_add_noti_to_email_reply.py index 2a49df3da..e7484b327 100644 --- a/migrations/versions/0123_add_noti_to_email_sender.py +++ b/migrations/versions/0123_add_noti_to_email_reply.py @@ -1,6 +1,6 @@ """ -Revision ID: 0123_add_noti_to_email_sender +Revision ID: 0123_add_noti_to_email_reply Revises: 0122_add_service_letter_contact Create Date: 2017-09-27 09:42:39.412731 @@ -9,23 +9,23 @@ from alembic import op import sqlalchemy as sa from sqlalchemy.dialects import postgresql -revision = '0123_add_noti_to_email_sender' +revision = '0123_add_noti_to_email_reply' down_revision = '0122_add_service_letter_contact' def upgrade(): - op.create_table('notification_to_email_sender', + op.create_table('notification_to_email_reply_to', sa.Column('notification_id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('service_email_reply_to_id', postgresql.UUID(as_uuid=True), nullable=False), sa.ForeignKeyConstraint(['notification_id'], ['notifications.id'], ), sa.ForeignKeyConstraint(['service_email_reply_to_id'], ['service_email_reply_to.id'], ), sa.PrimaryKeyConstraint('notification_id', 'service_email_reply_to_id') ) - op.create_index(op.f('ix_notification_to_email_sender_notification_id'), 'notification_to_email_sender', ['notification_id'], unique=False) - op.create_index(op.f('ix_notification_to_email_sender_service_email_reply_to_id'), 'notification_to_email_sender', ['service_email_reply_to_id'], unique=False) + op.create_index(op.f('ix_notification_to_email_sender_notification_id'), 'notification_to_email_reply_to', ['notification_id'], unique=False) + op.create_index(op.f('ix_notification_to_email_sender_service_email_reply_to_id'), 'notification_to_email_reply_to', ['service_email_reply_to_id'], unique=False) def downgrade(): - op.drop_index(op.f('ix_notification_to_email_sender_service_email_reply_to_id'), table_name='notification_to_email_sender') - op.drop_index(op.f('ix_notification_to_email_sender_notification_id'), table_name='notification_to_email_sender') - op.drop_table('notification_to_email_sender') + op.drop_index(op.f('ix_notification_to_email_sender_service_email_reply_to_id'), table_name='notification_to_email_reply_to') + op.drop_index(op.f('ix_notification_to_email_sender_notification_id'), table_name='notification_to_email_reply_to') + op.drop_table('notification_to_email_reply_to') From d8e1a34610bd0ba7a498a3bdbae936c77e768ed8 Mon Sep 17 00:00:00 2001 From: Richard Chapman Date: Tue, 3 Oct 2017 11:03:31 +0100 Subject: [PATCH 3/3] Added a unique constraint to the notification_id column of the notification_to_email_reply_to table so that each notification can only have one mapping to service_email_reply_to and hence one email address. --- app/models.py | 2 +- migrations/versions/0123_add_noti_to_email_reply.py | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/app/models.py b/app/models.py index 1103ce4f7..d1909d67c 100644 --- a/app/models.py +++ b/app/models.py @@ -1422,7 +1422,7 @@ class NotificationEmailReplyTo(db.Model): notification_id = db.Column( UUID(as_uuid=True), db.ForeignKey('notifications.id'), - unique=False, + unique=True, index=True, nullable=False, primary_key=True diff --git a/migrations/versions/0123_add_noti_to_email_reply.py b/migrations/versions/0123_add_noti_to_email_reply.py index e7484b327..f16c1605d 100644 --- a/migrations/versions/0123_add_noti_to_email_reply.py +++ b/migrations/versions/0123_add_noti_to_email_reply.py @@ -21,11 +21,10 @@ def upgrade(): sa.ForeignKeyConstraint(['service_email_reply_to_id'], ['service_email_reply_to.id'], ), sa.PrimaryKeyConstraint('notification_id', 'service_email_reply_to_id') ) - op.create_index(op.f('ix_notification_to_email_sender_notification_id'), 'notification_to_email_reply_to', ['notification_id'], unique=False) - op.create_index(op.f('ix_notification_to_email_sender_service_email_reply_to_id'), 'notification_to_email_reply_to', ['service_email_reply_to_id'], unique=False) - + op.create_index(op.f('ix_notification_to_email_reply_to_notification_id'), 'notification_to_email_reply_to', ['notification_id'], unique=True) + op.create_index(op.f('ix_notification_to_email_reply_to_service_email_reply_to_id'), 'notification_to_email_reply_to', ['service_email_reply_to_id'], unique=False) def downgrade(): - op.drop_index(op.f('ix_notification_to_email_sender_service_email_reply_to_id'), table_name='notification_to_email_reply_to') - op.drop_index(op.f('ix_notification_to_email_sender_notification_id'), table_name='notification_to_email_reply_to') + op.drop_index(op.f('ix_notification_to_email_reply_to_service_email_reply_to_id'), table_name='notification_to_email_reply_to') + op.drop_index(op.f('ix_notification_to_email_reply_to_notification_id'), table_name='notification_to_email_reply_to') op.drop_table('notification_to_email_reply_to')