Skip to content

Commit 9c07274

Browse files
committed
fix(migrate): improve error handling with sqlite install failure
refs #281 - add better error message when sqlite install hasn't worked
1 parent 6eec35c commit 9c07274

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/tasks/migrate.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = function runMigrations(context) {
3838
return knexMigratorPromise.then(() => {
3939
config.set('logging.transports', transports).save();
4040
}).catch((error) => {
41-
if (error.stderr.match(/CODE\: ENOTFOUND/)) {
41+
if (error.stderr && error.stderr.match(/CODE\: ENOTFOUND/)) {
4242
// Database not found
4343
error = new errors.ConfigError({
4444
config: {
@@ -47,7 +47,7 @@ module.exports = function runMigrations(context) {
4747
message: 'Invalid database host',
4848
environment: context.instance.system.environment
4949
});
50-
} else if (error.stderr.match(/CODE\: ER_ACCESS_DENIED_ERROR/)) {
50+
} else if (error.stderr && error.stderr.match(/CODE\: ER_ACCESS_DENIED_ERROR/)) {
5151
error = new errors.ConfigError({
5252
config: {
5353
'database.connection.user': config.get('database.connection.user'),
@@ -56,6 +56,13 @@ module.exports = function runMigrations(context) {
5656
message: 'Invalid database username or password',
5757
environment: context.instance.system.environment
5858
});
59+
} else if (error.stdout && error.stdout.match(/npm install sqlite3 \-\-save/)) {
60+
// We check stdout because knex outputs to stdout on this particular error
61+
error = new errors.SystemError({
62+
message: 'It appears that sqlite3 did not install properly when Ghost-CLI was installed.\n' +
63+
'Please either uninstall and reinstall Ghost-CLI, or switch to MySQL',
64+
help: 'https://docs.ghost.org/v1.0.0/docs/troubleshooting#section-sqlite3-install-failure'
65+
});
5966
}
6067

6168
config.set('logging.transports', transports).save();

test/unit/tasks/migrate-spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,26 @@ describe('Unit: Tasks > Migrate', function () {
113113
expect(config.set.args[1]).to.deep.equal(['logging.transports', ['stdout', 'file']]);
114114
});
115115
});
116+
117+
it('throws system error if sqlite3 error is thrown by knex', function () {
118+
let config = getConfigStub();
119+
config.get.withArgs('logging.transports', null).returns(['stdout', 'file']);
120+
let execaStub = sinon.stub().returns(Promise.reject({stdout: 'Knex: run\n$ npm install sqlite3 --save\nError:'}));
121+
let useGhostUserStub = sinon.stub().returns(false);
122+
123+
let migrate = proxyquire(migratePath, {
124+
execa: execaStub,
125+
'../utils/use-ghost-user': useGhostUserStub
126+
});
127+
128+
return migrate({ instance: {config: config, dir: '/some-dir', system: {environment: 'testing'}}}).then(() => {
129+
expect(false, 'error should have been thrown').to.be.true;
130+
}).catch((error) => {
131+
expect(error).to.be.an.instanceof(errors.SystemError);
132+
expect(error.message).to.match(/sqlite3 did not install properly/);
133+
expect(config.set.calledTwice).to.be.true;
134+
expect(config.set.args[0]).to.deep.equal(['logging.transports', ['file']]);
135+
expect(config.set.args[1]).to.deep.equal(['logging.transports', ['stdout', 'file']]);
136+
});
137+
});
116138
});

0 commit comments

Comments
 (0)