Skip to content

Commit 3f07b05

Browse files
committed
feat(sqlite): ensure sqlite db path is absolute
- ensure dbpath argument is absolute by default - add migration to make relative sqlite paths absolute
1 parent d654bf4 commit 3f07b05

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

lib/migrations.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
'use strict';
1+
const path = require('path');
22

33
async function ensureSettingsFolder(context) {
4-
const path = require('path');
54
const ghostUser = require('./utils/use-ghost-user');
65

76
const contentDir = context.instance.config.get('paths.contentPath');
@@ -14,8 +13,25 @@ async function ensureSettingsFolder(context) {
1413
}
1514
}
1615

16+
async function makeSqliteAbsolute({instance}) {
17+
const configs = await instance.getAvailableConfigs();
18+
19+
Object.values(configs).forEach((config) => {
20+
const currentFilename = config.get('database.connection.filename', null);
21+
if (!currentFilename || path.isAbsolute(currentFilename)) {
22+
return;
23+
}
24+
25+
config.set('database.connection.filename', path.resolve(instance.dir, currentFilename)).save();
26+
});
27+
}
28+
1729
module.exports = [{
1830
before: '1.7.0',
1931
title: 'Create content/settings directory',
2032
task: ensureSettingsFolder
33+
}, {
34+
before: '1.14.1',
35+
title: 'Fix Sqlite DB path',
36+
task: makeSqliteAbsolute
2137
}];

lib/tasks/configure/options.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
'use strict';
21
// Advanced options for the config command
32
const portfinder = require('portfinder');
43
const toString = require('lodash/toString');
54
const validator = require('validator');
65
const urlUtils = require('../../utils/url');
76
const url = require('url');
7+
const path = require('path');
88

99
const BASE_PORT = 2368;
1010
const knownMailServices = [
@@ -24,6 +24,7 @@ module.exports = {
2424
description: 'Site domain E.g. loveghost.com',
2525
validate: urlUtils.validate,
2626
transform: urlUtils.ensureProtocol,
27+
2728
type: 'string',
2829
group: 'Ghost Options:'
2930
},
@@ -83,8 +84,9 @@ module.exports = {
8384
}
8485

8586
const dbFile = (environment === 'production') ? 'ghost.db' : 'ghost-dev.db';
86-
return `./content/data/${dbFile}`;
87-
}
87+
return path.resolve(`./content/data/${dbFile}`);
88+
},
89+
transform: path.resolve
8890
},
8991
dbhost: {
9092
description: 'Database host',

test/unit/migrations-spec.js

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
const expect = require('chai').expect;
1+
const {expect} = require('chai');
42
const sinon = require('sinon');
53
const createConfig = require('../utils/config-stub');
64

@@ -53,4 +51,34 @@ describe('Unit: Migrations', function () {
5351
});
5452
});
5553
});
54+
55+
it('makeSqliteAbsolute makes sqlite filepaths absolute', async () => {
56+
const configs = {
57+
development: createConfig(),
58+
staging: createConfig(),
59+
production: createConfig()
60+
};
61+
62+
configs.development.get.withArgs('database.connection.filename', null).returns('./content/data/ghost.db');
63+
configs.staging.get.withArgs('database.connection.filename', null).returns('/absolute/path/content/data/ghost.db');
64+
configs.production.get.withArgs('database.connection.filename', null).returns(null);
65+
66+
const instance = {
67+
getAvailableConfigs: sinon.stub().resolves(configs),
68+
dir: '/test/instance/dir'
69+
};
70+
71+
await migrations[1].task({instance});
72+
73+
expect(instance.getAvailableConfigs.calledOnce).to.be.true;
74+
expect(configs.development.get.calledOnce).to.be.true;
75+
expect(configs.development.set.calledWithExactly('database.connection.filename', '/test/instance/dir/content/data/ghost.db')).to.be.true;
76+
expect(configs.development.save.calledOnce).to.be.true;
77+
expect(configs.staging.get.calledOnce).to.be.true;
78+
expect(configs.staging.set.called).to.be.false;
79+
expect(configs.staging.save.called).to.be.false;
80+
expect(configs.production.get.calledOnce).to.be.true;
81+
expect(configs.production.set.called).to.be.false;
82+
expect(configs.production.save.called).to.be.false;
83+
});
5684
});

test/unit/tasks/configure/options-spec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const {expect} = require('chai');
22
const sinon = require('sinon');
33
const proxyquire = require('proxyquire');
44
const Promise = require('bluebird');
5+
const path = require('path');
56

67
const options = require('../../../../lib/tasks/configure/options');
78
const urlUtils = require('../../../../lib/utils/url');
@@ -64,8 +65,8 @@ describe('Unit: Tasks: Configure > options', function () {
6465
it('dbpath', function () {
6566
expect(options.dbpath).to.exist;
6667
expect(options.dbpath.defaultValue({get: () => 'mysql'})).to.be.null;
67-
expect(options.dbpath.defaultValue({get: () => 'sqlite3'}, 'development')).to.equal('./content/data/ghost-dev.db');
68-
expect(options.dbpath.defaultValue({get: () => 'sqlite3'}, 'production')).to.equal('./content/data/ghost.db');
68+
expect(options.dbpath.defaultValue({get: () => 'sqlite3'}, 'development')).to.equal(path.resolve('./content/data/ghost-dev.db'));
69+
expect(options.dbpath.defaultValue({get: () => 'sqlite3'}, 'production')).to.equal(path.resolve('./content/data/ghost.db'));
6970
});
7071

7172
it('mail', function () {

0 commit comments

Comments
 (0)