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.
This commit is contained in:
Katie Smith
2020-03-12 14:29:39 +00:00
parent ae5b0e143c
commit 186670e10e
4 changed files with 155 additions and 11 deletions

View File

@@ -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
@@ -1352,10 +1352,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',
}
@@ -1432,7 +1435,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
@@ -1697,13 +1700,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)