From 7fd52017d019c43bc32397ec554b24046d862112 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Tue, 12 May 2020 16:25:14 +0100 Subject: [PATCH] Update postage db constraints for international letters The `notifications`, `notification_history`, `templates` and `templates_history` tables all had a check constraint on the postage column which specified that the postage had to be `first` or `second` if the notification or template was a letter. We now have two more options for postage - `europe` and `rest-of-world`. It's not possible to alter a check constraint, so the constraints have to be dropped then recreated. We are not recreating the constraint on the `notification_history` table since values here are always copied from the `notifications` table. The constraints get added as `NOT VALID` at first - this stage will lock the tables, so updating the `notification` table and `templates` and `templates_history` are done in separate migrations so that we don't lock all tables at the same time. In a third migration we then run `VALIDATE CONSTRAINT` for all tables - this will lock a row at a time, not the whole table. --- app/models.py | 18 ++-- .../0321_update_postage_constraint_1.py | 83 +++++++++++++++++++ .../0322_update_postage_constraint_2.py | 44 ++++++++++ .../0323_update_postage_constraint_3.py | 22 +++++ 4 files changed, 156 insertions(+), 11 deletions(-) create mode 100644 migrations/versions/0321_update_postage_constraint_1.py create mode 100644 migrations/versions/0322_update_postage_constraint_2.py create mode 100644 migrations/versions/0323_update_postage_constraint_3.py diff --git a/app/models.py b/app/models.py index 4c24fde92..d05145894 100644 --- a/app/models.py +++ b/app/models.py @@ -903,7 +903,7 @@ class TemplateBase(db.Model): postage = db.Column(db.String, nullable=True) CheckConstraint(""" CASE WHEN template_type = 'letter' THEN - postage is not null and postage in ('first', 'second') + postage is not null and postage in ('first', 'second', 'europe', 'rest-of-world') ELSE postage is null END @@ -1353,10 +1353,13 @@ DVLA_RESPONSE_STATUS_SENT = 'Sent' FIRST_CLASS = 'first' SECOND_CLASS = 'second' -POSTAGE_TYPES = [FIRST_CLASS, SECOND_CLASS] +EUROPE = 'europe' +REST_OF_WORLD = 'rest-of-world' RESOLVE_POSTAGE_FOR_FILE_NAME = { FIRST_CLASS: 1, - SECOND_CLASS: 2 + SECOND_CLASS: 2, + EUROPE: 'E', + REST_OF_WORLD: 'N', } @@ -1433,7 +1436,7 @@ class Notification(db.Model): postage = db.Column(db.String, nullable=True) CheckConstraint(""" CASE WHEN notification_type = 'letter' THEN - postage is not null and postage in ('first', 'second') + postage is not null and postage in ('first', 'second', 'europe', 'rest-of-world') ELSE postage is null END @@ -1698,13 +1701,6 @@ class NotificationHistory(db.Model, HistoryModel): created_by_id = db.Column(UUID(as_uuid=True), nullable=True) postage = db.Column(db.String, nullable=True) - CheckConstraint(""" - CASE WHEN notification_type = 'letter' THEN - postage is not null and postage in ('first', 'second') - ELSE - postage is null - END - """) document_download_count = db.Column(db.Integer, nullable=True) diff --git a/migrations/versions/0321_update_postage_constraint_1.py b/migrations/versions/0321_update_postage_constraint_1.py new file mode 100644 index 000000000..15d7f27d5 --- /dev/null +++ b/migrations/versions/0321_update_postage_constraint_1.py @@ -0,0 +1,83 @@ +""" + +Revision ID: 0321_update_postage_constraint_1 +Revises: 0320_optimise_notifications +Create Date: 2020-05-12 16:17:21.874281 + +""" +import os + +from alembic import op + + +revision = '0321_update_postage_constraint_1' +down_revision = '0320_optimise_notifications' +environment = os.environ['NOTIFY_ENVIRONMENT'] + + +def upgrade(): + op.drop_constraint('chk_notifications_postage_null', 'notifications') + op.execute(""" + ALTER TABLE notifications ADD CONSTRAINT "chk_notifications_postage_null" + CHECK ( + CASE WHEN notification_type = 'letter' THEN + postage is not null and postage in ('first', 'second', 'europe', 'rest-of-world') + ELSE + postage is null + END + ) + NOT VALID + """) + if environment not in ["live", "production"]: + op.execute('ALTER TABLE notification_history DROP CONSTRAINT IF EXISTS chk_notification_history_postage_null') + + +def downgrade(): + pass + # To downgrade this migration and migrations 0322 and 0323 * LOCALLY ONLY * use the following code. + # This should not be used in production - it will lock the tables for a long time + # + # op.drop_constraint('chk_notifications_postage_null', 'notifications') + # op.drop_constraint('chk_templates_postage', 'templates') + # op.drop_constraint('chk_templates_history_postage', 'templates_history') + # + # op.execute(""" + # ALTER TABLE notifications ADD CONSTRAINT "chk_notifications_postage_null" + # CHECK ( + # CASE WHEN notification_type = 'letter' THEN + # postage is not null and postage in ('first', 'second') + # ELSE + # postage is null + # END + # ) + # """) + # op.execute(""" + # ALTER TABLE notification_history ADD CONSTRAINT "chk_notification_history_postage_null" + # CHECK ( + # CASE WHEN notification_type = 'letter' THEN + # postage is not null and postage in ('first', 'second') + # ELSE + # postage is null + # END + # ) + # """) + # op.execute(""" + # ALTER TABLE templates ADD CONSTRAINT "chk_templates_postage" + # CHECK ( + # CASE WHEN template_type = 'letter' THEN + # postage is not null and postage in ('first', 'second') + # ELSE + # postage is null + # END + # ) + # """) + # op.execute(""" + # ALTER TABLE templates_history ADD CONSTRAINT "chk_templates_history_postage" + # CHECK ( + # CASE WHEN template_type = 'letter' THEN + # postage is not null and postage in ('first', 'second') + # ELSE + # postage is null + # END + # ) + # """) diff --git a/migrations/versions/0322_update_postage_constraint_2.py b/migrations/versions/0322_update_postage_constraint_2.py new file mode 100644 index 000000000..9fbfdac5b --- /dev/null +++ b/migrations/versions/0322_update_postage_constraint_2.py @@ -0,0 +1,44 @@ +""" + +Revision ID: 0322_update_postage_constraint_2 +Revises: 0321_update_postage_constraint_1 +Create Date: 2020-05-12 16:20:16.548347 + +""" +from alembic import op + + +revision = '0322_update_postage_constraint_2' +down_revision = '0321_update_postage_constraint_1' + + +def upgrade(): + op.drop_constraint('chk_templates_postage', 'templates') + op.drop_constraint('chk_templates_history_postage', 'templates_history') + + op.execute(""" + ALTER TABLE templates ADD CONSTRAINT "chk_templates_postage" + CHECK ( + CASE WHEN template_type = 'letter' THEN + postage is not null and postage in ('first', 'second', 'europe', 'rest-of-world') + ELSE + postage is null + END + ) + NOT VALID + """) + op.execute(""" + ALTER TABLE templates_history ADD CONSTRAINT "chk_templates_history_postage" + CHECK ( + CASE WHEN template_type = 'letter' THEN + postage is not null and postage in ('first', 'second', 'europe', 'rest-of-world') + ELSE + postage is null + END + ) + NOT VALID + """) + + +def downgrade(): + pass diff --git a/migrations/versions/0323_update_postage_constraint_3.py b/migrations/versions/0323_update_postage_constraint_3.py new file mode 100644 index 000000000..33d76c127 --- /dev/null +++ b/migrations/versions/0323_update_postage_constraint_3.py @@ -0,0 +1,22 @@ +""" + +Revision ID: 0323_update_postage_constraint_3 +Revises: 0322_update_postage_constraint_2 +Create Date: 2020-05-12 16:21:56.210025 + +""" +from alembic import op + + +revision = '0323_update_postage_constraint_3' +down_revision = '0322_update_postage_constraint_2' + + +def upgrade(): + op.execute('ALTER TABLE notifications VALIDATE CONSTRAINT "chk_notifications_postage_null"') + op.execute('ALTER TABLE templates VALIDATE CONSTRAINT "chk_templates_postage"') + op.execute('ALTER TABLE templates_history VALIDATE CONSTRAINT "chk_templates_history_postage"') + + +def downgrade(): + pass