From 1614ef697be41d719d6cdaf1dcf0c4224869dcd9 Mon Sep 17 00:00:00 2001 From: Eric Gullickson <16152721+ericgullickson@users.noreply.github.com> Date: Sat, 31 Jan 2026 20:22:53 -0600 Subject: [PATCH] fix: use upsert for tier change template migration (refs #59) Changed INSERT to INSERT...ON CONFLICT DO UPDATE so the migration works for: - Fresh deployments (inserts new template) - Existing databases (updates template to fix variable substitution) Removed unnecessary migration 008. Co-Authored-By: Claude Opus 4.5 --- .../007_subscription_tier_change_template.sql | 12 +++++++-- .../008_fix_tier_change_template.sql | 26 ------------------- 2 files changed, 10 insertions(+), 28 deletions(-) delete mode 100644 backend/src/features/notifications/migrations/008_fix_tier_change_template.sql diff --git a/backend/src/features/notifications/migrations/007_subscription_tier_change_template.sql b/backend/src/features/notifications/migrations/007_subscription_tier_change_template.sql index 1cd656c..8746ed8 100644 --- a/backend/src/features/notifications/migrations/007_subscription_tier_change_template.sql +++ b/backend/src/features/notifications/migrations/007_subscription_tier_change_template.sql @@ -17,7 +17,7 @@ CHECK (template_key IN ( 'subscription_tier_change' )); --- Insert subscription tier change email template +-- Insert or update subscription tier change email template INSERT INTO email_templates (template_key, name, description, subject, body, variables, html_body) VALUES ( 'subscription_tier_change', @@ -86,4 +86,12 @@ MotoVaultPro Team', ' - ); + ) +ON CONFLICT (template_key) DO UPDATE SET + name = EXCLUDED.name, + description = EXCLUDED.description, + subject = EXCLUDED.subject, + body = EXCLUDED.body, + variables = EXCLUDED.variables, + html_body = EXCLUDED.html_body, + updated_at = NOW(); diff --git a/backend/src/features/notifications/migrations/008_fix_tier_change_template.sql b/backend/src/features/notifications/migrations/008_fix_tier_change_template.sql deleted file mode 100644 index 31878be..0000000 --- a/backend/src/features/notifications/migrations/008_fix_tier_change_template.sql +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Migration: Fix subscription tier change email template - * @ai-summary Fixes template to use simple variable substitution instead of conditionals - * @ai-context TemplateService only supports {{variable}}, not Handlebars conditionals - */ - --- Update the existing template to use simple variable substitution -UPDATE email_templates -SET - body = 'Hi {{userName}}, - -Your MotoVaultPro subscription has been {{changeType}}. - -Previous Tier: {{previousTier}} -New Tier: {{newTier}} -Reason: {{reason}} - -{{additionalInfo}} - -If you have any questions, please contact support. - -Best regards, -MotoVaultPro Team', - variables = '["userName", "changeType", "previousTier", "newTier", "reason", "additionalInfo"]', - updated_at = NOW() -WHERE template_key = 'subscription_tier_change';