-
-
Notifications
You must be signed in to change notification settings - Fork 11.4k
Dropped automated_emails table
#27186
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
EvanHahn
wants to merge
4
commits into
main
Choose a base branch
from
NY-1188-part-4
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
4 commits
Select commit
Hold shift + click to select a range
e534b85
Added dormant tables for welcome email automations
EvanHahn f82570f
Added dormant welcome email models
EvanHahn 87c417f
Switched from automated emails table to welcome email automation tables
EvanHahn 1ccf85b
Dropped `automated_emails` table
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
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
59 changes: 59 additions & 0 deletions
59
.../data/migrations/versions/6.27/2026-04-06-14-27-21-add-welcome-email-automation-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,59 @@ | ||
| const logging = require('@tryghost/logging'); | ||
| const {commands} = require('../../../schema'); | ||
| const {createNonTransactionalMigration} = require('../../utils'); | ||
|
|
||
| // The default names are too long. | ||
| const WELCOME_EMAIL_AUTOMATED_EMAILS_AUTOMATION_FK = 'weae_automation_id_foreign'; | ||
| const WELCOME_EMAIL_AUTOMATED_EMAILS_NEXT_EMAIL_FK = 'weae_next_email_id_foreign'; | ||
|
|
||
| const welcomeEmailAutomationsSpec = { | ||
| id: {type: 'string', maxlength: 24, nullable: false, primary: true}, | ||
| status: {type: 'string', maxlength: 50, nullable: false, defaultTo: 'inactive', validations: {isIn: [['active', 'inactive']]}}, | ||
| name: {type: 'string', maxlength: 191, nullable: false, unique: true}, | ||
| slug: {type: 'string', maxlength: 191, nullable: false, unique: true}, | ||
| created_at: {type: 'dateTime', nullable: false}, | ||
| updated_at: {type: 'dateTime', nullable: true} | ||
| }; | ||
|
|
||
| const welcomeEmailAutomatedEmailsSpec = { | ||
| id: {type: 'string', maxlength: 24, nullable: false, primary: true}, | ||
| welcome_email_automation_id: {type: 'string', maxlength: 24, nullable: false, references: 'welcome_email_automations.id', constraintName: WELCOME_EMAIL_AUTOMATED_EMAILS_AUTOMATION_FK, cascadeDelete: true}, | ||
| next_welcome_email_automated_email_id: {type: 'string', maxlength: 24, nullable: true, references: 'welcome_email_automated_emails.id', constraintName: WELCOME_EMAIL_AUTOMATED_EMAILS_NEXT_EMAIL_FK, cascadeDelete: false}, | ||
| delay_days: {type: 'integer', nullable: false}, | ||
| subject: {type: 'string', maxlength: 300, nullable: false}, | ||
| lexical: {type: 'text', maxlength: 1000000000, fieldtype: 'long', nullable: true}, | ||
| sender_name: {type: 'string', maxlength: 191, nullable: true}, | ||
| sender_email: {type: 'string', maxlength: 191, nullable: true, validations: {isEmail: true}}, | ||
| sender_reply_to: {type: 'string', maxlength: 191, nullable: true, validations: {isEmail: true}}, | ||
| email_design_setting_id: {type: 'string', maxlength: 24, nullable: false, references: 'email_design_settings.id'}, | ||
| created_at: {type: 'dateTime', nullable: false}, | ||
| updated_at: {type: 'dateTime', nullable: true} | ||
| }; | ||
|
|
||
| module.exports = createNonTransactionalMigration( | ||
| async function up(knex) { | ||
| const automationsExists = await knex.schema.hasTable('welcome_email_automations'); | ||
| if (automationsExists) { | ||
| logging.warn('Skipping creating table welcome_email_automations - already exists'); | ||
| } else { | ||
| logging.info('Creating table: welcome_email_automations'); | ||
| await commands.createTable('welcome_email_automations', knex, welcomeEmailAutomationsSpec); | ||
| } | ||
|
|
||
| const automatedEmailsExists = await knex.schema.hasTable('welcome_email_automated_emails'); | ||
| if (automatedEmailsExists) { | ||
| logging.warn('Skipping creating table welcome_email_automated_emails - already exists'); | ||
| } else { | ||
| logging.info('Creating table: welcome_email_automated_emails'); | ||
| await commands.createTable('welcome_email_automated_emails', knex, welcomeEmailAutomatedEmailsSpec); | ||
| } | ||
| }, | ||
|
|
||
| async function down(knex) { | ||
| logging.info('Dropping table: welcome_email_automated_emails'); | ||
| await commands.deleteTable('welcome_email_automated_emails', knex); | ||
|
|
||
| logging.info('Dropping table: welcome_email_automations'); | ||
| await commands.deleteTable('welcome_email_automations', knex); | ||
| } | ||
| ); |
104 changes: 104 additions & 0 deletions
104
...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,104 @@ | ||
| const logging = require('@tryghost/logging'); | ||
| const {commands} = require('../../../schema'); | ||
| 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 and re-points the FK on | ||
| // automated_email_recipients. | ||
|
|
||
| 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 | ||
| }); | ||
| } | ||
|
|
||
| // Update FK on automated_email_recipients: automated_emails -> welcome_email_automated_emails | ||
| 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) { | ||
| // Restore FK on automated_email_recipients back to automated_emails | ||
| 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 | ||
| }); | ||
|
|
||
| // 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(); | ||
| } | ||
| ); |
46 changes: 46 additions & 0 deletions
46
...e/server/data/migrations/versions/6.27/2026-04-06-16-29-31-drop-automated-emails-table.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,46 @@ | ||
| const logging = require('@tryghost/logging'); | ||
| const {commands} = require('../../../schema'); | ||
| const {createNonTransactionalMigration} = require('../../utils'); | ||
|
|
||
| const oldAutomatedEmailsSpec = { | ||
| id: {type: 'string', maxlength: 24, nullable: false, primary: true}, | ||
| status: {type: 'string', maxlength: 50, nullable: false, defaultTo: 'inactive', validations: {isIn: [['active', 'inactive']]}}, | ||
| name: {type: 'string', maxlength: 191, nullable: false, unique: true}, | ||
| slug: {type: 'string', maxlength: 191, nullable: false, unique: true}, | ||
| subject: {type: 'string', maxlength: 300, nullable: false}, | ||
| lexical: {type: 'text', maxlength: 1000000000, fieldtype: 'long', nullable: true}, | ||
| sender_name: {type: 'string', maxlength: 191, nullable: true}, | ||
| sender_email: {type: 'string', maxlength: 191, nullable: true, validations: {isEmail: true}}, | ||
| sender_reply_to: {type: 'string', maxlength: 191, nullable: true, validations: {isEmail: true}}, | ||
| email_design_setting_id: {type: 'string', maxlength: 24, nullable: false, references: 'email_design_settings.id'}, | ||
| created_at: {type: 'dateTime', nullable: false}, | ||
| updated_at: {type: 'dateTime', nullable: true}, | ||
| '@@INDEXES@@': [ | ||
| ['slug'], | ||
| ['status'] | ||
| ] | ||
| }; | ||
|
|
||
| module.exports = createNonTransactionalMigration( | ||
| async function up(knex) { | ||
| const exists = await knex.schema.hasTable('automated_emails'); | ||
| if (!exists) { | ||
| logging.warn('Skipping dropping table automated_emails - does not exist'); | ||
| return; | ||
| } | ||
|
|
||
| logging.info('Dropping table: automated_emails'); | ||
| await commands.deleteTable('automated_emails', knex); | ||
| }, | ||
|
|
||
| async function down(knex) { | ||
| const exists = await knex.schema.hasTable('automated_emails'); | ||
| if (exists) { | ||
| logging.warn('Skipping recreating table automated_emails - already exists'); | ||
| return; | ||
| } | ||
|
|
||
| logging.info('Recreating table: automated_emails'); | ||
| await commands.createTable('automated_emails', knex, oldAutomatedEmailsSpec); | ||
| } | ||
| ); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: use the dropTables migration util