Skip to content

Commit

Permalink
🎨 make sqlite filename absolute (#7585)
Browse files Browse the repository at this point in the history
no issue

- add tests for makePathsAbsolute
- add support for windows paths

When Ghost-CLI inits the database of the current GhostVersion (in /current), then it uses knex-migrator to do that.
Knex migrator is reading the .knex-migrator file of the current Ghost version. This returns a relative path to the database location.
The problem: knex-migrator will init the database in the root folder of Ghost-CLI /content/data instead of /current/content . And when you start Ghost (ghost start), it always complains that
that database is not initialised, because it expects the database in /current/content...

* 🎨  move config_spec to config/index_spec
- add one more test case
  • Loading branch information
kirrg001 authored and ErisDS committed Oct 18, 2016
1 parent af4534c commit fd0a08a
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 14 deletions.
4 changes: 3 additions & 1 deletion core/server/config/index.js
Expand Up @@ -30,8 +30,10 @@ nconf.file('ghost4', __dirname + '/defaults.json');

/**
* transform all relative paths to absolute paths
* transform sqlite filename path for Ghost-CLI
*/
localUtils.makePathsAbsolute.bind(nconf)();
localUtils.makePathsAbsolute.bind(nconf)(nconf.get('paths'), 'paths');
localUtils.makePathsAbsolute.bind(nconf)(nconf.get('database:connection'), 'database:connection');

/**
* values we have to set manual
Expand Down
31 changes: 24 additions & 7 deletions core/server/config/utils.js
@@ -1,5 +1,6 @@
var path = require('path'),
_ = require('lodash');
_ = require('lodash'),
errors = require('../errors');

exports.isPrivacyDisabled = function isPrivacyDisabled(privacyFlag) {
if (!this.get('privacy')) {
Expand All @@ -16,20 +17,36 @@ exports.isPrivacyDisabled = function isPrivacyDisabled(privacyFlag) {
/**
* transform all relative paths to absolute paths
* @TODO: imagesRelPath is a dirty little attribute (especially when looking at the usages)
* @TODO: re-write this function a little bit so we don't have to add the parent path - that is hard to understand
*
* Path must be string.
* Path must match minimum one / or \
* Path can be a "." to re-present current folder
*/
exports.makePathsAbsolute = function makePathsAbsolute(paths, parent) {
exports.makePathsAbsolute = function makePathsAbsolute(obj, parent) {
var self = this;

if (!paths && !parent) {
paths = this.get('paths');
parent = 'paths';
if (!obj) {
throw new errors.IncorrectUsageError({
message: 'makePathsAbsolute: Object is missing.'
});
}

_.each(paths, function (configValue, pathsKey) {
if (!parent) {
throw new errors.IncorrectUsageError({
message: 'makePathsAbsolute: Parent is missing.'
});
}

_.each(obj, function (configValue, pathsKey) {
if (_.isObject(configValue)) {
makePathsAbsolute.bind(self)(configValue, parent + ':' + pathsKey);
} else {
if (configValue[0] !== '/' && pathsKey !== 'imagesRelPath') {
if (_.isString(configValue) &&
(configValue.match(/\/+|\\+/) || configValue === '.') &&
(configValue[0] !== '/' && configValue[0] !== '\\') &&
pathsKey !== 'imagesRelPath'
) {
self.set(parent + ':' + pathsKey, path.join(__dirname + '/../../../', configValue));
}
}
Expand Down
Expand Up @@ -6,14 +6,14 @@ var should = require('should'),
fs = require('fs'),
_ = require('lodash'),

testUtils = require('../utils'),
i18n = require('../../server/i18n'),
utils = require('../../server/utils'),
testUtils = require('../../utils'),
i18n = require('../../../server/i18n'),
utils = require('../../../server/utils'),
/*jshint unused:false*/
db = require('../../server/data/db/connection'),
db = require('../../../server/data/db/connection'),

// Thing we are testing
configUtils = require('../utils/configUtils'),
configUtils = require('../../utils/configUtils'),
config = configUtils.config;

i18n.init();
Expand Down Expand Up @@ -106,9 +106,10 @@ describe('Config', function () {

it('should have the correct values for each key', function () {
var pathConfig = config.get('paths'),
appRoot = path.resolve(__dirname, '../../../');
appRoot = path.resolve(__dirname, '../../../../');

pathConfig.should.have.property('appRoot', appRoot);
pathConfig.should.have.property('imagesRelPath', 'content/images');
});

it('should allow specific properties to be user defined', function () {
Expand Down
83 changes: 83 additions & 0 deletions core/test/unit/config/utils_spec.js
@@ -0,0 +1,83 @@
var configUtils = require('../../../server/config/utils'),
should = require('should');

should.equal(true, true);

describe('UNIT: Config utils', function () {
describe('makePathsAbsolute', function () {
it('ensure we change paths only', function () {
var changedKey = [],
obj = {
database: {
client: 'mysql',
connection: {
filename: 'content/data/ghost.db'
}
}
};

this.set = function (key, value) {
changedKey.push([key, value]);
};

configUtils.makePathsAbsolute.bind(this)(obj.database, 'database');

changedKey.length.should.eql(1);
changedKey[0][0].should.eql('database:connection:filename');
changedKey[0][1].should.not.eql('content/data/ghost.db');
});

it('ensure it skips non strings', function () {
var changedKey = [],
obj = {
database: {
test: 10
}
};

this.set = function (key, value) {
changedKey.push([key, value]);
};

configUtils.makePathsAbsolute.bind(this)(obj.database, 'database');
changedKey.length.should.eql(0);
});

it('ensure we don\' change absolute paths', function () {
var changedKey = [],
obj = {
database: {
client: 'mysql',
connection: {
filename: '/content/data/ghost.db'
}
}
};

this.set = function (key, value) {
changedKey.push([key, value]);
};

configUtils.makePathsAbsolute.bind(this)(obj.database, 'database');
changedKey.length.should.eql(0);
});

it('match paths on windows', function () {
var changedKey = [],
obj = {
database: {
filename: 'content\\data\\ghost.db'
}
};

this.set = function (key, value) {
changedKey.push([key, value]);
};

configUtils.makePathsAbsolute.bind(this)(obj.database, 'database');
changedKey.length.should.eql(1);
changedKey[0][0].should.eql('database:filename');
changedKey[0][1].should.not.eql('content\\data\\ghost.db');
});
});
});

0 comments on commit fd0a08a

Please sign in to comment.