Added migration to populate recipient_filter

no-issue

This populates the recipient_filter column for existing emails
based on the visibility of the related post at the time of the
migration.
This commit is contained in:
Fabien O'Carroll 2020-11-04 12:27:21 +00:00 committed by Fabien 'egg' O'Carroll
parent 918c721bd1
commit 601b6ce174

View File

@ -0,0 +1,35 @@
const {createTransactionalMigration} = require('../../utils');
const logging = require('../../../../../shared/logging');
module.exports = createTransactionalMigration(
async function up(connection) {
logging.info('Updating emails.recipient_filter values based on posts.visibility');
const paidPostIds = (await connection('posts')
.select('id')
.where('visibility', 'paid')).map(row => row.id);
const membersPostIds = (await connection('posts')
.select('id')
.where('visibility', 'members')).map(row => row.id);
const publicPostIds = (await connection('posts')
.select('id')
.where('visibility', 'public')).map(row => row.id);
await connection('emails')
.update('recipient_filter', 'paid')
.whereIn('post_id', paidPostIds);
await connection('emails')
.update('recipient_filter', 'all')
.whereIn('post_id', membersPostIds.concat(publicPostIds));
},
async function down(connection) {
logging.info('Updating emails.recipient_filter values to paid');
await connection('emails')
.update('recipient_filter', 'paid');
}
);