2014-02-05 12:40:30 +04:00
|
|
|
|
var _ = require('lodash'),
|
2013-09-24 14:46:30 +04:00
|
|
|
|
when = require('when'),
|
|
|
|
|
errors = require('../../errorHandling'),
|
2013-11-18 18:21:15 +04:00
|
|
|
|
client = require('../../models/base').client,
|
2013-09-23 02:20:08 +04:00
|
|
|
|
knex = require('../../models/base').knex,
|
2013-11-18 18:21:15 +04:00
|
|
|
|
sequence = require('when/sequence'),
|
2013-09-15 02:14:39 +04:00
|
|
|
|
|
|
|
|
|
defaultSettings = require('../default-settings'),
|
2013-09-24 14:46:30 +04:00
|
|
|
|
Settings = require('../../models/settings').Settings,
|
|
|
|
|
fixtures = require('../fixtures'),
|
2014-01-03 04:37:21 +04:00
|
|
|
|
schema = require('../schema').tables,
|
2013-09-15 02:14:39 +04:00
|
|
|
|
|
2013-09-24 14:46:30 +04:00
|
|
|
|
initialVersion = '000',
|
2013-11-18 18:21:15 +04:00
|
|
|
|
schemaTables = _.keys(schema),
|
|
|
|
|
defaultDatabaseVersion,
|
|
|
|
|
|
|
|
|
|
init,
|
|
|
|
|
reset,
|
|
|
|
|
migrateUp,
|
|
|
|
|
migrateUpFreshDb,
|
|
|
|
|
getTables;
|
2013-09-15 02:14:39 +04:00
|
|
|
|
|
|
|
|
|
// Default Database Version
|
|
|
|
|
// The migration version number according to the hardcoded default settings
|
|
|
|
|
// This is the version the database should be at or migrated to
|
|
|
|
|
function getDefaultDatabaseVersion() {
|
|
|
|
|
if (!defaultDatabaseVersion) {
|
|
|
|
|
// This be the current version according to the software
|
|
|
|
|
defaultDatabaseVersion = _.find(defaultSettings.core, function (setting) {
|
|
|
|
|
return setting.key === 'databaseVersion';
|
|
|
|
|
}).defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return defaultDatabaseVersion;
|
|
|
|
|
}
|
2013-07-27 23:41:10 +04:00
|
|
|
|
|
2013-09-15 02:14:39 +04:00
|
|
|
|
// Database Current Version
|
|
|
|
|
// The migration version number according to the database
|
|
|
|
|
// This is what the database is currently at and may need to be updated
|
2013-09-15 01:13:59 +04:00
|
|
|
|
function getDatabaseVersion() {
|
2013-09-23 02:20:08 +04:00
|
|
|
|
return knex.schema.hasTable('settings').then(function (exists) {
|
2013-09-15 02:14:39 +04:00
|
|
|
|
// Check for the current version from the settings table
|
|
|
|
|
if (exists) {
|
|
|
|
|
// Temporary code to deal with old databases with currentVersion settings
|
|
|
|
|
return knex('settings')
|
|
|
|
|
.where('key', 'databaseVersion')
|
|
|
|
|
.orWhere('key', 'currentVersion')
|
|
|
|
|
.select('value')
|
|
|
|
|
.then(function (versions) {
|
|
|
|
|
var databaseVersion = _.reduce(versions, function (memo, version) {
|
2013-09-15 15:11:47 +04:00
|
|
|
|
if (isNaN(version.value)) {
|
|
|
|
|
errors.throwError('Database version is not recognised');
|
|
|
|
|
}
|
2013-09-15 02:14:39 +04:00
|
|
|
|
return parseInt(version.value, 10) > parseInt(memo, 10) ? version.value : memo;
|
|
|
|
|
}, initialVersion);
|
|
|
|
|
|
|
|
|
|
if (!databaseVersion || databaseVersion.length === 0) {
|
|
|
|
|
// we didn't get a response we understood, assume initialVersion
|
|
|
|
|
databaseVersion = initialVersion;
|
|
|
|
|
}
|
2013-09-15 15:11:47 +04:00
|
|
|
|
|
|
|
|
|
return databaseVersion;
|
2013-09-15 02:14:39 +04:00
|
|
|
|
});
|
|
|
|
|
}
|
2013-10-09 19:58:50 +04:00
|
|
|
|
throw new Error('Settings table does not exist');
|
2013-08-03 19:11:16 +04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-15 02:14:39 +04:00
|
|
|
|
function setDatabaseVersion() {
|
|
|
|
|
return knex('settings')
|
|
|
|
|
.where('key', 'databaseVersion')
|
|
|
|
|
.update({ 'value': defaultDatabaseVersion });
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
|
function createTable(table) {
|
|
|
|
|
return knex.schema.createTable(table, function (t) {
|
|
|
|
|
var column,
|
|
|
|
|
columnKeys = _.keys(schema[table]);
|
|
|
|
|
_.each(columnKeys, function (key) {
|
2014-01-15 17:29:23 +04:00
|
|
|
|
// creation distinguishes between text with fieldtype, string with maxlength and all others
|
|
|
|
|
if (schema[table][key].type === 'text' && schema[table][key].hasOwnProperty('fieldtype')) {
|
|
|
|
|
column = t[schema[table][key].type](key, schema[table][key].fieldtype);
|
|
|
|
|
} else if (schema[table][key].type === 'string' && schema[table][key].hasOwnProperty('maxlength')) {
|
2013-11-18 18:21:15 +04:00
|
|
|
|
column = t[schema[table][key].type](key, schema[table][key].maxlength);
|
|
|
|
|
} else {
|
|
|
|
|
column = t[schema[table][key].type](key);
|
2013-08-03 19:11:16 +04:00
|
|
|
|
}
|
2014-01-15 17:29:23 +04:00
|
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
|
if (schema[table][key].hasOwnProperty('nullable') && schema[table][key].nullable === true) {
|
|
|
|
|
column.nullable();
|
|
|
|
|
} else {
|
|
|
|
|
column.notNullable();
|
2013-09-15 02:14:39 +04:00
|
|
|
|
}
|
2013-11-18 18:21:15 +04:00
|
|
|
|
if (schema[table][key].hasOwnProperty('primary') && schema[table][key].primary === true) {
|
|
|
|
|
column.primary();
|
2013-09-15 02:14:39 +04:00
|
|
|
|
}
|
2013-11-18 18:21:15 +04:00
|
|
|
|
if (schema[table][key].hasOwnProperty('unique') && schema[table][key].unique) {
|
|
|
|
|
column.unique();
|
|
|
|
|
}
|
|
|
|
|
if (schema[table][key].hasOwnProperty('unsigned') && schema[table][key].unsigned) {
|
|
|
|
|
column.unsigned();
|
|
|
|
|
}
|
|
|
|
|
if (schema[table][key].hasOwnProperty('references') && schema[table][key].hasOwnProperty('inTable')) {
|
|
|
|
|
//check if table exists?
|
|
|
|
|
column.references(schema[table][key].references);
|
|
|
|
|
column.inTable(schema[table][key].inTable);
|
|
|
|
|
}
|
|
|
|
|
if (schema[table][key].hasOwnProperty('defaultTo')) {
|
|
|
|
|
column.defaultTo(schema[table][key].defaultTo);
|
2013-09-15 02:14:39 +04:00
|
|
|
|
}
|
|
|
|
|
});
|
2013-11-18 18:21:15 +04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function deleteTable(table) {
|
|
|
|
|
return knex.schema.dropTableIfExists(table);
|
|
|
|
|
}
|
2013-07-27 23:41:10 +04:00
|
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
|
function getDeleteCommands(oldTables, newTables) {
|
|
|
|
|
var deleteTables = _.difference(oldTables, newTables);
|
|
|
|
|
if (!_.isEmpty(deleteTables)) {
|
|
|
|
|
return _.map(deleteTables, function (table) {
|
2013-07-27 23:41:10 +04:00
|
|
|
|
return function () {
|
2013-11-18 18:21:15 +04:00
|
|
|
|
return deleteTable(table);
|
2013-07-27 23:41:10 +04:00
|
|
|
|
};
|
|
|
|
|
});
|
2013-11-18 18:21:15 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-27 23:41:10 +04:00
|
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
|
function getAddCommands(oldTables, newTables) {
|
|
|
|
|
var addTables = _.difference(newTables, oldTables);
|
|
|
|
|
if (!_.isEmpty(addTables)) {
|
|
|
|
|
return _.map(addTables, function (table) {
|
2013-08-03 19:11:16 +04:00
|
|
|
|
return function () {
|
2013-11-18 18:21:15 +04:00
|
|
|
|
return createTable(table);
|
2013-08-03 19:11:16 +04:00
|
|
|
|
};
|
|
|
|
|
});
|
2013-11-18 18:21:15 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-03 19:11:16 +04:00
|
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
|
function getTablesFromSqlite3() {
|
|
|
|
|
return knex.raw("select * from sqlite_master where type = 'table'").then(function (response) {
|
|
|
|
|
return _.reject(_.pluck(response[0], 'tbl_name'), function (name) {
|
|
|
|
|
return name === 'sqlite_sequence';
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-08-03 19:11:16 +04:00
|
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
|
function getTablesFromPgSQL() {
|
2013-11-26 13:35:33 +04:00
|
|
|
|
return knex.raw("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'").then(function (response) {
|
2014-01-06 23:32:20 +04:00
|
|
|
|
return _.flatten(_.pluck(response.rows, 'table_name'));
|
2013-11-26 13:35:33 +04:00
|
|
|
|
});
|
2013-11-18 18:21:15 +04:00
|
|
|
|
}
|
2013-07-27 23:41:10 +04:00
|
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
|
function getTablesFromMySQL() {
|
|
|
|
|
return knex.raw("show tables").then(function (response) {
|
|
|
|
|
return _.flatten(_.map(response[0], function (entry) {
|
|
|
|
|
return _.values(entry);
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-07-27 23:41:10 +04:00
|
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
|
// Check for whether data is needed to be bootstrapped or not
|
|
|
|
|
init = function () {
|
|
|
|
|
var self = this;
|
|
|
|
|
// There are 4 possibilities:
|
|
|
|
|
// 1. The database exists and is up-to-date
|
|
|
|
|
// 2. The database exists but is out of date
|
|
|
|
|
// 3. The database exists but the currentVersion setting does not or cannot be understood
|
|
|
|
|
// 4. The database has not yet been created
|
|
|
|
|
return getDatabaseVersion().then(function (databaseVersion) {
|
|
|
|
|
var defaultVersion = getDefaultDatabaseVersion();
|
|
|
|
|
if (databaseVersion === defaultVersion) {
|
|
|
|
|
// 1. The database exists and is up-to-date
|
|
|
|
|
return when.resolve();
|
2013-07-27 23:41:10 +04:00
|
|
|
|
}
|
2013-11-18 18:21:15 +04:00
|
|
|
|
if (databaseVersion < defaultVersion) {
|
|
|
|
|
// 2. The database exists but is out of date
|
|
|
|
|
// Migrate to latest version
|
|
|
|
|
return self.migrateUp().then(function () {
|
|
|
|
|
// Finally update the databases current version
|
|
|
|
|
return setDatabaseVersion();
|
|
|
|
|
});
|
2013-07-27 23:41:10 +04:00
|
|
|
|
}
|
2013-11-18 18:21:15 +04:00
|
|
|
|
if (databaseVersion > defaultVersion) {
|
|
|
|
|
// 3. The database exists but the currentVersion setting does not or cannot be understood
|
|
|
|
|
// In this case we don't understand the version because it is too high
|
|
|
|
|
errors.logErrorAndExit(
|
|
|
|
|
'Your database is not compatible with this version of Ghost',
|
|
|
|
|
'You will need to create a new database'
|
|
|
|
|
);
|
2013-08-03 19:11:16 +04:00
|
|
|
|
}
|
2013-11-18 18:21:15 +04:00
|
|
|
|
}, function (err) {
|
|
|
|
|
if (err.message || err === 'Settings table does not exist') {
|
|
|
|
|
// 4. The database has not yet been created
|
|
|
|
|
// Bring everything up from initial version.
|
|
|
|
|
return self.migrateUpFreshDb();
|
|
|
|
|
}
|
|
|
|
|
// 3. The database exists but the currentVersion setting does not or cannot be understood
|
|
|
|
|
// In this case the setting was missing or there was some other problem
|
|
|
|
|
errors.logErrorAndExit('There is a problem with the database', err.message || err);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ### Reset
|
|
|
|
|
// Delete all tables from the database in reverse order
|
|
|
|
|
reset = function () {
|
|
|
|
|
var tables = [];
|
|
|
|
|
tables = _.map(schemaTables, function (table) {
|
|
|
|
|
return function () {
|
|
|
|
|
return deleteTable(table);
|
|
|
|
|
};
|
|
|
|
|
}).reverse();
|
|
|
|
|
|
|
|
|
|
return sequence(tables);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Only do this if we have no database at all
|
|
|
|
|
migrateUpFreshDb = function () {
|
|
|
|
|
var tables = [];
|
|
|
|
|
tables = _.map(schemaTables, function (table) {
|
|
|
|
|
return function () {
|
|
|
|
|
return createTable(table);
|
|
|
|
|
};
|
|
|
|
|
});
|
2013-08-03 19:11:16 +04:00
|
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
|
return sequence(tables).then(function () {
|
|
|
|
|
// Load the fixtures
|
|
|
|
|
return fixtures.populateFixtures().then(function () {
|
|
|
|
|
// Initialise the default settings
|
|
|
|
|
return Settings.populateDefaults();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-01-15 17:29:23 +04:00
|
|
|
|
// 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
|
|
|
|
|
function checkMySQLPostTable() {
|
|
|
|
|
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') {
|
|
|
|
|
return knex.raw("ALTER TABLE posts MODIFY " + entry.Field + " MEDIUMTEXT").then(function () {
|
|
|
|
|
return when.resolve();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
|
// Migrate from a specific version to the latest
|
|
|
|
|
migrateUp = function () {
|
|
|
|
|
return getTables().then(function (oldTables) {
|
2014-03-14 21:36:45 +04:00
|
|
|
|
// if tables exist and client is mysqls check if posts table is okay
|
2014-01-15 17:29:23 +04:00
|
|
|
|
if (!_.isEmpty(oldTables) && client === 'mysql') {
|
|
|
|
|
return checkMySQLPostTable().then(function () {
|
|
|
|
|
return oldTables;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return oldTables;
|
|
|
|
|
}).then(function (oldTables) {
|
2013-11-18 18:21:15 +04:00
|
|
|
|
var deleteCommands = getDeleteCommands(oldTables, schemaTables),
|
|
|
|
|
addCommands = getAddCommands(oldTables, schemaTables),
|
|
|
|
|
commands = [];
|
2014-01-15 17:29:23 +04:00
|
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
|
if (!_.isEmpty(deleteCommands)) {
|
|
|
|
|
commands = commands.concat(deleteCommands);
|
|
|
|
|
}
|
|
|
|
|
if (!_.isEmpty(addCommands)) {
|
|
|
|
|
commands = commands.concat(addCommands);
|
2013-08-03 19:11:16 +04:00
|
|
|
|
}
|
2013-11-18 18:21:15 +04:00
|
|
|
|
if (!_.isEmpty(commands)) {
|
|
|
|
|
return sequence(commands);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2014-03-14 21:36:45 +04:00
|
|
|
|
}).then(function () {
|
|
|
|
|
return fixtures.updateFixtures();
|
2013-11-18 18:21:15 +04:00
|
|
|
|
});
|
|
|
|
|
};
|
2013-08-03 19:11:16 +04:00
|
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
|
getTables = function () {
|
|
|
|
|
if (client === 'sqlite3') {
|
|
|
|
|
return getTablesFromSqlite3();
|
|
|
|
|
}
|
|
|
|
|
if (client === 'mysql') {
|
|
|
|
|
return getTablesFromMySQL();
|
|
|
|
|
}
|
|
|
|
|
if (client === 'pg') {
|
|
|
|
|
return getTablesFromPgSQL();
|
2013-07-27 23:41:10 +04:00
|
|
|
|
}
|
2013-11-18 18:21:15 +04:00
|
|
|
|
return when.reject("No support for database client " + client);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
getDatabaseVersion: getDatabaseVersion,
|
|
|
|
|
init: init,
|
|
|
|
|
reset: reset,
|
|
|
|
|
migrateUp: migrateUp,
|
|
|
|
|
migrateUpFreshDb: migrateUpFreshDb
|
2013-07-27 23:41:10 +04:00
|
|
|
|
};
|