diff --git a/core/server/config/index.js b/core/server/config/index.js index cee5842865..960981cad0 100644 --- a/core/server/config/index.js +++ b/core/server/config/index.js @@ -7,12 +7,14 @@ var path = require('path'), when = require('when'), url = require('url'), _ = require('lodash'), + knex = require('knex'), requireTree = require('../require-tree').readAll, theme = require('./theme'), configUrl = require('./url'), ghostConfig = {}, appRoot = path.resolve(__dirname, '../../../'), - corePath = path.resolve(appRoot, 'core/'); + corePath = path.resolve(appRoot, 'core/'), + knexInstance; // Are we using sockets? Custom socket or the default? function getSocket() { @@ -51,7 +53,14 @@ function updateConfig(config) { // Otherwise default to default content path location contentPath = ghostConfig.paths.contentPath || path.resolve(appRoot, 'content'); + if (!knexInstance && ghostConfig.database) { + knexInstance = knex(ghostConfig.database); + } + _.merge(ghostConfig, { + database: { + knex: knexInstance + }, paths: { 'appRoot': appRoot, 'subdir': subdir, diff --git a/core/server/data/migration/index.js b/core/server/data/migration/index.js index eae2d510b0..4a645c903a 100644 --- a/core/server/data/migration/index.js +++ b/core/server/data/migration/index.js @@ -4,8 +4,6 @@ var _ = require('lodash'), fs = require('fs'), nodefn = require('when/node'), errors = require('../../errors'), - client = require('../../models/base').client, - knex = require('../../models/base').knex, sequence = require('when/sequence'), versioning = require('../versioning'), @@ -151,8 +149,10 @@ migrateUpFreshDb = function () { // This function changes the type of posts.html and posts.markdown columns to mediumtext. Due to // a wrong datatype in schema.js some installations using mysql could have been created using the // data type text instead of mediumtext. -// For details see: https://github.com/TryGhost/Ghost/issues/1947 +// For details see: https://github.com/TryGhost/Ghost/issues/1947 function checkMySQLPostTable() { + var knex = config().database.knex; + return knex.raw("SHOW FIELDS FROM posts where Field ='html' OR Field = 'markdown'").then(function (response) { return _.flatten(_.map(response[0], function (entry) { if (entry.Type.toLowerCase() !== 'mediumtext') { @@ -178,6 +178,7 @@ migrateUp = function () { var deleteCommands, addCommands, oldTables, + client = config().database.client, addColumns = [], modifyUniCommands = [], commands = []; @@ -244,4 +245,4 @@ module.exports = { reset: reset, migrateUp: migrateUp, migrateUpFreshDb: migrateUpFreshDb -}; +}; \ No newline at end of file diff --git a/core/server/models/base.js b/core/server/models/base.js index 9d808b316c..d96984f038 100644 --- a/core/server/models/base.js +++ b/core/server/models/base.js @@ -6,7 +6,6 @@ // accesses the models directly. All other parts of Ghost, including the blog frontend, admin UI, and apps are only // allowed to access data via the API. var bookshelf = require('bookshelf'), - knex = require('knex'), when = require('when'), moment = require('moment'), _ = require('lodash'), @@ -21,8 +20,7 @@ var bookshelf = require('bookshelf'), // ### ghostBookshelf // Initializes a new Bookshelf instance called ghostBookshelf, for reference elsewhere in Ghost. -ghostBookshelf = bookshelf(knex(config().database)); -ghostBookshelf.client = config().database.client; +ghostBookshelf = bookshelf(config().database.knex); // ### ghostBookshelf.Model // The Base Model which other Ghost objects will inherit from, diff --git a/core/server/update-check.js b/core/server/update-check.js index 60ad1bcf9c..c22eb7949b 100644 --- a/core/server/update-check.js +++ b/core/server/update-check.js @@ -70,7 +70,7 @@ function updateCheckData() { data.ghost_version = currentVersion; data.node_version = process.versions.node; data.env = process.env.NODE_ENV; - data.database_type = require('./models/base').client; + data.database_type = config().database.client; data.email_transport = mailConfig && (mailConfig.options && mailConfig.options.service ? mailConfig.options.service : mailConfig.transport); return when.settle(ops).then(function (descriptors) {