mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-25 20:03:12 +03:00
Added migration to populate cancellation events (#14438)
refs https://github.com/TryGhost/Team/issues/1302 We do not want to update MRR calculations until 5.0 - so will be adding a separate migration to populate the mrr_delta column for 5.0 We've only added events for non-expired subscriptions as this is simpler and won't impact the mrr events when mrr_delta is updated
This commit is contained in:
parent
184dc861cb
commit
cf6eef60e3
@ -0,0 +1,51 @@
|
||||
const ObjectID = require('bson-objectid').default;
|
||||
const logging = require('@tryghost/logging');
|
||||
|
||||
const {createTransactionalMigration} = require('../../utils');
|
||||
|
||||
module.exports = createTransactionalMigration(
|
||||
async function up(knex) {
|
||||
const cancelledSubscriptions = await knex
|
||||
.select(
|
||||
'members.id as member_id',
|
||||
'members_stripe_customers_subscriptions.id',
|
||||
'members_stripe_customers_subscriptions.stripe_price_id',
|
||||
'members_stripe_customers_subscriptions.plan_currency',
|
||||
'members_stripe_customers_subscriptions.updated_at'
|
||||
)
|
||||
.from('members_stripe_customers_subscriptions')
|
||||
.join('members_stripe_customers', 'members_stripe_customers.customer_id', '=', 'members_stripe_customers_subscriptions.customer_id')
|
||||
.join('members', 'members_stripe_customers.member_id', '=', 'members.id')
|
||||
.where('members_stripe_customers_subscriptions.cancel_at_period_end', true)
|
||||
.whereNot('members_stripe_customers_subscriptions.status', 'canceled');
|
||||
|
||||
if (cancelledSubscriptions.length === 0) {
|
||||
logging.info('No missing cancelled events - skipping migration');
|
||||
return;
|
||||
}
|
||||
|
||||
const eventsToInsert = cancelledSubscriptions.map((subscription) => {
|
||||
const event = {
|
||||
id: (new ObjectID()).toHexString(),
|
||||
type: 'canceled',
|
||||
member_id: subscription.member_id,
|
||||
subscription_id: subscription.id,
|
||||
from_plan: subscription.stripe_price_id,
|
||||
to_plan: subscription.stripe_price_id,
|
||||
currency: subscription.plan_currency,
|
||||
source: 'migration',
|
||||
mrr_delta: 0,
|
||||
created_at: subscription.updated_at
|
||||
};
|
||||
|
||||
return event;
|
||||
});
|
||||
|
||||
logging.info(`Found ${eventsToInsert.length} missing cancellation events`);
|
||||
await knex('members_paid_subscription_events').insert(eventsToInsert);
|
||||
},
|
||||
async function down(knex) {
|
||||
logging.info('Deleting all members_paid_subscription_events with a "type" of "cancelled"');
|
||||
await knex('members_paid_subscription_events').where({type: 'canceled', source: 'migration'}).del();
|
||||
}
|
||||
);
|
Loading…
Reference in New Issue
Block a user