Updated status population to handle comped status (#12651)

refs https://github.com/TryGhost/Ghost/issues/12602

As part of collecting Member event data, we have added a third status
for members "comped" - this fixes the population of the column to handle
this
This commit is contained in:
Fabien 'egg' O'Carroll 2021-02-16 10:37:06 +00:00 committed by GitHub
parent 964fe222be
commit da9cd3b9d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,10 +24,34 @@ module.exports = createTransactionalMigration(
}
)).map(({id}) => id);
const compedMemberIds = (await knex('members')
.select('members.id')
.innerJoin(
'members_stripe_customers',
'members.id',
'members_stripe_customers.member_id'
).innerJoin(
'members_stripe_customers_subscriptions',
function () {
this.on(
'members_stripe_customers.customer_id',
'members_stripe_customers_subscriptions.customer_id'
).onIn(
'members_stripe_customers_subscriptions.status',
['active', 'trialing', 'past_due', 'unpaid']
);
}
).where(
'members_stripe_customers_subscriptions.plan_nickname',
'=',
'Complimentary'
)).map(({id}) => id);
// Umm? Well... The current version of SQLite3 bundled with Ghost supports
// a maximum of 999 variables, we use one variable for the SET value
// and so we're left with 998 for our WHERE IN clause values
const chunkSize = 998;
const paidMemberIdChunks = chunk(paidMemberIds, chunkSize);
for (const paidMemberIdsChunk of paidMemberIdChunks) {
@ -35,6 +59,14 @@ module.exports = createTransactionalMigration(
.update('status', 'paid')
.whereIn('id', paidMemberIdsChunk);
}
const compedMemberIdChunks = chunk(compedMemberIds, chunkSize);
for (const compedMemberIdsChunk of compedMemberIdChunks) {
await knex('members')
.update('status', 'comped')
.whereIn('id', compedMemberIdsChunk);
}
},
async function down(knex) {
logging.info('Updating all members status to "free"');