Drop postage constraints

The constaints on notifications and notification_history have already
been dropped in production, but still exist in staging and in dev
environments.

The constraints on templates and templates_history exist in all
environments.
This commit is contained in:
Katie Smith
2020-06-08 11:55:25 +01:00
parent 4b1705aa14
commit bdde035a0c
2 changed files with 69 additions and 35 deletions

View File

@@ -0,0 +1,69 @@
"""
Revision ID: 0321_drop_postage_constraints
Revises: 0320_optimise_notifications
Create Date: 2020-06-08 11:48:53.315768
"""
import os
from alembic import op
revision = '0321_drop_postage_constraints'
down_revision = '0320_optimise_notifications'
environment = os.environ['NOTIFY_ENVIRONMENT']
def upgrade():
if environment not in ["live", "production"]:
op.execute('ALTER TABLE notifications DROP CONSTRAINT IF EXISTS chk_notifications_postage_null')
op.execute('ALTER TABLE notification_history DROP CONSTRAINT IF EXISTS chk_notification_history_postage_null')
op.execute('ALTER TABLE templates DROP CONSTRAINT IF EXISTS chk_templates_postage')
op.execute('ALTER TABLE templates_history DROP CONSTRAINT IF EXISTS chk_templates_history_postage')
def downgrade():
# The downgrade command must not be run in production - it will lock the tables for a long time
if environment not in ["live", "production"]:
op.execute("""
ALTER TABLE notifications ADD CONSTRAINT "chk_notifications_postage_null"
CHECK (
CASE WHEN notification_type = 'letter' THEN
postage is not null and postage in ('first', 'second')
ELSE
postage is null
END
)
""")
op.execute("""
ALTER TABLE notification_history ADD CONSTRAINT "chk_notification_history_postage_null"
CHECK (
CASE WHEN notification_type = 'letter' THEN
postage is not null and postage in ('first', 'second')
ELSE
postage is null
END
)
""")
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
)
""")

View File

@@ -1,7 +1,6 @@
from datetime import datetime from datetime import datetime
from freezegun import freeze_time from freezegun import freeze_time
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.orm.exc import NoResultFound
import pytest import pytest
@@ -485,37 +484,3 @@ def test_get_template_versions_is_empty_for_hidden_templates(sample_service):
) )
versions = dao_get_template_versions(service_id=sample_template.service_id, template_id=sample_template.id) versions = dao_get_template_versions(service_id=sample_template.service_id, template_id=sample_template.id)
assert len(versions) == 0 assert len(versions) == 0
@pytest.mark.parametrize("template_type,postage", [('letter', 'third'), ('sms', 'second')])
def test_template_postage_constraint_on_create(sample_service, sample_user, template_type, postage):
data = {
'name': 'Sample Template',
'template_type': template_type,
'content': "Template content",
'service': sample_service,
'created_by': sample_user,
'postage': postage
}
template = Template(**data)
with pytest.raises(expected_exception=SQLAlchemyError):
dao_create_template(template)
def test_template_postage_constraint_on_update(sample_service, sample_user):
data = {
'name': 'Sample Template',
'template_type': "letter",
'content': "Template content",
'service': sample_service,
'created_by': sample_user,
'postage': 'second'
}
template = Template(**data)
dao_create_template(template)
created = dao_get_all_templates_for_service(sample_service.id)[0]
assert created.name == 'Sample Template'
created.postage = 'third'
with pytest.raises(expected_exception=SQLAlchemyError):
dao_update_template(created)