-
-
Notifications
You must be signed in to change notification settings - Fork 11.4k
Switched from automated emails table to welcome email automation tables #27185
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
daf2c9a
Switched from automated emails table to welcome email automation tables
EvanHahn 46ccd2a
Split FK changes into separate migration
EvanHahn d9dc761
Check relation by ID property, not object truthiness
EvanHahn 88e7de6
Merge branch 'main' into NY-1188-part-3
EvanHahn 2d773f0
Removed redundant variable assignment
EvanHahn 9eecec4
Change logging when saving WelcomeEmailAutomation
EvanHahn d2cd9c7
Merge branch 'main' into NY-1188-part-3
EvanHahn e0b7217
Don't look up associated email twice
EvanHahn c6be57a
Fixed some broken tests for log messages
EvanHahn db9a8a1
Updates based on recent commit
EvanHahn 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
66 changes: 66 additions & 0 deletions
66
...ons/versions/6.27/2026-04-06-15-55-20-split-automated-emails-into-welcome-email-tables.js
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,66 @@ | ||
| const logging = require('@tryghost/logging'); | ||
| const {createTransactionalMigration} = require('../../utils'); | ||
| const ObjectId = require('bson-objectid').default; | ||
|
|
||
| module.exports = createTransactionalMigration( | ||
| async function up(knex) { | ||
| // The welcome_email_automations and welcome_email_automated_emails tables | ||
| // already exist from a prior dormant migration. This migration copies data | ||
| // from the old automated_emails table into them. | ||
|
|
||
| const oldTableExists = await knex.schema.hasTable('automated_emails'); | ||
| if (!oldTableExists) { | ||
| logging.warn('Skipping data migration - automated_emails table does not exist'); | ||
| return; | ||
| } | ||
|
|
||
| const rows = await knex('automated_emails').select('*'); | ||
| logging.info(`Migrating ${rows.length} rows from automated_emails to new tables`); | ||
|
|
||
| // Only 2 rows exist (free + paid welcome emails), so sequential iteration is fine | ||
| // eslint-disable-next-line no-restricted-syntax | ||
| for (const row of rows) { | ||
| // Check if already migrated (idempotency) by looking for a matching slug | ||
| const existingAutomation = await knex('welcome_email_automations').where('slug', row.slug).first(); | ||
| if (existingAutomation) { | ||
| logging.warn(`Skipping row for slug ${row.slug} - already migrated`); | ||
| continue; | ||
| } | ||
|
|
||
| const automationId = ObjectId().toHexString(); | ||
|
|
||
| // Insert automation first (emails reference automations via FK) | ||
| await knex('welcome_email_automations').insert({ | ||
| id: automationId, | ||
| status: row.status, | ||
| name: row.name, | ||
| slug: row.slug, | ||
| created_at: row.created_at, | ||
| updated_at: row.updated_at | ||
| }); | ||
|
|
||
| // Reuse the original automated_email id so the existing | ||
| // automated_email_recipients rows continue to reference the same id | ||
| await knex('welcome_email_automated_emails').insert({ | ||
| id: row.id, | ||
| welcome_email_automation_id: automationId, | ||
| delay_days: 0, | ||
| subject: row.subject, | ||
| lexical: row.lexical, | ||
| sender_name: row.sender_name, | ||
| sender_email: row.sender_email, | ||
| sender_reply_to: row.sender_reply_to, | ||
| email_design_setting_id: row.email_design_setting_id, | ||
| created_at: row.created_at, | ||
| updated_at: row.updated_at | ||
| }); | ||
| } | ||
| }, | ||
|
|
||
| async function down(knex) { | ||
| // Remove migrated data from new tables | ||
| logging.info('Removing migrated data from new tables'); | ||
| await knex('welcome_email_automated_emails').del(); | ||
| await knex('welcome_email_automations').del(); | ||
| } | ||
| ); | ||
79 changes: 79 additions & 0 deletions
79
...ations/versions/6.27/2026-04-07-15-10-32-update-automated-email-recipients-foreign-key.js
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,79 @@ | ||
| const logging = require('@tryghost/logging'); | ||
| const {commands} = require('../../../schema'); | ||
| const {createTransactionalMigration} = require('../../utils'); | ||
|
|
||
| module.exports = createTransactionalMigration( | ||
| async function up(knex) { | ||
| const recipientsTableExists = await knex.schema.hasTable('automated_email_recipients'); | ||
| if (!recipientsTableExists) { | ||
| logging.warn('Skipping foreign key migration - automated_email_recipients table does not exist'); | ||
| return; | ||
| } | ||
|
|
||
| const oldTableExists = await knex.schema.hasTable('automated_emails'); | ||
| if (!oldTableExists) { | ||
| logging.warn('Skipping foreign key migration - automated_emails table does not exist'); | ||
| return; | ||
| } | ||
|
|
||
| const newTableExists = await knex.schema.hasTable('welcome_email_automated_emails'); | ||
| if (!newTableExists) { | ||
| logging.warn('Skipping foreign key migration - welcome_email_automated_emails table does not exist'); | ||
| return; | ||
| } | ||
|
|
||
| logging.info('Updating foreign key on automated_email_recipients'); | ||
| await commands.dropForeign({ | ||
| fromTable: 'automated_email_recipients', | ||
| fromColumn: 'automated_email_id', | ||
| toTable: 'automated_emails', | ||
| toColumn: 'id', | ||
| transaction: knex | ||
| }); | ||
|
|
||
| await commands.addForeign({ | ||
| fromTable: 'automated_email_recipients', | ||
| fromColumn: 'automated_email_id', | ||
| toTable: 'welcome_email_automated_emails', | ||
| toColumn: 'id', | ||
| transaction: knex | ||
| }); | ||
| }, | ||
|
|
||
| async function down(knex) { | ||
| const recipientsTableExists = await knex.schema.hasTable('automated_email_recipients'); | ||
| if (!recipientsTableExists) { | ||
| logging.warn('Skipping foreign key rollback - automated_email_recipients table does not exist'); | ||
| return; | ||
| } | ||
|
|
||
| const oldTableExists = await knex.schema.hasTable('automated_emails'); | ||
| if (!oldTableExists) { | ||
| logging.warn('Skipping foreign key rollback - automated_emails table does not exist'); | ||
| return; | ||
| } | ||
|
|
||
| const newTableExists = await knex.schema.hasTable('welcome_email_automated_emails'); | ||
| if (!newTableExists) { | ||
| logging.warn('Skipping foreign key rollback - welcome_email_automated_emails table does not exist'); | ||
| return; | ||
| } | ||
|
|
||
| logging.info('Restoring foreign key on automated_email_recipients'); | ||
| await commands.dropForeign({ | ||
| fromTable: 'automated_email_recipients', | ||
| fromColumn: 'automated_email_id', | ||
| toTable: 'welcome_email_automated_emails', | ||
| toColumn: 'id', | ||
| transaction: knex | ||
| }); | ||
|
|
||
| await commands.addForeign({ | ||
| fromTable: 'automated_email_recipients', | ||
| fromColumn: 'automated_email_id', | ||
| toTable: 'automated_emails', | ||
| toColumn: 'id', | ||
| transaction: knex | ||
| }); | ||
| } | ||
| ); |
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.