add postage constraint to notification history

A not valid constraint only checks against new rows, not existing rows.
We can call VALIDATE CONSTRAINT against this new constraint to check
the old rows (which we know are good, having run the command from
74961781). Adding a normal constraint acquires an ACCESS EXCLUSIVE
lock, but validate constraint only needs a SHARE UPDATE EXCLUSIVE lock.

see 9d4b8961 and 0a50993f for more information on marking constraints
as not valid.
This commit is contained in:
Leo Hemsted
2018-09-24 15:36:05 +01:00
parent d2962cdc10
commit 17612e5446
2 changed files with 58 additions and 2 deletions

View File

@@ -0,0 +1,44 @@
"""
Revision ID: 0230_noti_postage_constraint
Revises: 0229_new_letter_rates
Create Date: 2018-09-19 11:42:52.229430
"""
from alembic import op
revision = '0230_noti_postage_constraint'
down_revision = '0229_new_letter_rates'
def upgrade():
op.execute("""
ALTER TABLE notifications ADD CONSTRAINT "chk_notifications_postage_null"
CHECK (
CASE WHEN notification_type = 'letter' THEN
postage in ('first', 'second')
ELSE
postage is null
END
)
NOT VALID
""")
op.execute("""
ALTER TABLE notification_history ADD CONSTRAINT "chk_notification_history_postage_null"
CHECK (
CASE WHEN notification_type = 'letter' THEN
postage in ('first', 'second')
ELSE
postage is null
END
)
NOT VALID
""")
op.execute('ALTER TABLE notifications VALIDATE CONSTRAINT "chk_notifications_postage_null"')
op.execute('ALTER TABLE notification_history VALIDATE CONSTRAINT "chk_notification_history_postage_null"')
def downgrade():
op.drop_constraint('chk_notifications_postage_null', 'notifications', type_='check')
op.drop_constraint('chk_notification_history_postage_null', 'notification_history', type_='check')