Skip to content

Commit

Permalink
Added down migration
Browse files Browse the repository at this point in the history
  • Loading branch information
naz committed Mar 11, 2020
1 parent 8551be6 commit 9efdd55
Showing 1 changed file with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const _ = require('lodash');
const Promise = require('bluebird');
const common = require('../../../../lib/common');
const debug = require('ghost-ignition').debug('migrations');

Expand Down Expand Up @@ -54,9 +53,57 @@ module.exports.up = (options) => {
});
};

// `up` is only run to fix a problem that is introduced with 3.10.0,
// it doesn't make sense to "reintroduced" broken state with down migration
module.exports.down = () => Promise.resolve();
module.exports.down = (options) => {
let localOptions = _.merge({
context: {internal: true}
}, options);
const settingsKey = 'members_subscription_settings';

return localOptions
.transacting('settings')
.then((response) => {
if (!response) {
common.logging.warn('Cannot find settings.');
return;
}

let subscriptionSettingsEntry = response.find((entry) => {
return entry.key === settingsKey;
});

if (!subscriptionSettingsEntry) {
common.logging.warn('Cannot find members subscription settings.');
return;
}

let subscriptionSettings = JSON.parse(subscriptionSettingsEntry.value);

debug('before cleanup');
debug(JSON.stringify(subscriptionSettings, null, 2));

let isPaid = false;

const stripePaymentProcessor = subscriptionSettings.paymentProcessors.find(
paymentProcessor => paymentProcessor.adapter === 'stripe'
);

if (stripePaymentProcessor && stripePaymentProcessor.config.public_token && stripePaymentProcessor.config.secret_token) {
isPaid = true;
}

subscriptionSettings.isPaid = isPaid;

debug('after cleanup');
debug(JSON.stringify(subscriptionSettings, null, 2));

return localOptions
.transacting('settings')
.where('key', settingsKey)
.update({
value: JSON.stringify(subscriptionSettings)
});
});
};

module.exports.config = {
transaction: true
Expand Down

0 comments on commit 9efdd55

Please sign in to comment.