From 9cb069eb8e1a8678fdc4faf1a175f3eef37eb6f7 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 14 Jul 2020 10:16:50 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=20Updated=20"unsplash"=20setting?= =?UTF-8?q?=20handling=20to=20match=20updated=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs https://github.com/TryGhost/Ghost/issues/10318 - JSON object format used in previous "unsplash" setting was considered an anti-pattern. Flat structure was extracted out of the "unsplash.isActive" JSON. - The naming convention uses `amp` as a precedent (https://github.com/TryGhost/Team/issues/331#issuecomment-658815017) --- .../4.0/10-refactor-unsplash-setting.js | 29 ++++++++++++++++ .../4.0/20-refactor-unsplash-setting.js | 33 +++++++++++++++++++ core/server/data/schema/default-settings.json | 8 +++-- test/unit/data/schema/integrity_spec.js | 2 +- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 core/server/data/migrations/versions/4.0/10-refactor-unsplash-setting.js create mode 100644 core/server/data/migrations/versions/4.0/20-refactor-unsplash-setting.js diff --git a/core/server/data/migrations/versions/4.0/10-refactor-unsplash-setting.js b/core/server/data/migrations/versions/4.0/10-refactor-unsplash-setting.js new file mode 100644 index 0000000000..434fcc6d80 --- /dev/null +++ b/core/server/data/migrations/versions/4.0/10-refactor-unsplash-setting.js @@ -0,0 +1,29 @@ +const {createIrreversibleMigration} = require('../../utils'); + +module.exports = createIrreversibleMigration(async (knex) => { + const unsplashSetting = await knex('settings') + .select('value') + .where({ + key: 'unsplash' + }) + .first(); + + let isActive; + try { + const value = JSON.parse(unsplashSetting.value); + isActive = typeof value.isActive === 'boolean' ? value.isActive : true; + } catch (err) { + isActive = true; + } + + await knex('settings') + .update({ + group: 'unsplash', + type: 'boolean', + flags: null, + value: isActive.toString() + }) + .where({ + key: 'unsplash' + }); +}); diff --git a/core/server/data/migrations/versions/4.0/20-refactor-unsplash-setting.js b/core/server/data/migrations/versions/4.0/20-refactor-unsplash-setting.js new file mode 100644 index 0000000000..91e180032f --- /dev/null +++ b/core/server/data/migrations/versions/4.0/20-refactor-unsplash-setting.js @@ -0,0 +1,33 @@ +const logging = require('../../../../../shared/logging'); +const {createIrreversibleMigration} = require('../../utils'); + +module.exports = createIrreversibleMigration(async (knex) => { + const unsplashSetting = await knex('settings') + .select('value') + .where({ + key: 'unsplash' + }) + .first(); + + let isActive; + try { + const value = JSON.parse(unsplashSetting.value); + isActive = typeof value.isActive === 'boolean' ? value.isActive : true; + } catch (err) { + logging.warn(`Wasn't able to parse 'unsplash' setting: ${unsplashSetting.value}, falling back to: 'true'`); + isActive = true; + } + + logging.info(`Updating 'unsplash' setting to: ${isActive}`); + + await knex('settings') + .update({ + group: 'unsplash', + type: 'boolean', + flags: null, + value: isActive.toString() + }) + .where({ + key: 'unsplash' + }); +}); diff --git a/core/server/data/schema/default-settings.json b/core/server/data/schema/default-settings.json index 15f65730b9..cbebf602dc 100644 --- a/core/server/data/schema/default-settings.json +++ b/core/server/data/schema/default-settings.json @@ -406,8 +406,12 @@ }, "unsplash": { "unsplash": { - "defaultValue": "{\"isActive\": true}", - "type": "object" + "defaultValue": "true", + "validations": { + "isEmpty": false, + "isIn": [["true", "false"]] + }, + "type": "boolean" } }, "views": { diff --git a/test/unit/data/schema/integrity_spec.js b/test/unit/data/schema/integrity_spec.js index 77e2a311fe..2bcce8436e 100644 --- a/test/unit/data/schema/integrity_spec.js +++ b/test/unit/data/schema/integrity_spec.js @@ -34,7 +34,7 @@ describe('DB version integrity', function () { // Only these variables should need updating const currentSchemaHash = '5861ed57418a0195ea01e431b8b55335'; const currentFixturesHash = '370d0da0ab7c45050b2ff30bce8896ba'; - const currentSettingsHash = '6db8d92f1b76b43946bf75fbac78599d'; + const currentSettingsHash = 'e1f85186a7c7ed76064b6026f68c6321'; const currentRoutesHash = '3d180d52c663d173a6be791ef411ed01'; // If this test is failing, then it is likely a change has been made that requires a DB version bump,