From 3b8cdb16576472b54742b2b178575730972a0fdb Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 30 Jun 2020 09:54:14 +0200 Subject: [PATCH] Fixed migrations for missing settings refs #10318 If migrating from a previous version that does not include the setting being migrated from we can safely not update the new setting, and just rely on its default value being present. When rolling back we can use defaults if the new setting does not exist. --- .../versions/3.22/02-settings-key-renames.js | 10 +++++++ ...5-migrate-members-subscription-settings.js | 27 ++++++++++++++----- .../06-migrate-stripe-connect-settings.js | 15 +++++++---- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/core/server/data/migrations/versions/3.22/02-settings-key-renames.js b/core/server/data/migrations/versions/3.22/02-settings-key-renames.js index 74173894cc..b16910366e 100644 --- a/core/server/data/migrations/versions/3.22/02-settings-key-renames.js +++ b/core/server/data/migrations/versions/3.22/02-settings-key-renames.js @@ -37,6 +37,11 @@ module.exports = { .select('value') .first(); + if (!oldSetting) { + logging.warn(`Could not find setting ${renameMapping.from}, not updating ${renameMapping.to} value`); + return; + } + const updatedValue = renameMapping.getToValue ? renameMapping.getToValue(oldSetting.value) : oldSetting.value; logging.info(`Updating ${renameMapping.to} with value from ${renameMapping.from}`); @@ -58,6 +63,11 @@ module.exports = { .select('value') .first(); + if (!newSetting) { + logging.warn(`Could not find setting ${renameMapping.to}, not updating ${renameMapping.from} value`); + return; + } + const updatedValue = renameMapping.getFromValue ? renameMapping.getFromValue(newSetting.value) : newSetting.value; logging.info(`Updating ${renameMapping.from} with value from ${renameMapping.to}`); diff --git a/core/server/data/migrations/versions/3.22/05-migrate-members-subscription-settings.js b/core/server/data/migrations/versions/3.22/05-migrate-members-subscription-settings.js index 1d0fb829ff..e03f0f68bb 100644 --- a/core/server/data/migrations/versions/3.22/05-migrate-members-subscription-settings.js +++ b/core/server/data/migrations/versions/3.22/05-migrate-members-subscription-settings.js @@ -13,6 +13,11 @@ module.exports = { .where('key', 'members_subscription_settings') .first(); + if (!membersSubscriptionSettingsJSON) { + logging.warn(`Could not find members_subscription_settings - using default values`); + return; + } + const membersSubscriptionSettings = JSON.parse(membersSubscriptionSettingsJSON.value); const membersFromAddress = membersSubscriptionSettings.fromAddress; @@ -84,20 +89,30 @@ module.exports = { const stripePlans = await getSetting('stripe_plans'); - const allowSelfSignupBoolean = allowSelfSignup.value === 'true'; + const allowSelfSignupBoolean = allowSelfSignup && allowSelfSignup.value === 'true'; const membersSubscriptionSettings = { - fromAddress: membersFromAddress.value, + fromAddress: membersFromAddress ? membersFromAddress.value : 'noreply', allowSelfSignup: allowSelfSignupBoolean, paymentProcessors: [{ adapter: 'stripe', config: { - secret_token: stripeDirectSecretKey.value, - public_token: stripeDirectPublishableKey.value, + secret_token: stripeDirectSecretKey ? stripeDirectSecretKey.value : null, + public_token: stripeDirectPublishableKey ? stripeDirectPublishableKey.value : null, product: { - name: stripeProductName.value + name: stripeProductName ? stripeProductName.value : 'Ghost Subscription' }, - plans: JSON.parse(stripePlans.value) + plans: stripePlans ? JSON.parse(stripePlans.value) : [{ + name: 'Monthly', + currency: 'usd', + interval: 'month', + amount: 500 + }, { + name: 'Yearly', + currency: 'usd', + interval: 'year', + amount: 5000 + }] } }] }; diff --git a/core/server/data/migrations/versions/3.22/06-migrate-stripe-connect-settings.js b/core/server/data/migrations/versions/3.22/06-migrate-stripe-connect-settings.js index bdfcc7d4ec..5d3e0fb202 100644 --- a/core/server/data/migrations/versions/3.22/06-migrate-stripe-connect-settings.js +++ b/core/server/data/migrations/versions/3.22/06-migrate-stripe-connect-settings.js @@ -13,6 +13,11 @@ module.exports = { .where('key', 'stripe_connect_integration') .first(); + if (!stripeConnectIntegrationJSON) { + logging.warn(`Could not find stripe_connect_integration - using default values`); + return; + } + const stripeConnectIntegration = JSON.parse(stripeConnectIntegrationJSON.value); const operations = [{ @@ -64,11 +69,11 @@ module.exports = { const secretKey = await getSetting('stripe_connect_secret_key'); const stripeConnectIntegration = { - account_id: accountId.value, - display_name: displayName.value, - livemode: livemode.value, - public_key: publishableKey.value, - secret_key: secretKey.value + account_id: accountId ? accountId.value : null, + display_name: displayName ? displayName.value : null, + livemode: livemode ? livemode.value : null, + public_key: publishableKey ? publishableKey.value : null, + secret_key: secretKey ? secretKey.value : null }; const now = knex.raw('CURRENT_TIMESTAMP');