Skip to content

Commit

Permalink
fix(migrate): improve error handling with sqlite install failure
Browse files Browse the repository at this point in the history
refs #281
- add better error message when sqlite install hasn't worked
  • Loading branch information
acburdine committed Jul 31, 2017
1 parent 6eec35c commit 9c07274
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/tasks/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = function runMigrations(context) {
return knexMigratorPromise.then(() => {
config.set('logging.transports', transports).save();
}).catch((error) => {
if (error.stderr.match(/CODE\: ENOTFOUND/)) {
if (error.stderr && error.stderr.match(/CODE\: ENOTFOUND/)) {
// Database not found
error = new errors.ConfigError({
config: {
Expand All @@ -47,7 +47,7 @@ module.exports = function runMigrations(context) {
message: 'Invalid database host',
environment: context.instance.system.environment
});
} else if (error.stderr.match(/CODE\: ER_ACCESS_DENIED_ERROR/)) {
} else if (error.stderr && error.stderr.match(/CODE\: ER_ACCESS_DENIED_ERROR/)) {
error = new errors.ConfigError({
config: {
'database.connection.user': config.get('database.connection.user'),
Expand All @@ -56,6 +56,13 @@ module.exports = function runMigrations(context) {
message: 'Invalid database username or password',
environment: context.instance.system.environment
});
} else if (error.stdout && error.stdout.match(/npm install sqlite3 \-\-save/)) {
// We check stdout because knex outputs to stdout on this particular error
error = new errors.SystemError({
message: 'It appears that sqlite3 did not install properly when Ghost-CLI was installed.\n' +
'Please either uninstall and reinstall Ghost-CLI, or switch to MySQL',
help: 'https://docs.ghost.org/v1.0.0/docs/troubleshooting#section-sqlite3-install-failure'
});
}

config.set('logging.transports', transports).save();
Expand Down
22 changes: 22 additions & 0 deletions test/unit/tasks/migrate-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,26 @@ describe('Unit: Tasks > Migrate', function () {
expect(config.set.args[1]).to.deep.equal(['logging.transports', ['stdout', 'file']]);
});
});

it('throws system error if sqlite3 error is thrown by knex', function () {
let config = getConfigStub();
config.get.withArgs('logging.transports', null).returns(['stdout', 'file']);
let execaStub = sinon.stub().returns(Promise.reject({stdout: 'Knex: run\n$ npm install sqlite3 --save\nError:'}));
let useGhostUserStub = sinon.stub().returns(false);

let migrate = proxyquire(migratePath, {
execa: execaStub,
'../utils/use-ghost-user': useGhostUserStub
});

return migrate({ instance: {config: config, dir: '/some-dir', system: {environment: 'testing'}}}).then(() => {
expect(false, 'error should have been thrown').to.be.true;
}).catch((error) => {
expect(error).to.be.an.instanceof(errors.SystemError);
expect(error.message).to.match(/sqlite3 did not install properly/);
expect(config.set.calledTwice).to.be.true;
expect(config.set.args[0]).to.deep.equal(['logging.transports', ['file']]);
expect(config.set.args[1]).to.deep.equal(['logging.transports', ['stdout', 'file']]);
});
});
});

0 comments on commit 9c07274

Please sign in to comment.