-
Notifications
You must be signed in to change notification settings - Fork 91
ENG-3300: Add label column to MessagingTemplate for multi-template support #7900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JadeCara
wants to merge
18
commits into
main
Choose a base branch
from
ENG-3300/multi-template-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
432f58f
ENG-3300: Add label column to MessagingTemplate for multi-template su…
JadeCara 8a8e159
Add changelog for ENG-3300
JadeCara e5ac7df
Fix governor review findings: fixtures, endpoint labels, docstring
JadeCara bdcc7e0
Fix mypy: add missing label arg in get_messaging_template_by_id endpoint
JadeCara 8c90163
Add default value for label column to avoid NOT NULL violations
JadeCara ebea261
fix db yaml
JadeCara 6828b84
Merge branch 'main' into ENG-3300/multi-template-support
JadeCara d8e8cc0
fix db yaml
JadeCara 7161ac5
Fix messaging template tests for (type, label) unique constraint
JadeCara 5630023
improve test cov
JadeCara 3bb5e66
update validation and tests
JadeCara 2a6b87d
test clean up
JadeCara 44c552a
Fix label falsy-check and split combined test
JadeCara eeadc02
Merge branch 'main' into ENG-3300/multi-template-support
JadeCara fa7ca24
ENG-3300: Auto-number default labels for duplicate messaging templates
JadeCara 0515977
Merge branch 'main' into ENG-3300/multi-template-support
JadeCara 34bb853
Merge branch 'main' into ENG-3300/multi-template-support
JadeCara 4807dd8
Address PR review feedback for ENG-3300 multi-template support
JadeCara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| type: Added | ||
| description: | | ||
| Add label column to MessagingTemplate model enabling multiple named | ||
| templates per MessagingActionType. Includes migration with backfill, | ||
| unique constraint on (type, label), and get_templates_by_type query. | ||
| pr: 7900 | ||
| labels: ["db-migration"] |
101 changes: 101 additions & 0 deletions
101
...ic/migrations/versions/xx_2026_04_10_1800_d71c7d274c04_add_label_to_messaging_template.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| """Add label column to messaging_template | ||
|
|
||
| Revision ID: d71c7d274c04 | ||
| Revises: a42ef09a3dfe | ||
| Create Date: 2026-04-10 18:00:00.000000 | ||
|
|
||
| """ | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
| revision = "d71c7d274c04" | ||
| down_revision = "a42ef09a3dfe" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
| # Default labels at the time this migration was written (2026-04-10). | ||
| # Intentionally hardcoded — migrations must not import application code. | ||
| DEFAULT_LABELS = { | ||
| "subject_identity_verification": "Subject identity verification", | ||
| "privacy_request_receipt": "Privacy request received", | ||
| "privacy_request_review_approve": "Privacy request approved", | ||
| "privacy_request_review_deny": "Privacy request denied", | ||
| "privacy_request_complete_access": "Access request completed", | ||
| "privacy_request_complete_deletion": "Erasure request completed", | ||
| "privacy_request_complete_consent": "Consent request completed", | ||
| "manual_task_digest": "Manual task digest", | ||
| "external_user_welcome": "External user welcome", | ||
| } | ||
|
|
||
|
|
||
| def upgrade(): | ||
| # Phase 1: Add nullable column | ||
| op.add_column( | ||
| "messaging_template", | ||
| sa.Column("label", sa.String(), nullable=True), | ||
| ) | ||
|
|
||
| # Phase 2: Backfill labels | ||
| conn = op.get_bind() | ||
|
|
||
| # Backfill from known defaults | ||
| for template_type, label in DEFAULT_LABELS.items(): | ||
| conn.execute( | ||
| sa.text( | ||
| "UPDATE messaging_template SET label = :label " | ||
| "WHERE type = :type AND label IS NULL" | ||
| ), | ||
| {"label": label, "type": template_type}, | ||
| ) | ||
|
|
||
| # Backfill any remaining rows (types not in DEFAULT_LABELS) with title-cased type | ||
| conn.execute( | ||
| sa.text( | ||
| "UPDATE messaging_template SET label = INITCAP(REPLACE(type, '_', ' ')) " | ||
| "WHERE label IS NULL" | ||
| ) | ||
| ) | ||
|
|
||
| # Deduplicate: if multiple rows share (type, label), append a suffix | ||
| dupes = conn.execute( | ||
| sa.text( | ||
| "SELECT type, label FROM messaging_template " | ||
| "GROUP BY type, label HAVING COUNT(*) > 1" | ||
| ) | ||
| ).fetchall() | ||
|
|
||
| for template_type, label in dupes: | ||
| rows = conn.execute( | ||
| sa.text( | ||
| "SELECT id FROM messaging_template " | ||
| "WHERE type = :type AND label = :label " | ||
| "ORDER BY updated_at DESC" | ||
| ), | ||
| {"type": template_type, "label": label}, | ||
| ).fetchall() | ||
| # Keep the first (most recently updated) as-is, suffix the rest | ||
| for i, row in enumerate(rows[1:], start=2): | ||
| conn.execute( | ||
| sa.text( | ||
| "UPDATE messaging_template SET label = :new_label WHERE id = :id" | ||
| ), | ||
| {"new_label": f"{label} ({i})", "id": row[0]}, | ||
| ) | ||
|
|
||
| # Phase 3: Set NOT NULL and add unique constraint | ||
| op.alter_column("messaging_template", "label", nullable=False) | ||
| op.create_unique_constraint( | ||
| "uq_messaging_template_type_label", | ||
| "messaging_template", | ||
| ["type", "label"], | ||
| ) | ||
|
|
||
|
|
||
| def downgrade(): | ||
| op.drop_constraint( | ||
| "uq_messaging_template_type_label", | ||
| "messaging_template", | ||
| type_="unique", | ||
| ) | ||
| op.drop_column("messaging_template", "label") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.