mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 17:31:14 -05:00
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 from74961781). Adding a normal constraint acquires an ACCESS EXCLUSIVE lock, but validate constraint only needs a SHARE UPDATE EXCLUSIVE lock. see9d4b8961and0a50993ffor more information on marking constraints as not valid.
This commit is contained in:
44
migrations/versions/0230_noti_postage_constraint.py
Normal file
44
migrations/versions/0230_noti_postage_constraint.py
Normal 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')
|
||||
Reference in New Issue
Block a user