From 97b1e034b8028aff4566d0dcf24d3e1d41c803e9 Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Mon, 11 Feb 2019 18:22:29 +0000 Subject: [PATCH 1/2] Remove service.postage and choose_postage permission from database Also change constraint on template postage so it cannot be null for letters Also add postage to all letters in template_history to respect new constraint --- .../versions/0259_remove_service_postage.py | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 migrations/versions/0259_remove_service_postage.py diff --git a/migrations/versions/0259_remove_service_postage.py b/migrations/versions/0259_remove_service_postage.py new file mode 100644 index 000000000..65e18bf3a --- /dev/null +++ b/migrations/versions/0259_remove_service_postage.py @@ -0,0 +1,89 @@ +""" + +Revision ID: 0259_remove_service_postage +Revises: 0258_service_postage_nullable +Create Date: 2019-02-11 17:12:22.341599 + +""" +from alembic import op +import sqlalchemy as sa + + +revision = '0259_remove_service_postage' +down_revision = '0258_service_postage_nullable' + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('services', 'postage') + op.drop_column('services_history', 'postage') + op.execute("DELETE FROM service_permissions WHERE permission = 'choose_postage'") + op.execute("DELETE FROM service_permission_types WHERE name = 'choose_postage'") + op.execute( + """UPDATE templates_history SET postage = templates.postage + FROM templates WHERE templates_history.id = templates.id AND templates_history.template_type = 'letter' + AND templates_history.postage is null""" + ) + 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 + ) + """) + op.execute(""" + ALTER TABLE templates DROP CONSTRAINT "chk_templates_postage_null" + """) + op.execute(""" + ALTER TABLE templates_history DROP CONSTRAINT "chk_templates_history_postage_null" + """) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('services_history', sa.Column('postage', sa.VARCHAR(length=255), autoincrement=False, nullable=True)) + op.add_column('services', sa.Column('postage', sa.VARCHAR(length=255), autoincrement=False, nullable=False)) + op.execute("INSERT INTO service_permission_types VALUES ('choose_postage')") + op.execute(""" + ALTER TABLE templates ADD CONSTRAINT "chk_templates_postage_null" + CHECK ( + CASE WHEN template_type = 'letter' THEN + postage in ('first', 'second') OR + postage is null + ELSE + postage is null + END + ) + """) + op.execute(""" + ALTER TABLE templates_history ADD CONSTRAINT "chk_templates_history_postage_null" + CHECK ( + CASE WHEN template_type = 'letter' THEN + postage in ('first', 'second') OR + postage is null + ELSE + postage is null + END + ) + """) + op.execute(""" + ALTER TABLE templates DROP CONSTRAINT "chk_templates_postage" + """) + op.execute(""" + ALTER TABLE templates_history DROP CONSTRAINT "chk_templates_history_postage" + """) + # ### end Alembic commands ### From 4ce5724bc79ddf9b76401861a16f17585536b1b0 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Wed, 13 Feb 2019 15:40:08 +0000 Subject: [PATCH 2/2] Fix the downgrade of the migration. By the way I'm not concerned about fixing service.postage data if we have to rollback. --- migrations/versions/0259_remove_service_postage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrations/versions/0259_remove_service_postage.py b/migrations/versions/0259_remove_service_postage.py index 65e18bf3a..4f2992342 100644 --- a/migrations/versions/0259_remove_service_postage.py +++ b/migrations/versions/0259_remove_service_postage.py @@ -56,7 +56,7 @@ def upgrade(): def downgrade(): # ### commands auto generated by Alembic - please adjust! ### op.add_column('services_history', sa.Column('postage', sa.VARCHAR(length=255), autoincrement=False, nullable=True)) - op.add_column('services', sa.Column('postage', sa.VARCHAR(length=255), autoincrement=False, nullable=False)) + op.add_column('services', sa.Column('postage', sa.VARCHAR(length=255), autoincrement=False, nullable=True)) op.execute("INSERT INTO service_permission_types VALUES ('choose_postage')") op.execute(""" ALTER TABLE templates ADD CONSTRAINT "chk_templates_postage_null"