0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Assigned existing email records and related posts to the default newsletter (#14695)

refs https://github.com/TryGhost/Team/issues/1571

- With the addition of multiple newsletters, all emails sent previously should be assigned to the default newsletter
- This will make sure that the sent count for the default newsletter displays correctly
This commit is contained in:
Matt Hanley 2022-05-09 12:48:24 +01:00 committed by GitHub
parent 895ffee90b
commit e5c5661701
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,44 @@
const logging = require('@tryghost/logging');
const {createTransactionalMigration} = require('../../utils');
module.exports = createTransactionalMigration(
async function up(knex) {
logging.info('Linking existing emails and related posts to the default newsletter');
// Get the default newsletter
// Note we intentionally use the default newsletter slug instead of the usual orderBy logic
let newsletter = await knex('newsletters')
.where('slug', 'default-newsletter')
.first('id', 'slug');
if (!newsletter) {
// Fall back to orderBy - just in case
logging.warn(`Original default newsletter not found - using first in sort order`);
newsletter = await knex('newsletters')
.where('status', 'active')
.orderBy('sort_order', 'asc')
.orderBy('created_at', 'asc')
.orderBy('id', 'asc')
.first('id', 'slug');
}
if (!newsletter) {
logging.error(`Newsletter not found - skipping`);
return;
}
logging.info(`Assigning existing emails to newsletter ID ${newsletter.id} (${newsletter.slug})`);
// Set newsletter ID only on posts with related email records without a newsletter assigned
await knex('posts')
.update('newsletter_id', newsletter.id)
.whereIn('id', knex.raw('SELECT post_id FROM emails WHERE emails.newsletter_id IS NULL'));
await knex('emails')
.update('newsletter_id', newsletter.id)
.whereNull('newsletter_id');
},
async function down() {
// Not required
}
);