Added migration to remove invalid subscriptions

closes https://github.com/TryGhost/Team/issues/660

All subscriptions in Ghost are expected to have a corresponding price details in `stripe_price` table, which is used to determine the Stripe price a subscription is on. In some edge cases, specially before we started deleted old Stripe data during Stripe disconnect, it's possible that a subscription exists in DB without having a corresponding Stripe price in the DB. These subscriptions are not active for the connected Stripe account, and are save to remove. Going forward, all existing subscriptions with connected account will be removed when disconnecting stripe so we shouldn't have invalid subscriptions in DB in future.

The goal of this migration is to clean all such subscriptions from the DB to avoid any issues around missing price with invalid subscriptions.
This commit is contained in:
Rishabh 2021-07-14 16:58:04 +05:30 committed by Rishabh Garg
parent aa19008651
commit aad662267c
2 changed files with 20 additions and 0 deletions

View File

@ -168,6 +168,8 @@ module.exports = function MembersAPI({
return stripeMigrations.populateDefaultProductYearlyPriceId();
}).then(() => {
return stripeMigrations.revertPortalPlansSetting();
}).then(() => {
return stripeMigrations.removeInvalidSubscriptions();
}),
stripeWebhookService.configure({
webhookSecret: process.env.WEBHOOK_SECRET,

View File

@ -480,4 +480,22 @@ module.exports = class StripeMigrations {
id: portalPlansSetting.id
});
}
async removeInvalidSubscriptions() {
const subscriptionModels = await this._StripeCustomerSubscription.findAll({
withRelated: ['stripePrice']
});
const invalidSubscriptions = subscriptionModels.filter((sub) => {
return !sub.toJSON().price;
});
if (invalidSubscriptions.length > 0) {
this._logging.warn(`Deleting ${invalidSubscriptions.length} invalid subscription(s)`);
for (let sub of invalidSubscriptions) {
this._logging.warn(`Deleting subscription - ${sub.id} - no price found`);
await sub.destroy();
}
} else {
this._logging.info(`No invalid subscriptions, skipping migration`);
}
}
};