2014-02-05 12:40:30 +04:00
|
|
|
var _ = require('lodash'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
|
|
|
sequence = require('../../utils/sequence'),
|
2014-06-01 03:53:21 +04:00
|
|
|
path = require('path'),
|
|
|
|
fs = require('fs'),
|
2014-05-09 14:11:29 +04:00
|
|
|
errors = require('../../errors'),
|
2014-07-17 18:33:21 +04:00
|
|
|
commands = require('./commands'),
|
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),
|
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
// private
|
|
|
|
logInfo,
|
|
|
|
populateDefaultSettings,
|
|
|
|
backupDatabase,
|
|
|
|
|
|
|
|
// public
|
2013-11-18 18:21:15 +04:00
|
|
|
init,
|
|
|
|
reset,
|
|
|
|
migrateUp,
|
2014-06-10 00:37:44 +04:00
|
|
|
migrateUpFreshDb;
|
2013-07-27 23:41:10 +04:00
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
logInfo = function logInfo(message) {
|
|
|
|
errors.logInfo('Migrations', message);
|
|
|
|
};
|
2014-07-11 11:12:07 +04:00
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
populateDefaultSettings = function populateDefaultSettings() {
|
|
|
|
// Initialise the default settings
|
|
|
|
logInfo('Populating default settings');
|
2015-08-24 14:43:26 +03:00
|
|
|
return models.Settings.populateDefaults().then(function () {
|
2014-07-10 19:10:00 +04:00
|
|
|
logInfo('Complete');
|
2014-06-12 19:25:55 +04:00
|
|
|
});
|
2014-07-10 19:10:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
backupDatabase = function backupDatabase() {
|
|
|
|
logInfo('Creating database backup');
|
|
|
|
return dataExport().then(function (exportedData) {
|
|
|
|
// Save the exported data to the file system for download
|
2014-07-31 20:22:05 +04:00
|
|
|
return dataExport.fileName().then(function (fileName) {
|
|
|
|
fileName = path.resolve(config.paths.contentPath + '/data/' + fileName);
|
2014-07-10 19:10:00 +04:00
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.promisify(fs.writeFile)(fileName, JSON.stringify(exportedData)).then(function () {
|
2014-07-31 20:22:05 +04:00
|
|
|
logInfo('Database backup written to: ' + fileName);
|
|
|
|
});
|
2014-07-10 19:10:00 +04:00
|
|
|
});
|
2014-06-12 19:25:55 +04:00
|
|
|
});
|
2014-07-10 19:10:00 +04:00
|
|
|
};
|
2014-06-12 19:25:55 +04:00
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
// Check for whether data is needed to be bootstrapped or not
|
2014-07-21 21:50:04 +04:00
|
|
|
init = function (tablesOnly) {
|
|
|
|
tablesOnly = tablesOnly || false;
|
|
|
|
|
2013-11-18 18:21:15 +04:00
|
|
|
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();
|
2014-07-16 19:24:22 +04:00
|
|
|
|
|
|
|
if (databaseVersion < defaultVersion || process.env.FORCE_MIGRATION) {
|
2013-11-18 18:21:15 +04:00
|
|
|
// 2. The database exists but is out of date
|
|
|
|
// Migrate to latest version
|
2014-07-10 19:10:00 +04:00
|
|
|
logInfo('Database upgrade required from version ' + databaseVersion + ' to ' + defaultVersion);
|
2014-07-11 11:12:07 +04:00
|
|
|
return self.migrateUp(databaseVersion, defaultVersion).then(function () {
|
2013-11-18 18:21:15 +04:00
|
|
|
// 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
|
|
|
}
|
2014-07-16 19:24:22 +04:00
|
|
|
|
|
|
|
if (databaseVersion === defaultVersion) {
|
|
|
|
// 1. The database exists and is up-to-date
|
|
|
|
logInfo('Up to date at version ' + databaseVersion);
|
2014-08-17 10:17:23 +04:00
|
|
|
return;
|
2014-07-16 19:24:22 +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.
|
2014-07-10 19:10:00 +04:00
|
|
|
logInfo('Database initialisation required for version ' + versioning.getDefaultDatabaseVersion());
|
2014-07-21 21:50:04 +04:00
|
|
|
return self.migrateUpFreshDb(tablesOnly);
|
2013-11-18 18:21:15 +04:00
|
|
|
}
|
|
|
|
// 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 () {
|
2014-07-10 19:10:00 +04:00
|
|
|
var tables = _.map(schemaTables, function (table) {
|
2013-11-18 18:21:15 +04:00
|
|
|
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
|
2014-07-21 21:50:04 +04:00
|
|
|
migrateUpFreshDb = function (tablesOnly) {
|
|
|
|
var tableSequence,
|
|
|
|
tables = _.map(schemaTables, function (table) {
|
2014-09-10 08:06:24 +04:00
|
|
|
return function () {
|
|
|
|
logInfo('Creating table: ' + table);
|
|
|
|
return utils.createTable(table);
|
|
|
|
};
|
|
|
|
});
|
2014-07-10 19:10:00 +04:00
|
|
|
logInfo('Creating tables...');
|
2014-07-21 21:50:04 +04:00
|
|
|
tableSequence = sequence(tables);
|
|
|
|
|
|
|
|
if (tablesOnly) {
|
|
|
|
return tableSequence;
|
|
|
|
}
|
|
|
|
return tableSequence.then(function () {
|
2013-11-18 18:21:15 +04:00
|
|
|
// Load the fixtures
|
2014-07-10 19:10:00 +04:00
|
|
|
return fixtures.populate();
|
|
|
|
}).then(function () {
|
|
|
|
return populateDefaultSettings();
|
2013-11-18 18:21:15 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Migrate from a specific version to the latest
|
2014-07-11 11:12:07 +04:00
|
|
|
migrateUp = function (fromVersion, toVersion) {
|
2014-07-10 19:10:00 +04:00
|
|
|
var oldTables,
|
2014-06-12 19:25:55 +04:00
|
|
|
modifyUniCommands = [],
|
2014-07-10 19:10:00 +04:00
|
|
|
migrateOps = [];
|
2014-06-12 19:25:55 +04:00
|
|
|
|
2014-06-01 03:53:21 +04:00
|
|
|
return backupDatabase().then(function () {
|
2014-07-10 19:10:00 +04:00
|
|
|
return utils.getTables();
|
|
|
|
}).then(function (tables) {
|
|
|
|
oldTables = tables;
|
|
|
|
if (!_.isEmpty(oldTables)) {
|
|
|
|
return utils.checkTables();
|
2014-01-15 17:29:23 +04:00
|
|
|
}
|
2014-06-12 19:25:55 +04:00
|
|
|
}).then(function () {
|
2014-07-10 19:10:00 +04:00
|
|
|
migrateOps = migrateOps.concat(commands.getDeleteCommands(oldTables, schemaTables));
|
|
|
|
migrateOps = migrateOps.concat(commands.getAddCommands(oldTables, schemaTables));
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.all(
|
2014-06-12 19:25:55 +04:00
|
|
|
_.map(oldTables, function (table) {
|
|
|
|
return utils.getIndexes(table).then(function (indexes) {
|
2014-07-10 19:10:00 +04:00
|
|
|
modifyUniCommands = modifyUniCommands.concat(commands.modifyUniqueCommands(table, indexes));
|
2014-06-12 19:25:55 +04:00
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}).then(function () {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.all(
|
2014-06-12 19:25:55 +04:00
|
|
|
_.map(oldTables, function (table) {
|
|
|
|
return utils.getColumns(table).then(function (columns) {
|
2014-07-10 19:10:00 +04:00
|
|
|
migrateOps = migrateOps.concat(commands.addColumnCommands(table, columns));
|
2014-06-12 19:25:55 +04:00
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}).then(function () {
|
2014-07-10 19:10:00 +04:00
|
|
|
migrateOps = migrateOps.concat(_.compact(modifyUniCommands));
|
2014-01-15 17:29:23 +04:00
|
|
|
|
2014-06-12 19:25:55 +04:00
|
|
|
// execute the commands in sequence
|
2014-07-10 19:10:00 +04:00
|
|
|
if (!_.isEmpty(migrateOps)) {
|
|
|
|
logInfo('Running migrations');
|
2014-08-17 10:17:23 +04:00
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
return sequence(migrateOps);
|
2013-11-18 18:21:15 +04:00
|
|
|
}
|
2014-03-14 21:36:45 +04:00
|
|
|
}).then(function () {
|
2015-08-24 14:43:26 +03:00
|
|
|
// Ensure all of the current default settings are created (these are fixtures, so should be inserted first)
|
2014-07-10 19:10:00 +04:00
|
|
|
return populateDefaultSettings();
|
2015-08-24 14:43:26 +03:00
|
|
|
}).then(function () {
|
|
|
|
// Finally, run any updates to the fixtures, including default settings
|
|
|
|
return fixtures.update(fromVersion, toVersion);
|
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-08-17 10:17:23 +04:00
|
|
|
};
|