Skip to content

Commit

Permalink
fix(migrate): re-work migrate step to run knex-migrator in a subprocess
Browse files Browse the repository at this point in the history
closes TryGhost#349
- move knex-migrator to run in a child process, which allows us to initialize a sqlite database during a production ubuntu install
  • Loading branch information
acburdine committed Jul 14, 2017
1 parent 3a450e1 commit 4528162
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions lib/tasks/migrate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';
const path = require('path');
const KnexMigrator = require('knex-migrator');
const execa = require('execa');

const errors = require('../errors');
const shouldUseGhostUser = require('../utils/use-ghost-user');

module.exports = function runMigrations(context) {
let config = context.instance.config;
Expand All @@ -12,22 +13,33 @@ module.exports = function runMigrations(context) {
}

let transports = config.get('logging.transports', null);
// TODO: revisit just hiding migration output altogether
config.set('logging.transports', []).save();
config.set('logging.transports', ['file']).save();

let knexMigrator = new KnexMigrator({
knexMigratorFilePath: path.join(process.cwd(), 'current')
});
let contentDir = path.join(context.instance.dir, 'content');
let currentDir = path.join(context.instance.dir, 'current');
let knexMigratorPromise;

return knexMigrator.isDatabaseOK().catch((error) => {
if (error.code === 'DB_NOT_INITIALISED' ||
error.code === 'MIGRATION_TABLE_IS_MISSING') {
return knexMigrator.init();
} else if (error.code === 'DB_NEEDS_MIGRATION') {
return knexMigrator.migrate();
}
let args = ['--init', '--mgpath', currentDir];

// If we're using sqlite and the ghost user owns the content folder, then
// we should run sudo, otherwise run normally
if (shouldUseGhostUser(contentDir)) {
let knexMigratorPath = path.resolve(__dirname, '../../node_modules/.bin/knex-migrator-migrate');
knexMigratorPromise = execa('sudo', ['-E', '-u', 'ghost', knexMigratorPath].concat(args), {
stderr: 'pipe'
});
} else {
knexMigratorPromise = execa('knex-migrator-migrate', args, {
preferLocal: true,
localDir: __dirname,
stderr: 'pipe'
});
}

if (error.code === 'ENOTFOUND') {
return knexMigratorPromise.then(() => {
config.set('logging.transports', transports).save();
}).catch((error) => {
if (error.stderr.match(/CODE\: ENOTFOUND/)) {
// Database not found
error = new errors.ConfigError({
config: {
Expand All @@ -36,7 +48,7 @@ module.exports = function runMigrations(context) {
message: 'Invalid database host',
environment: context.instance.system.environment
});
} else if (error.code === 'ER_ACCESS_DENIED_ERROR') {
} else if (error.stderr.match(/CODE\: ER_ACCESS_DENIED_ERROR/)) {
error = new errors.ConfigError({
config: {
'database.connection.user': config.get('database.connection.user'),
Expand All @@ -49,8 +61,6 @@ module.exports = function runMigrations(context) {

config.set('logging.transports', transports).save();
return Promise.reject(error);
}).then(() => {
config.set('logging.transports', transports).save();
});
}

0 comments on commit 4528162

Please sign in to comment.