From 9876f130134af6c12a59e9366e7f57f362c0e51e Mon Sep 17 00:00:00 2001 From: Jure Triglav <juretriglav@gmail.com> Date: Thu, 24 Jan 2019 14:09:55 +1300 Subject: [PATCH] feat(db-manager): allow passing options to umzug --- packages/db-manager/package.json | 2 +- packages/db-manager/src/commands/migrate.js | 6 +++--- packages/db-manager/src/helpers/umzug.js | 22 ++++++++++----------- packages/server/test/helpers/db_cleaner.js | 4 ++-- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/packages/db-manager/package.json b/packages/db-manager/package.json index 12f0848ed..c5f86250f 100644 --- a/packages/db-manager/package.json +++ b/packages/db-manager/package.json @@ -36,7 +36,7 @@ "lodash": "^4.17.11", "objection": "^1.3.0", "pg": "^7.4.1", - "tmp": "^0.0.33", + "tmp-promise": "^1.0.5", "umzug": "^2.1.0" }, "peerDependencies": { diff --git a/packages/db-manager/src/commands/migrate.js b/packages/db-manager/src/commands/migrate.js index 4beb2fa0e..25ca960db 100644 --- a/packages/db-manager/src/commands/migrate.js +++ b/packages/db-manager/src/commands/migrate.js @@ -1,9 +1,9 @@ const getUmzug = require('../helpers/umzug') const getMigrationPaths = require('../helpers/migrationPaths') -const migrate = async () => { - const umzug = await getUmzug(getMigrationPaths()) - await umzug.up() +const migrate = async options => { + const { umzug, cleanup } = await getUmzug(getMigrationPaths()) + await umzug.up(options).finally(() => cleanup()) } module.exports = migrate diff --git a/packages/db-manager/src/helpers/umzug.js b/packages/db-manager/src/helpers/umzug.js index 26763b464..ef1148691 100644 --- a/packages/db-manager/src/helpers/umzug.js +++ b/packages/db-manager/src/helpers/umzug.js @@ -2,13 +2,10 @@ const logger = require('@pubsweet/logger') const db = require('../db') const Umzug = require('umzug') const fs = require('fs-extra') -const path = require('path') -const tmp = require('tmp') -const { promisify } = require('util') +const { extname } = require('path') +const tmp = require('tmp-promise') const storage = require('./umzugStorage') -const makeTempDir = promisify(tmp.dir) - // load SQL files as migrations const sqlResolver = filePath => ({ up: async db => { @@ -19,8 +16,8 @@ const sqlResolver = filePath => ({ const getUmzug = async migrationsPaths => { // collect up all migrations to be run - const tempDir = await makeTempDir({ - prefix: 'migrations-', + const { path, cleanup } = await tmp.dir({ + prefix: '_migrations-', unsafeCleanup: true, dir: process.cwd(), }) @@ -29,26 +26,27 @@ const getUmzug = async migrationsPaths => { await Promise.all( migrationsPaths.map(async migrationPath => { if (await fs.exists(migrationPath)) { - await fs.copy(migrationPath, tempDir) + await fs.copy(migrationPath, path) } }), ) - - return new Umzug({ + const umzug = new Umzug({ storage, logging: logger.debug.bind(logger), migrations: { - path: tempDir, + path, params: [db], pattern: /\d+-[\w-]+\.(js|sql)/, customResolver: filePath => { - if (path.extname(filePath) === '.sql') { + if (extname(filePath) === '.sql') { return sqlResolver(filePath) } return require(filePath) }, }, }) + + return { cleanup, umzug } } module.exports = getUmzug diff --git a/packages/server/test/helpers/db_cleaner.js b/packages/server/test/helpers/db_cleaner.js index 455d631f2..1b1ab77ef 100644 --- a/packages/server/test/helpers/db_cleaner.js +++ b/packages/server/test/helpers/db_cleaner.js @@ -1,11 +1,11 @@ const { db, migrate } = require('@pubsweet/db-manager') const logger = require('@pubsweet/logger') -const dbCleaner = async () => { +const dbCleaner = async options => { await db.raw('DROP SCHEMA public CASCADE;') await db.raw('CREATE SCHEMA public;') await db.raw('GRANT ALL ON SCHEMA public TO public;') - await migrate() + await migrate(options) logger.info('Dropped all tables and ran all migrations') } -- GitLab