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.
This commit is contained in:
Fabien 'egg' O'Carroll 2022-03-07 11:38:39 +00:00 committed by GitHub
parent 390e1cac50
commit cff033bb47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 102 additions and 0 deletions

View File

@ -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;
}
}
);

View File

@ -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');
}
);