0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

Added 'on delete cascade' to several foreign keys in sqlite

issue https://github.com/TryGhost/Team/issues/476
blocked by https://github.com/TryGhost/Ghost/pull/12702
This commit is contained in:
Thibaut Patel 2021-03-01 16:54:13 +01:00 committed by Thibaut Patel
parent b7a092a24a
commit 0843ab6a37

View file

@ -0,0 +1,75 @@
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 adding "on delete cascade" - database is not SQLite3');
}
logging.info('Adding on delete cascade for members_labels, members_stripe_customers and members_stripe_customers_subscriptions');
await dropForeign({
fromTable: 'members_labels',
fromColumn: 'member_id',
toTable: 'members',
toColumn: 'id',
transaction: knex
});
await addForeign({
fromTable: 'members_labels',
fromColumn: 'member_id',
toTable: 'members',
toColumn: 'id',
cascade: true,
transaction: knex
});
await dropForeign({
fromTable: 'members_labels',
fromColumn: 'label_id',
toTable: 'labels',
toColumn: 'id',
transaction: knex
});
await addForeign({
fromTable: 'members_labels',
fromColumn: 'label_id',
toTable: 'labels',
toColumn: 'id',
cascade: true,
transaction: knex
});
await dropForeign({
fromTable: 'members_stripe_customers',
fromColumn: 'member_id',
toTable: 'members',
toColumn: 'id',
transaction: knex
});
await addForeign({
fromTable: 'members_stripe_customers',
fromColumn: 'member_id',
toTable: 'members',
toColumn: 'id',
cascade: true,
transaction: knex
});
await dropForeign({
fromTable: 'members_stripe_customers_subscriptions',
fromColumn: 'customer_id',
toTable: 'members_stripe_customers',
toColumn: 'id',
transaction: knex
});
await addForeign({
fromTable: 'members_stripe_customers_subscriptions',
fromColumn: 'customer_id',
toTable: 'members_stripe_customers',
toColumn: 'id',
cascade: true,
transaction: knex
});
});