Skip to content

Commit

Permalink
feat(sqlite): ensure sqlite db path is absolute
Browse files Browse the repository at this point in the history
- ensure dbpath argument is absolute by default
- add migration to make relative sqlite paths absolute
  • Loading branch information
acburdine committed Jun 3, 2020
1 parent d654bf4 commit 3f07b05
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
20 changes: 18 additions & 2 deletions lib/migrations.js
@@ -1,7 +1,6 @@
'use strict';
const path = require('path');

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

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

async function makeSqliteAbsolute({instance}) {
const configs = await instance.getAvailableConfigs();

Object.values(configs).forEach((config) => {
const currentFilename = config.get('database.connection.filename', null);
if (!currentFilename || path.isAbsolute(currentFilename)) {
return;
}

config.set('database.connection.filename', path.resolve(instance.dir, currentFilename)).save();
});
}

module.exports = [{
before: '1.7.0',
title: 'Create content/settings directory',
task: ensureSettingsFolder
}, {
before: '1.14.1',
title: 'Fix Sqlite DB path',
task: makeSqliteAbsolute
}];
8 changes: 5 additions & 3 deletions lib/tasks/configure/options.js
@@ -1,10 +1,10 @@
'use strict';
// Advanced options for the config command
const portfinder = require('portfinder');
const toString = require('lodash/toString');
const validator = require('validator');
const urlUtils = require('../../utils/url');
const url = require('url');
const path = require('path');

const BASE_PORT = 2368;
const knownMailServices = [
Expand All @@ -24,6 +24,7 @@ module.exports = {
description: 'Site domain E.g. loveghost.com',
validate: urlUtils.validate,
transform: urlUtils.ensureProtocol,

type: 'string',
group: 'Ghost Options:'
},
Expand Down Expand Up @@ -83,8 +84,9 @@ module.exports = {
}

const dbFile = (environment === 'production') ? 'ghost.db' : 'ghost-dev.db';
return `./content/data/${dbFile}`;
}
return path.resolve(`./content/data/${dbFile}`);
},
transform: path.resolve
},
dbhost: {
description: 'Database host',
Expand Down
34 changes: 31 additions & 3 deletions test/unit/migrations-spec.js
@@ -1,6 +1,4 @@
'use strict';

const expect = require('chai').expect;
const {expect} = require('chai');
const sinon = require('sinon');
const createConfig = require('../utils/config-stub');

Expand Down Expand Up @@ -53,4 +51,34 @@ describe('Unit: Migrations', function () {
});
});
});

it('makeSqliteAbsolute makes sqlite filepaths absolute', async () => {
const configs = {
development: createConfig(),
staging: createConfig(),
production: createConfig()
};

configs.development.get.withArgs('database.connection.filename', null).returns('./content/data/ghost.db');
configs.staging.get.withArgs('database.connection.filename', null).returns('/absolute/path/content/data/ghost.db');
configs.production.get.withArgs('database.connection.filename', null).returns(null);

const instance = {
getAvailableConfigs: sinon.stub().resolves(configs),
dir: '/test/instance/dir'
};

await migrations[1].task({instance});

expect(instance.getAvailableConfigs.calledOnce).to.be.true;
expect(configs.development.get.calledOnce).to.be.true;
expect(configs.development.set.calledWithExactly('database.connection.filename', '/test/instance/dir/content/data/ghost.db')).to.be.true;
expect(configs.development.save.calledOnce).to.be.true;
expect(configs.staging.get.calledOnce).to.be.true;
expect(configs.staging.set.called).to.be.false;
expect(configs.staging.save.called).to.be.false;
expect(configs.production.get.calledOnce).to.be.true;
expect(configs.production.set.called).to.be.false;
expect(configs.production.save.called).to.be.false;
});
});
5 changes: 3 additions & 2 deletions test/unit/tasks/configure/options-spec.js
Expand Up @@ -2,6 +2,7 @@ const {expect} = require('chai');
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const Promise = require('bluebird');
const path = require('path');

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

it('mail', function () {
Expand Down

0 comments on commit 3f07b05

Please sign in to comment.