diff --git a/core/server/data/migrations/versions/1.18/1-add-webhooks-table.js b/core/server/data/migrations/versions/1.18/1-add-webhooks-table.js index a26ed305ad..6c7fd8ef44 100644 --- a/core/server/data/migrations/versions/1.18/1-add-webhooks-table.js +++ b/core/server/data/migrations/versions/1.18/1-add-webhooks-table.js @@ -1,36 +1,2 @@ -const Promise = require('bluebird'); -const logging = require('../../../../../shared/logging'); -const commands = require('../../../schema').commands; -const table = 'webhooks'; -const message1 = 'Adding table: ' + table; -const message2 = 'Dropping table: ' + table; - -module.exports.up = function addWebhooksTable(options) { - let connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (exists) { - logging.warn(message1); - return Promise.resolve(); - } - - logging.info(message1); - return commands.createTable(table, connection); - }); -}; - -module.exports.down = function removeWebhooksTable(options) { - let connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (!exists) { - logging.warn(message2); - return Promise.resolve(); - } - - logging.info(message2); - return commands.deleteTable(table, connection); - }); -}; +const {addTable} = require('../../utils'); +module.exports = addTable('webhooks'); diff --git a/core/server/data/migrations/versions/1.22/1-multiple-authors-DDL.js b/core/server/data/migrations/versions/1.22/1-multiple-authors-DDL.js index ba16cdabe7..6808e3086b 100644 --- a/core/server/data/migrations/versions/1.22/1-multiple-authors-DDL.js +++ b/core/server/data/migrations/versions/1.22/1-multiple-authors-DDL.js @@ -1,36 +1,2 @@ -const Promise = require('bluebird'); -const logging = require('../../../../../shared/logging'); -const commands = require('../../../schema').commands; -const table = 'posts_authors'; -const message1 = 'Adding table: ' + table; -const message2 = 'Dropping table: ' + table; - -module.exports.up = function addMultipleAuthorsTable(options) { - let connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (exists) { - logging.warn(message1); - return Promise.resolve(); - } - - logging.info(message1); - return commands.createTable(table, connection); - }); -}; - -module.exports.down = function removeMultipleAuthorsTable(options) { - let connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (!exists) { - logging.warn(message2); - return Promise.resolve(); - } - - logging.info(message2); - return commands.deleteTable(table, connection); - }); -}; +const {addTable} = require('../../utils'); +module.exports = addTable('posts_authors'); diff --git a/core/server/data/migrations/versions/2.14/1-add-actions-table.js b/core/server/data/migrations/versions/2.14/1-add-actions-table.js index 71c3761280..76386d0007 100644 --- a/core/server/data/migrations/versions/2.14/1-add-actions-table.js +++ b/core/server/data/migrations/versions/2.14/1-add-actions-table.js @@ -1,35 +1,2 @@ -const logging = require('../../../../../shared/logging'); -const commands = require('../../../schema').commands; -const table = 'actions'; -const message1 = `Adding table: ${table}`; -const message2 = `Dropping table: ${table}`; - -module.exports.up = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (exists) { - logging.warn(message1); - return; - } - - logging.info(message1); - return commands.createTable(table, connection); - }); -}; - -module.exports.down = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (!exists) { - logging.warn(message2); - return; - } - - logging.info(message2); - return commands.deleteTable(table, connection); - }); -}; +const {addTable} = require('../../utils'); +module.exports = addTable('actions'); diff --git a/core/server/data/migrations/versions/2.2/1-add-sessions-table.js b/core/server/data/migrations/versions/2.2/1-add-sessions-table.js index 216f847b69..a70f58a617 100644 --- a/core/server/data/migrations/versions/2.2/1-add-sessions-table.js +++ b/core/server/data/migrations/versions/2.2/1-add-sessions-table.js @@ -1,35 +1,2 @@ -const logging = require('../../../../../shared/logging'); -const commands = require('../../../schema').commands; -const table = 'sessions'; -const message1 = 'Adding table: ' + table; -const message2 = 'Dropping table: ' + table; - -module.exports.up = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (exists) { - logging.warn(message1); - return; - } - - logging.info(message1); - return commands.createTable(table, connection); - }); -}; - -module.exports.down = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (!exists) { - logging.warn(message2); - return; - } - - logging.info(message2); - return commands.deleteTable(table, connection); - }); -}; +const {addTable} = require('../../utils'); +module.exports = addTable('sessions'); diff --git a/core/server/data/migrations/versions/2.2/2-add-integrations-and-api-key-tables.js b/core/server/data/migrations/versions/2.2/2-add-integrations-and-api-key-tables.js index 2753ff3749..bdc8fc4a73 100644 --- a/core/server/data/migrations/versions/2.2/2-add-integrations-and-api-key-tables.js +++ b/core/server/data/migrations/versions/2.2/2-add-integrations-and-api-key-tables.js @@ -1,34 +1,6 @@ -const Promise = require('bluebird'); -const commands = require('../../../schema').commands; -const logging = require('../../../../../shared/logging'); +const {addTable, combineNonTransactionalMigrations} = require('../../utils'); -const tables = ['integrations', 'api_keys']; -const _private = {}; - -_private.addOrRemoveTable = (type, table, options) => { - const isAdding = type === 'Adding'; - const operation = isAdding ? commands.createTable : commands.deleteTable; - const message = `${type} ${table} table`; - - return options.connection.schema.hasTable(table) - .then((exists) => { - if ((isAdding && exists || !isAdding && !exists)) { - logging.warn(message); - return Promise.resolve(); - } - - logging.info(message); - return operation(table, options.connection); - }); -}; - -_private.handle = (migrationOptions) => { - return (options) => { - return Promise.each(tables, (table) => { - return _private.addOrRemoveTable(migrationOptions.type, table, options); - }); - }; -}; - -module.exports.up = _private.handle({type: 'Adding'}); -module.exports.down = _private.handle({type: 'Dropping'}); +module.exports = combineNonTransactionalMigrations( + addTable('integrations'), + addTable('api_keys') +); diff --git a/core/server/data/migrations/versions/2.2/5-add-mobiledoc-revisions-table.js b/core/server/data/migrations/versions/2.2/5-add-mobiledoc-revisions-table.js index b075b54632..b22d7ca638 100644 --- a/core/server/data/migrations/versions/2.2/5-add-mobiledoc-revisions-table.js +++ b/core/server/data/migrations/versions/2.2/5-add-mobiledoc-revisions-table.js @@ -1,35 +1,2 @@ -const logging = require('../../../../../shared/logging'); -const commands = require('../../../schema').commands; -const table = 'mobiledoc_revisions'; -const message1 = 'Adding table: ' + table; -const message2 = 'Dropping table: ' + table; - -module.exports.up = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (exists) { - logging.warn(message1); - return; - } - - logging.info(message1); - return commands.createTable(table, connection); - }); -}; - -module.exports.down = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (!exists) { - logging.warn(message2); - return; - } - - logging.info(message2); - return commands.deleteTable(table, connection); - }); -}; +const {addTable} = require('../../utils'); +module.exports = addTable('mobiledoc_revisions'); diff --git a/core/server/data/migrations/versions/2.34/01-add-stripe-customers-subscriptions-table.js b/core/server/data/migrations/versions/2.34/01-add-stripe-customers-subscriptions-table.js index d184a0d2f8..38ef6d82ab 100644 --- a/core/server/data/migrations/versions/2.34/01-add-stripe-customers-subscriptions-table.js +++ b/core/server/data/migrations/versions/2.34/01-add-stripe-customers-subscriptions-table.js @@ -1,34 +1,2 @@ -const logging = require('../../../../../shared/logging'); -const commands = require('../../../schema/commands'); - -module.exports = { - config: { - transaction: true - }, - - async up(options){ - const conn = options.transacting || options.connection; - const hasTable = await conn.schema.hasTable('members_stripe_customers_subscriptions'); - - if (hasTable) { - logging.warn('Adding table: members_stripe_customers_subscriptions'); - return; - } - - logging.info('Adding table: members_stripe_customers_subscriptions'); - return commands.createTable('members_stripe_customers_subscriptions', conn); - }, - - async down(options){ - const conn = options.transacting || options.connection; - const hasTable = await conn.schema.hasTable('members_stripe_customers_subscriptions'); - - if (!hasTable) { - logging.warn('Dropping table: members_stripe_customers_subscriptions'); - return; - } - - logging.info('Dropping table: members_stripe_customers_subscriptions'); - return commands.deleteTable('members_stripe_customers_subscriptions', conn); - } -}; +const {addTable} = require('../../utils'); +module.exports = addTable('members_stripe_customers_subscriptions'); diff --git a/core/server/data/migrations/versions/2.8/1-add-members-table.js b/core/server/data/migrations/versions/2.8/1-add-members-table.js index 5d9b046147..0effea658e 100644 --- a/core/server/data/migrations/versions/2.8/1-add-members-table.js +++ b/core/server/data/migrations/versions/2.8/1-add-members-table.js @@ -1,35 +1,2 @@ -const logging = require('../../../../../shared/logging'); -const commands = require('../../../schema').commands; -const table = 'members'; -const message1 = 'Adding table: ' + table; -const message2 = 'Dropping table: ' + table; - -module.exports.up = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (exists) { - logging.warn(message1); - return; - } - - logging.info(message1); - return commands.createTable(table, connection); - }); -}; - -module.exports.down = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (!exists) { - logging.warn(message2); - return; - } - - logging.info(message2); - return commands.deleteTable(table, connection); - }); -}; +const {addTable} = require('../../utils'); +module.exports = addTable('members'); diff --git a/core/server/data/migrations/versions/3.0/04-add-posts-meta-table.js b/core/server/data/migrations/versions/3.0/04-add-posts-meta-table.js index bbe729aff8..68cc738bb0 100644 --- a/core/server/data/migrations/versions/3.0/04-add-posts-meta-table.js +++ b/core/server/data/migrations/versions/3.0/04-add-posts-meta-table.js @@ -1,35 +1,2 @@ -const logging = require('../../../../../shared/logging'); -const commands = require('../../../schema').commands; -const table = 'posts_meta'; -const message1 = `Adding table: ${table}`; -const message2 = `Dropping table: ${table}`; - -module.exports.up = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (exists) { - logging.warn(message1); - return; - } - - logging.info(message1); - return commands.createTable(table, connection); - }); -}; - -module.exports.down = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (!exists) { - logging.warn(message2); - return; - } - - logging.info(message2); - return commands.deleteTable(table, connection); - }); -}; +const {addTable} = require('../../utils'); +module.exports = addTable('posts_meta'); diff --git a/core/server/data/migrations/versions/3.1/05-add-emails-table.js b/core/server/data/migrations/versions/3.1/05-add-emails-table.js index eab763e452..34ef6cca71 100644 --- a/core/server/data/migrations/versions/3.1/05-add-emails-table.js +++ b/core/server/data/migrations/versions/3.1/05-add-emails-table.js @@ -1,35 +1,2 @@ -const logging = require('../../../../../shared/logging'); -const commands = require('../../../schema').commands; -const table = 'emails'; -const message1 = 'Adding table: ' + table; -const message2 = 'Dropping table: ' + table; - -module.exports.up = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (exists) { - logging.warn(message1); - return; - } - - logging.info(message1); - return commands.createTable(table, connection); - }); -}; - -module.exports.down = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (!exists) { - logging.warn(message2); - return; - } - - logging.info(message2); - return commands.deleteTable(table, connection); - }); -}; +const {addTable} = require('../../utils'); +module.exports = addTable('emails'); diff --git a/core/server/data/migrations/versions/3.33/01-add-email-recipients-tables.js b/core/server/data/migrations/versions/3.33/01-add-email-recipients-tables.js index b461818d5b..35c45e3e61 100644 --- a/core/server/data/migrations/versions/3.33/01-add-email-recipients-tables.js +++ b/core/server/data/migrations/versions/3.33/01-add-email-recipients-tables.js @@ -1,35 +1,8 @@ -const Promise = require('bluebird'); -const logging = require('../../../../../shared/logging'); -const commands = require('../../../schema').commands; +const {addTable, combineNonTransactionalMigrations} = require('../../utils'); -module.exports = { - async up({connection}) { - // table creation order is important because of foreign key constraints, - // email_recipients references email_batches so email_batches has to exist when creating - return Promise.each(['email_batches', 'email_recipients'], async (table) => { - const tableExists = await connection.schema.hasTable(table); - - if (tableExists) { - return logging.warn(`Skipping add table "${table}" - already exists`); - } - - logging.info(`Adding table: ${table}`); - return commands.createTable(table, connection); - }); - }, - - async down({connection}) { - // table deletion order is important because of foreign key constraints, - // email_recipients references email_batches so it has to be deleted first to not break constraints - return Promise.each(['email_recipients', 'email_batches'], async (table) => { - const tableExists = await connection.schema.hasTable(table); - - if (!tableExists) { - return logging.warn(`Skipping drop table "${table}" - does not exist`); - } - - logging.info(`Dropping table: ${table}`); - return commands.deleteTable(table, connection); - }); - } -}; +// table order is important because of foreign key constraints, +// email_recipients references email_batches so email_batches has to exist when creating +module.exports = combineNonTransactionalMigrations( + addTable('email_batches'), + addTable('email_recipients') +); diff --git a/core/server/data/migrations/versions/3.6/1-add-labels-table.js b/core/server/data/migrations/versions/3.6/1-add-labels-table.js index 46d25d4c95..73d0c21378 100644 --- a/core/server/data/migrations/versions/3.6/1-add-labels-table.js +++ b/core/server/data/migrations/versions/3.6/1-add-labels-table.js @@ -1,35 +1,2 @@ -const logging = require('../../../../../shared/logging'); -const commands = require('../../../schema').commands; -const table = 'labels'; -const message1 = 'Adding table: ' + table; -const message2 = 'Dropping table: ' + table; - -module.exports.up = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (exists) { - logging.warn(message1); - return; - } - - logging.info(message1); - return commands.createTable(table, connection); - }); -}; - -module.exports.down = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (!exists) { - logging.warn(message2); - return; - } - - logging.info(message2); - return commands.deleteTable(table, connection); - }); -}; +const {addTable} = require('../../utils'); +module.exports = addTable('labels'); diff --git a/core/server/data/migrations/versions/3.6/2-add-members-labels-table.js b/core/server/data/migrations/versions/3.6/2-add-members-labels-table.js index 5e02a64057..7bd6526c76 100644 --- a/core/server/data/migrations/versions/3.6/2-add-members-labels-table.js +++ b/core/server/data/migrations/versions/3.6/2-add-members-labels-table.js @@ -1,35 +1,2 @@ -const logging = require('../../../../../shared/logging'); -const commands = require('../../../schema').commands; -const table = 'members_labels'; -const message1 = 'Adding table: ' + table; -const message2 = 'Dropping table: ' + table; - -module.exports.up = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (exists) { - logging.warn(message1); - return; - } - - logging.info(message1); - return commands.createTable(table, connection); - }); -}; - -module.exports.down = (options) => { - const connection = options.connection; - - return connection.schema.hasTable(table) - .then(function (exists) { - if (!exists) { - logging.warn(message2); - return; - } - - logging.info(message2); - return commands.deleteTable(table, connection); - }); -}; +const {addTable} = require('../../utils'); +module.exports = addTable('members_labels');