2014-02-05 12:40:30 +04:00
|
|
|
var _ = require('lodash'),
|
2013-09-24 14:46:30 +04:00
|
|
|
when = require('when'),
|
2014-06-01 03:53:21 +04:00
|
|
|
path = require('path'),
|
|
|
|
fs = require('fs'),
|
2014-06-16 07:33:25 +04:00
|
|
|
nodefn = require('when/node'),
|
2014-05-09 14:11:29 +04:00
|
|
|
errors = require('../../errors'),
|
2013-11-18 18:21:15 +04:00
|
|
|
sequence = require('when/sequence'),
|
2013-09-15 02:14:39 +04:00
|
|
|
|
2014-06-01 03:53:21 +04:00
|
|
|
versioning = require('../versioning'),
|
2014-05-16 06:29:42 +04:00
|
|
|
models = require('../../models'),
|
2013-09-24 14:46:30 +04:00
|
|
|
fixtures = require('../fixtures'),
|
2014-01-03 04:37:21 +04:00
|
|
|
schema = require('../schema').tables,
|
2014-06-01 03:53:21 +04:00
|
|
|
dataExport = require('../export'),
|
2014-06-10 00:37:44 +04:00
|
|
|
utils = require('../utils'),
|
2014-06-12 00:16:21 +04:00
|
|
|
config = require('../../config'),
|
2013-09-15 02:14:39 +04:00
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
schemaTables = _.keys(schema),
|
|
|
|
|
|
|
|
init,
|
|
|
|
reset,
|
|
|
|
migrateUp,
|
2014-06-10 00:37:44 +04:00
|
|
|
migrateUpFreshDb;
|
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 () {
|
2014-06-10 00:37:44 +04:00
|
|
|
return utils.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 () {
|
2014-06-10 00:37:44 +04:00
|
|
|
return utils.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
|
|
|
|
2014-06-12 19:25:55 +04:00
|
|
|
function addColumnCommands(table, columns) {
|
|
|
|
var columnKeys = _.keys(schema[table]),
|
|
|
|
addColumns = _.difference(columnKeys, columns);
|
|
|
|
|
|
|
|
return _.map(addColumns, function (column) {
|
|
|
|
return function () {
|
|
|
|
utils.addColumn(table, column);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function modifyUniqueCommands(table, indexes) {
|
|
|
|
var columnKeys = _.keys(schema[table]);
|
|
|
|
return _.map(columnKeys, function (column) {
|
|
|
|
if (schema[table][column].unique && schema[table][column].unique === true) {
|
|
|
|
if (!_.contains(indexes, table + '_' + column + '_unique')) {
|
|
|
|
return function () {
|
|
|
|
return utils.addUnique(table, column);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} else if (!schema[table][column].unique) {
|
|
|
|
if (_.contains(indexes, table + '_' + column + '_unique')) {
|
|
|
|
return function () {
|
|
|
|
return utils.dropUnique(table, column);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
2014-06-01 03:53:21 +04:00
|
|
|
return versioning.getDatabaseVersion().then(function (databaseVersion) {
|
|
|
|
var defaultVersion = versioning.getDefaultDatabaseVersion();
|
2013-11-18 18:21:15 +04:00
|
|
|
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
|
2014-06-01 03:53:21 +04:00
|
|
|
return versioning.setDatabaseVersion();
|
2013-11-18 18:21:15 +04:00
|
|
|
});
|
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 () {
|
2014-06-10 00:37:44 +04:00
|
|
|
return utils.deleteTable(table);
|
2013-11-18 18:21:15 +04:00
|
|
|
};
|
|
|
|
}).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 () {
|
2014-06-10 00:37:44 +04:00
|
|
|
return utils.createTable(table);
|
2013-11-18 18:21:15 +04:00
|
|
|
};
|
|
|
|
});
|
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
|
2014-05-16 06:29:42 +04:00
|
|
|
return models.Settings.populateDefaults();
|
2013-11-18 18:21:15 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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.
|
2014-05-15 07:28:10 +04:00
|
|
|
// For details see: https://github.com/TryGhost/Ghost/issues/1947
|
2014-01-15 17:29:23 +04:00
|
|
|
function checkMySQLPostTable() {
|
2014-05-15 07:28:10 +04:00
|
|
|
var knex = config().database.knex;
|
|
|
|
|
2014-01-15 17:29:23 +04:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-01 03:53:21 +04:00
|
|
|
function backupDatabase() {
|
|
|
|
return dataExport().then(function (exportedData) {
|
|
|
|
// Save the exported data to the file system for download
|
2014-06-12 00:16:21 +04:00
|
|
|
var fileName = path.resolve(config().paths.contentPath + '/data/exported-' + (new Date().getTime()) + '.json');
|
2014-06-01 03:53:21 +04:00
|
|
|
|
|
|
|
return nodefn.call(fs.writeFile, fileName, JSON.stringify(exportedData));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
// Migrate from a specific version to the latest
|
|
|
|
migrateUp = function () {
|
2014-06-12 19:25:55 +04:00
|
|
|
var deleteCommands,
|
|
|
|
addCommands,
|
|
|
|
oldTables,
|
2014-05-15 07:28:10 +04:00
|
|
|
client = config().database.client,
|
2014-06-12 19:25:55 +04:00
|
|
|
addColumns = [],
|
|
|
|
modifyUniCommands = [],
|
|
|
|
commands = [];
|
|
|
|
|
2014-06-01 03:53:21 +04:00
|
|
|
return backupDatabase().then(function () {
|
2014-06-12 19:25:55 +04:00
|
|
|
return utils.getTables().then(function (tables) {
|
|
|
|
oldTables = tables;
|
|
|
|
});
|
|
|
|
}).then(function () {
|
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') {
|
2014-06-12 19:25:55 +04:00
|
|
|
return checkMySQLPostTable();
|
2014-01-15 17:29:23 +04:00
|
|
|
}
|
2014-06-12 19:25:55 +04:00
|
|
|
}).then(function () {
|
|
|
|
deleteCommands = getDeleteCommands(oldTables, schemaTables);
|
|
|
|
addCommands = getAddCommands(oldTables, schemaTables);
|
|
|
|
return when.all(
|
|
|
|
_.map(oldTables, function (table) {
|
|
|
|
return utils.getIndexes(table).then(function (indexes) {
|
|
|
|
modifyUniCommands = modifyUniCommands.concat(modifyUniqueCommands(table, indexes));
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}).then(function () {
|
|
|
|
return when.all(
|
|
|
|
_.map(oldTables, function (table) {
|
|
|
|
return utils.getColumns(table).then(function (columns) {
|
|
|
|
addColumns = addColumns.concat(addColumnCommands(table, columns));
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
}).then(function () {
|
|
|
|
modifyUniCommands = _.compact(modifyUniCommands);
|
2014-01-15 17:29:23 +04:00
|
|
|
|
2014-06-12 19:25:55 +04:00
|
|
|
// delete tables
|
2013-11-18 18:21:15 +04:00
|
|
|
if (!_.isEmpty(deleteCommands)) {
|
|
|
|
commands = commands.concat(deleteCommands);
|
|
|
|
}
|
2014-06-12 19:25:55 +04:00
|
|
|
// add tables
|
2013-11-18 18:21:15 +04:00
|
|
|
if (!_.isEmpty(addCommands)) {
|
|
|
|
commands = commands.concat(addCommands);
|
2013-08-03 19:11:16 +04:00
|
|
|
}
|
2014-06-12 19:25:55 +04:00
|
|
|
// add columns if needed
|
|
|
|
if (!_.isEmpty(addColumns)) {
|
|
|
|
commands = commands.concat(addColumns);
|
|
|
|
}
|
|
|
|
// add/drop unique constraint
|
|
|
|
if (!_.isEmpty(modifyUniCommands)) {
|
|
|
|
commands = commands.concat(modifyUniCommands);
|
|
|
|
}
|
|
|
|
// execute the commands in sequence
|
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
|
|
|
module.exports = {
|
|
|
|
init: init,
|
|
|
|
reset: reset,
|
|
|
|
migrateUp: migrateUp,
|
|
|
|
migrateUpFreshDb: migrateUpFreshDb
|
2014-05-15 07:28:10 +04:00
|
|
|
};
|