2014-02-05 12:40:30 +04:00
|
|
|
var _ = require('lodash'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2015-09-25 22:03:33 +03:00
|
|
|
crypto = require('crypto'),
|
2014-08-17 10:17:23 +04:00
|
|
|
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'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../../i18n'),
|
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,
|
2015-09-25 22:03:33 +03:00
|
|
|
fixClientSecret,
|
2014-07-10 19:10:00 +04:00
|
|
|
|
|
|
|
// 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) {
|
2015-11-12 15:29:45 +03:00
|
|
|
errors.logInfo(i18n.t('notices.data.migration.index.migrations'), message);
|
2014-07-10 19:10:00 +04:00
|
|
|
};
|
2014-07-11 11:12:07 +04:00
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
populateDefaultSettings = function populateDefaultSettings() {
|
|
|
|
// Initialise the default settings
|
2015-11-12 15:29:45 +03:00
|
|
|
logInfo(i18n.t('notices.data.migration.index.populatingDefaultSettings'));
|
2015-08-24 14:43:26 +03:00
|
|
|
return models.Settings.populateDefaults().then(function () {
|
2015-11-12 15:29:45 +03:00
|
|
|
logInfo(i18n.t('notices.data.migration.index.complete'));
|
2014-06-12 19:25:55 +04:00
|
|
|
});
|
2014-07-10 19:10:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
backupDatabase = function backupDatabase() {
|
2015-11-12 15:29:45 +03:00
|
|
|
logInfo(i18n.t('notices.data.migration.index.creatingDatabaseBackup'));
|
2014-07-10 19:10:00 +04:00
|
|
|
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 () {
|
2015-11-12 15:29:45 +03:00
|
|
|
logInfo(i18n.t('notices.data.migration.index.databaseBackupDestination', {filename: fileName}));
|
2014-07-31 20:22:05 +04:00
|
|
|
});
|
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
|
|
|
|
2015-09-25 22:03:33 +03:00
|
|
|
// TODO: move to migration.to005() for next DB version
|
|
|
|
fixClientSecret = function () {
|
|
|
|
return models.Clients.forge().query('where', 'secret', '=', 'not_available').fetch().then(function updateClients(results) {
|
|
|
|
return Promise.map(results.models, function mapper(client) {
|
|
|
|
if (process.env.NODE_ENV.indexOf('testing') !== 0) {
|
|
|
|
logInfo('Updating client secret');
|
|
|
|
client.secret = crypto.randomBytes(6).toString('hex');
|
|
|
|
}
|
|
|
|
return models.Client.edit(client, {context: {internal: true}, id: client.id});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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
|
2015-11-12 15:29:45 +03:00
|
|
|
logInfo(i18n.t('notices.data.migration.index.databaseUpgradeRequired',
|
|
|
|
{dbVersion: databaseVersion, defaultVersion: 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
|
2015-11-12 15:29:45 +03:00
|
|
|
logInfo(i18n.t('notices.data.migration.index.upToDateAtVersion', {dbVersion: databaseVersion}));
|
2015-09-25 22:03:33 +03:00
|
|
|
// TODO: temporary fix for missing client.secret
|
|
|
|
return fixClientSecret();
|
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(
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n.t('notices.data.migration.index.databaseNotCompatible.error'),
|
|
|
|
i18n.t('notices.data.migration.index.databaseNotCompatible.help')
|
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 (err) {
|
|
|
|
if (err.message || err === 'Settings table does not exist') {
|
|
|
|
// 4. The database has not yet been created
|
|
|
|
// Bring everything up from initial version.
|
2015-11-12 15:29:45 +03:00
|
|
|
logInfo(i18n.t('notices.data.migration.index.dbInitialisationRequired', {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
|
2015-11-12 15:29:45 +03:00
|
|
|
errors.logErrorAndExit(i18n.t('notices.data.migration.index.problemWithDatabase'), err.message || err);
|
2013-11-18 18:21:15 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// ### 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 () {
|
2015-11-12 15:29:45 +03:00
|
|
|
logInfo(i18n.t('notices.data.migration.index.creatingTable', {table: table}));
|
2014-09-10 08:06:24 +04:00
|
|
|
return utils.createTable(table);
|
|
|
|
};
|
|
|
|
});
|
2015-11-12 15:29:45 +03:00
|
|
|
logInfo(i18n.t('notices.data.migration.index.creatingTables'));
|
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)) {
|
2015-11-12 15:29:45 +03:00
|
|
|
logInfo(i18n.t('notices.data.migration.index.runningMigrations'));
|
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
|
|
|
};
|