diff --git a/core/server/data/migrations/utils.js b/core/server/data/migrations/utils.js index 26f94b67f4..6a81edae48 100644 --- a/core/server/data/migrations/utils.js +++ b/core/server/data/migrations/utils.js @@ -1,6 +1,7 @@ const ObjectId = require('bson-objectid').default; const logging = require('../../../shared/logging'); const commands = require('../schema').commands; +const Promise = require('bluebird'); const MIGRATION_USER = 1; @@ -32,6 +33,28 @@ function addTable(name) { ); } +/** + * Creates migration which will drop a table + * + * @param {[string]} names - names of the tables to drop + */ +function dropTables(names) { + return createTransactionalMigration( + async function up(connection) { + for (const name of names) { + const exists = await connection.schema.hasTable(name); + + if (!exists) { + logging.warn(`Failed dropping table: ${name}. Table does not exits`); + } else { + logging.info(`Dropping table: ${name}`); + await commands.deleteTable(name, connection); + } + } + } + ); +} + /** * Creates a migration which will add a permission to the database * @@ -221,6 +244,25 @@ function createNonTransactionalMigration(up, down) { }; } +/** + * @param {(connection: import('knex')) => Promise} up + * + * @returns {Migration} + */ +function createIrreversibleMigration(up) { + return { + config: { + irreversible: true + }, + async up(config) { + await up(config.connection); + }, + async down() { + return Promise.reject(); + } + }; +} + /** * @param {(connection: import('knex')) => Promise} up * @param {(connection: import('knex')) => Promise} down @@ -351,11 +393,13 @@ function createDropColumnMigration(table, column, columnDefinition) { module.exports = { addTable, + dropTables, addPermission, addPermissionToRole, addPermissionWithRoles, createTransactionalMigration, createNonTransactionalMigration, + createIrreversibleMigration, combineTransactionalMigrations, combineNonTransactionalMigrations, createAddColumnMigration,