From cff033bb4728d861e00fbe95e6884c4b9bc6286b Mon Sep 17 00:00:00 2001 From: Fabien 'egg' O'Carroll Date: Mon, 7 Mar 2022 11:38:39 +0000 Subject: [PATCH] Migrated visibility column from portal settings (#14253) https://github.com/TryGhost/Team/issues/1387 This is split into two migrations, one for the portal_products setting and one for the portal_plans setting, as dealing with both of them in a single migration led to too many branches. --- ...-update-free-products-visibility-column.js | 66 +++++++++++++++++++ ...10-57-update-products-visibility-column.js | 36 ++++++++++ 2 files changed, 102 insertions(+) create mode 100644 core/server/data/migrations/versions/4.39/2022-03-07-10-57-update-free-products-visibility-column.js create mode 100644 core/server/data/migrations/versions/4.39/2022-03-07-10-57-update-products-visibility-column.js diff --git a/core/server/data/migrations/versions/4.39/2022-03-07-10-57-update-free-products-visibility-column.js b/core/server/data/migrations/versions/4.39/2022-03-07-10-57-update-free-products-visibility-column.js new file mode 100644 index 0000000000..2d3af49060 --- /dev/null +++ b/core/server/data/migrations/versions/4.39/2022-03-07-10-57-update-free-products-visibility-column.js @@ -0,0 +1,66 @@ +const logging = require('@tryghost/logging'); +const {createTransactionalMigration} = require('../../utils'); + +module.exports = createTransactionalMigration( + async function up(knex) { + const portalPlanSetting = await knex('settings').select('value').where('key', 'portal_plans').first(); + + if (!portalPlanSetting) { + logging.warn('Could not find portal_plans setting - skipping migration'); + return; + } + + try { + const settingData = JSON.parse(portalPlanSetting.value); + + if (!settingData.includes('free')) { + logging.warn(`portal_plans does not include "free" - skipping migration`); + return; + } + + logging.info(`Updating free products to visible`); + await knex('products').update('visibility', 'public').where('type', 'free'); + } catch (err) { + logging.error(err); + logging.warn('portal_plans setting is invalid - skipping migration'); + return; + } + }, + async function down(knex) { + const freeTier = await knex('products').select('id').where('type', 'free').first(); + const portalPlanSetting = await knex('settings').select('value').where('key', 'portal_plans').first(); + + if (!freeTier) { + logging.info('Free tier is not visible, not updating portal_plans'); + return; + } + + if (!portalPlanSetting) { + logging.warn('Could not find portal_plans setting - skipping migration'); + return; + } + + try { + const existingSettingData = JSON.parse(portalPlanSetting.value); + let settingData; + + if (freeTier.visibility === 'public') { + if (existingSettingData.includes('free')) { + logging.info('portal_plans setting already contains "free" - skipping update'); + return; + } else { + settingData = JSON.stringify(existingSettingData.concat('free')); + } + } else { + settingData = JSON.stringify(existingSettingData.filter(value => value !== 'free')); + } + + logging.info(`Updating portal_plans to ${settingData}`); + await knex('settings').update('value', settingData).where('key', 'portal_plans'); + } catch (err) { + logging.error(err); + logging.warn('portal_plans setting is invalid - skipping migration'); + return; + } + } +); diff --git a/core/server/data/migrations/versions/4.39/2022-03-07-10-57-update-products-visibility-column.js b/core/server/data/migrations/versions/4.39/2022-03-07-10-57-update-products-visibility-column.js new file mode 100644 index 0000000000..25a03f06b2 --- /dev/null +++ b/core/server/data/migrations/versions/4.39/2022-03-07-10-57-update-products-visibility-column.js @@ -0,0 +1,36 @@ +const logging = require('@tryghost/logging'); +const {createTransactionalMigration} = require('../../utils'); + +module.exports = createTransactionalMigration( + async function up(knex) { + const portalProductSetting = await knex('settings').select('value').where('key', 'portal_products').first(); + + if (!portalProductSetting) { + logging.warn('Could not find portal_products setting - skipping migration'); + return; + } + + try { + const settingData = JSON.parse(portalProductSetting.value); + + if (settingData.length === 0) { + logging.warn(`portal_product is empty, skipping migrations`); + return; + } + + logging.info(`Updating ${settingData.length} products to visible, ${settingData}`); + await knex('products').update('visibility', 'public').whereIn('id', settingData); + } catch (err) { + logging.warn('portal_products setting is invalid - skipping migration'); + return; + } + }, + async function down(knex) { + const visibleTiers = await knex('products').select('id').where('visibility', 'public'); + + const settingData = JSON.stringify(visibleTiers.map(obj => obj.id)); + + logging.info(`Updating portal_products to ${settingData}`); + await knex('settings').update('value', settingData).where('key', 'portal_products'); + } +);