From 0a50993fc23c14cccf21cf5383a694f09c665b22 Mon Sep 17 00:00:00 2001 From: Alexey Bezhan Date: Wed, 15 Nov 2017 14:48:50 +0000 Subject: [PATCH] Validate notifcations.template_history foreign key constraints Removes "NOT VALID" mark from the notifications -> template_history foreign key constraint by validating existing records. From postgres docs: > This form validates a foreign key or check constraint that was > previously created as NOT VALID, by scanning the table to ensure there > are no rows for which the constraint is not satisfied. Nothing happens > if the constraint is already marked valid. > Validation can be a long process on larger tables. The value of > separating validation from initial creation is that you can defer > validation to less busy times, or can be used to give additional time > to correct pre-existing errors while preventing new errors. Note also > that validation on its own does not prevent normal write commands > against the table while it runs. > Validation acquires only a SHARE UPDATE EXCLUSIVE lock on the table > being altered. If the constraint is a foreign key then a ROW SHARE > lock is also required on the table referenced by the constraint. --- .../versions/0142_validate_constraints.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 migrations/versions/0142_validate_constraints.py diff --git a/migrations/versions/0142_validate_constraints.py b/migrations/versions/0142_validate_constraints.py new file mode 100644 index 000000000..dea88b707 --- /dev/null +++ b/migrations/versions/0142_validate_constraints.py @@ -0,0 +1,21 @@ +""" + +Revision ID: 0142_validate_constraint +Revises: 0141_remove_unused +Create Date: 2017-11-15 14:39:13.657666 + +""" +from alembic import op +from sqlalchemy.dialects import postgresql + +revision = '0142_validate_constraint' +down_revision = '0141_remove_unused' + + +def upgrade(): + op.execute('ALTER TABLE notifications VALIDATE CONSTRAINT "notifications_templates_history_fkey"') + op.execute('ALTER TABLE notification_history VALIDATE CONSTRAINT "notification_history_templates_history_fkey"') + + +def downgrade(): + pass