From 2eb5f19090b047ff664b618ef5b1e06021cb5edf Mon Sep 17 00:00:00 2001 From: Thibaut Patel Date: Tue, 9 Mar 2021 13:51:51 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20a=20foreign=20key=20to?= =?UTF-8?q?=20point=20to=20the=20right=20column?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pr https://github.com/TryGhost/Ghost/pull/12713 --- .../versions/4.0/26-add-cascade-on-delete.js | 4 +-- ...-members-stripe-customers-subscriptions.js | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 core/server/data/migrations/versions/4.0/29-fix-foreign-key-for-members-stripe-customers-subscriptions.js diff --git a/core/server/data/migrations/versions/4.0/26-add-cascade-on-delete.js b/core/server/data/migrations/versions/4.0/26-add-cascade-on-delete.js index 35a0c16ea9..400f20c52a 100644 --- a/core/server/data/migrations/versions/4.0/26-add-cascade-on-delete.js +++ b/core/server/data/migrations/versions/4.0/26-add-cascade-on-delete.js @@ -61,14 +61,14 @@ module.exports = createIrreversibleMigration(async (knex) => { fromTable: 'members_stripe_customers_subscriptions', fromColumn: 'customer_id', toTable: 'members_stripe_customers', - toColumn: 'id', + toColumn: 'customer_id', transaction: knex }); await addForeign({ fromTable: 'members_stripe_customers_subscriptions', fromColumn: 'customer_id', toTable: 'members_stripe_customers', - toColumn: 'id', + toColumn: 'customer_id', cascadeDelete: true, transaction: knex }); diff --git a/core/server/data/migrations/versions/4.0/29-fix-foreign-key-for-members-stripe-customers-subscriptions.js b/core/server/data/migrations/versions/4.0/29-fix-foreign-key-for-members-stripe-customers-subscriptions.js new file mode 100644 index 0000000000..2a495faf2b --- /dev/null +++ b/core/server/data/migrations/versions/4.0/29-fix-foreign-key-for-members-stripe-customers-subscriptions.js @@ -0,0 +1,34 @@ +const logging = require('../../../../../shared/logging'); +const {createIrreversibleMigration} = require('../../utils'); +const {addForeign, dropForeign} = require('../../../schema/commands'); + +module.exports = createIrreversibleMigration(async (knex) => { + if (knex.client.config.client !== 'sqlite3') { + return logging.warn('Skipping fixing foreign key for members_stripe_customers_subscriptions - database is not SQLite3'); + } + + logging.info('Fixing foreign keys for members_stripe_customers_subscriptions'); + + await dropForeign({ + fromTable: 'members_stripe_customers_subscriptions', + fromColumn: 'customer_id', + toTable: 'members_stripe_customers', + toColumn: 'id', + transaction: knex + }); + await dropForeign({ + fromTable: 'members_stripe_customers_subscriptions', + fromColumn: 'customer_id', + toTable: 'members_stripe_customers', + toColumn: 'customer_id', + transaction: knex + }); + await addForeign({ + fromTable: 'members_stripe_customers_subscriptions', + fromColumn: 'customer_id', + toTable: 'members_stripe_customers', + toColumn: 'customer_id', + cascadeDelete: true, + transaction: knex + }); +});