mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 02:11:11 -05:00
Add unique constraint to email branding domains
Does two things:
1. Revert "Revert "Add unique constraint to email branding domain""
This reverts commit af9cb30ef3.
2. Don’t allow empty string in email branding domain
Columns with multiple `null`s can have a uniqueness constraint. Columns
with multiple empty string values are not considered unique.
This commit:
- removes any duplicate empty string values
- casts empty strings to null string any time these columns are updated
---
Squashed into this single commits because these two things are not
atomic as individual commits.
This commit is contained in:
@@ -23,5 +23,5 @@ def dao_create_email_branding(email_branding):
|
||||
@transactional
|
||||
def dao_update_email_branding(email_branding, **kwargs):
|
||||
for key, value in kwargs.items():
|
||||
setattr(email_branding, key, value)
|
||||
setattr(email_branding, key, value or None)
|
||||
db.session.add(email_branding)
|
||||
|
||||
@@ -207,7 +207,7 @@ class EmailBranding(db.Model):
|
||||
logo = db.Column(db.String(255), nullable=True)
|
||||
name = db.Column(db.String(255), nullable=True)
|
||||
text = db.Column(db.String(255), nullable=True)
|
||||
domain = db.Column(db.Text, nullable=True)
|
||||
domain = db.Column(db.Text, unique=True, nullable=True)
|
||||
brand_type = db.Column(
|
||||
db.String(255),
|
||||
db.ForeignKey('branding_type.name'),
|
||||
|
||||
28
migrations/versions/0223_add_domain_constraint.py
Normal file
28
migrations/versions/0223_add_domain_constraint.py
Normal file
@@ -0,0 +1,28 @@
|
||||
"""
|
||||
Revision ID: 0223_add_domain_constraint
|
||||
Revises: 0222_drop_service_branding
|
||||
Create Date: 2018-08-24 13:36:49.346156
|
||||
"""
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = '0223_add_domain_constraint'
|
||||
down_revision = '0222_drop_service_branding'
|
||||
|
||||
|
||||
def upgrade():
|
||||
|
||||
op.execute("""
|
||||
update
|
||||
email_branding
|
||||
set
|
||||
domain = null
|
||||
where
|
||||
domain = ''
|
||||
""")
|
||||
op.create_unique_constraint('uq_email_branding_domain', 'email_branding', ['domain'])
|
||||
|
||||
|
||||
def downgrade():
|
||||
|
||||
op.drop_constraint('uq_email_branding_domain', 'email_branding')
|
||||
@@ -51,3 +51,15 @@ def test_update_email_branding(notify_db, notify_db_session):
|
||||
|
||||
assert len(email_branding) == 1
|
||||
assert email_branding[0].name == updated_name
|
||||
|
||||
|
||||
def test_domain_cant_be_empty_string(notify_db, notify_db_session):
|
||||
create_email_branding()
|
||||
|
||||
email_branding = EmailBranding.query.all()
|
||||
assert email_branding[0].domain is None
|
||||
|
||||
dao_update_email_branding(email_branding[0], domain='')
|
||||
|
||||
email_branding = EmailBranding.query.all()
|
||||
assert email_branding[0].domain is None
|
||||
|
||||
Reference in New Issue
Block a user