From 744e5e57a06b6039878b90ab176b0c1471a1467b Mon Sep 17 00:00:00 2001 From: Rishabh Garg Date: Fri, 4 Feb 2022 19:57:38 +0530 Subject: [PATCH] Fixed missing portal product data (#14115) closes https://github.com/TryGhost/Team/issues/1311 For some sites, the `portal_products` array was created without any value and due to a possible bug in older version of Ghost, it also didn't get filled on Stripe connect with default product. This causes a side-effect of sites not showing the prices in Portal when tiers beta is enabled or is out as GA. This change populates the missing product data in `portal_product` for sites that have a single tier (haven't enabled tiers beta), as they right now don't have an option to hide the tier. --- ...04-04-34-populate-empty-portal-products.js | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 core/server/data/migrations/versions/4.35/2022-02-04-04-34-populate-empty-portal-products.js diff --git a/core/server/data/migrations/versions/4.35/2022-02-04-04-34-populate-empty-portal-products.js b/core/server/data/migrations/versions/4.35/2022-02-04-04-34-populate-empty-portal-products.js new file mode 100644 index 0000000000..53ad3f6ab0 --- /dev/null +++ b/core/server/data/migrations/versions/4.35/2022-02-04-04-34-populate-empty-portal-products.js @@ -0,0 +1,60 @@ +const {createTransactionalMigration} = require('../../utils'); +const logging = require('@tryghost/logging'); + +module.exports = createTransactionalMigration( + async function up(knex) { + const products = await knex + .select('id') + .where({ + type: 'paid' + }) + .from('products'); + + if (products.length === 0) { + logging.warn(`Skipping updating portal_products, no product exists`); + return; + } + + if (products.length > 1) { + logging.warn(`Skipping updating portal_products, tiers beta is enabled`); + return; + } + + const portalProductsSetting = await knex('settings') + .where('key', 'portal_products') + .select('value') + .first(); + + if (!portalProductsSetting) { + logging.warn(`Missing portal_products setting`); + return; + } + try { + const currPortalProductsValue = JSON.parse(portalProductsSetting.value); + + if (currPortalProductsValue.length > 0) { + logging.warn(`Ignoring - portal_products setting is not empty, - ${currPortalProductsValue}`); + return; + } + + const defaultProduct = products[0]; + const portalProductsValue = [defaultProduct.id]; + + logging.info(`Setting portal_products setting to have product - ${defaultProduct.id}`); + + const now = knex.raw('CURRENT_TIMESTAMP'); + + await knex('settings') + .where('key', 'portal_products') + .update({ + value: JSON.stringify(portalProductsValue), + updated_at: now + }); + } catch (e) { + logging.warn(`Ignoring, unable to parse portal_products setting value - ${portalProductsSetting.value}`); + logging.warn(e); + } + }, + // no-op - we don't want to return to invalid state + async function down() {} +);