Migrated members_status_events for comped members

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

Since version 4.6 the 'comped' status has not been used. Any members
which were given complimentary plans since then will have had a `status`
of 'paid', and therefore the corresponding members_status_events row
would have a `to_status` of 'paid'.

This migration is designed to fix these members_status_events rows by
ensuring that the last (chronologically) members_status_event row for a
comped member has a to status of 'comped'.

Unfortuantely this migration loses information which makes writing a
perfect inverse migraion impossible. Alternative down migrations were
considered, but these would lose further information.
This commit is contained in:
Fabien O'Carroll 2021-07-01 19:38:19 +01:00 committed by Fabien 'egg' O'Carroll
parent 47cf21514e
commit 8a87eb9e36

View File

@ -0,0 +1,38 @@
const logging = require('@tryghost/logging');
const {createTransactionalMigration} = require('../../utils.js');
module.exports = createTransactionalMigration(
async function up(knex) {
logging.info('Updating members_status_events for comped members');
const compedMembers = await knex('members')
.select('id')
.where('status', 'comped');
if (compedMembers.length === 0) {
logging.info('No comped members found - skipping migration');
return;
} else {
logging.info(`Found ${compedMembers.length} comped members - checking members_status_events`);
}
for (const member of compedMembers) {
const mostRecentStatusEvent = await knex('members_status_events')
.select('*')
.where('member_id', member.id)
.orderBy('created_at', 'desc')
.limit(1)
.first();
if (!mostRecentStatusEvent) {
logging.warn(`Could not find a status event for member ${member.id} - skipping this member`);
} else if (mostRecentStatusEvent.to_status !== 'comped') {
logging.info(`Updating members_status_event ${mostRecentStatusEvent.id}`);
await knex('members_status_events')
.update('to_status', 'comped')
.where('id', mostRecentStatusEvent.id);
}
}
},
async function down() {}
);