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-06-01 03:53:21 +04:00
|
|
|
path = require('path'),
|
|
|
|
fs = require('fs'),
|
2016-01-25 20:50:04 +03:00
|
|
|
builder = require('./builder'),
|
|
|
|
fixtures = require('./fixtures'),
|
2014-01-03 04:37:21 +04:00
|
|
|
schema = require('../schema').tables,
|
2016-01-25 20:50:04 +03:00
|
|
|
commands = require('../schema').commands,
|
|
|
|
versioning = require('../schema').versioning,
|
2014-06-01 03:53:21 +04:00
|
|
|
dataExport = require('../export'),
|
2014-06-12 00:16:21 +04:00
|
|
|
config = require('../../config'),
|
2016-01-25 20:50:04 +03:00
|
|
|
errors = require('../../errors'),
|
|
|
|
models = require('../../models'),
|
|
|
|
sequence = require('../../utils/sequence'),
|
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
|
2016-02-25 10:10:36 +03:00
|
|
|
modelOptions,
|
2014-07-10 19:10:00 +04:00
|
|
|
logInfo,
|
|
|
|
populateDefaultSettings,
|
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,
|
2016-02-14 12:04:43 +03:00
|
|
|
migrateUpFreshDb,
|
|
|
|
backupDatabase;
|
2013-07-27 23:41:10 +04:00
|
|
|
|
2016-02-25 10:10:36 +03:00
|
|
|
// modelOptions & logInfo are passed through to migration/fixture actions
|
|
|
|
modelOptions = {context: {internal: true}};
|
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
logInfo = function logInfo(message) {
|
2016-02-14 22:36:49 +03:00
|
|
|
errors.logInfo('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
|
2016-02-14 22:36:49 +03:00
|
|
|
logInfo('Populating default settings');
|
2015-08-24 14:43:26 +03:00
|
|
|
return models.Settings.populateDefaults().then(function () {
|
2016-02-14 22:36:49 +03:00
|
|
|
logInfo('Complete');
|
2014-06-12 19:25:55 +04:00
|
|
|
});
|
2014-07-10 19:10:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
backupDatabase = function backupDatabase() {
|
2016-02-14 22:36:49 +03:00
|
|
|
logInfo('Creating database backup');
|
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 () {
|
2016-02-14 22:36:49 +03:00
|
|
|
logInfo('Database backup written to: ' + 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
|
2016-02-14 22:36:49 +03: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
|
2016-02-14 22:36:49 +03:00
|
|
|
logInfo('Up to date at version ' + 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(
|
2016-02-14 22:36:49 +03:00
|
|
|
'Your database is not compatible with this version of Ghost',
|
|
|
|
'You will need to create a new database'
|
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.
|
2016-02-14 22:36:49 +03: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
|
2016-02-14 22:36:49 +03:00
|
|
|
errors.logErrorAndExit('There is a problem with the database', 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 () {
|
2016-01-25 20:50:04 +03:00
|
|
|
return commands.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 () {
|
2016-02-14 22:36:49 +03:00
|
|
|
logInfo('Creating table: ' + table);
|
2016-01-25 20:50:04 +03:00
|
|
|
return commands.createTable(table);
|
2014-09-10 08:06:24 +04:00
|
|
|
};
|
|
|
|
});
|
2016-02-14 22:36:49 +03: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
|
2016-02-25 10:10:36 +03:00
|
|
|
return fixtures.populate(modelOptions, logInfo);
|
2014-07-10 19:10:00 +04:00
|
|
|
}).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
|
|
|
|
2016-02-25 10:10:36 +03:00
|
|
|
// Is the current version lower than the version we can migrate from?
|
|
|
|
// E.g. is this blog's DB older than 003?
|
|
|
|
if (fromVersion < versioning.canMigrateFromVersion) {
|
|
|
|
return versioning.showCannotMigrateError();
|
|
|
|
}
|
|
|
|
|
2014-06-01 03:53:21 +04:00
|
|
|
return backupDatabase().then(function () {
|
2016-01-25 20:50:04 +03:00
|
|
|
return commands.getTables();
|
2014-07-10 19:10:00 +04:00
|
|
|
}).then(function (tables) {
|
|
|
|
oldTables = tables;
|
|
|
|
if (!_.isEmpty(oldTables)) {
|
2016-01-25 20:50:04 +03:00
|
|
|
return commands.checkTables();
|
2014-01-15 17:29:23 +04:00
|
|
|
}
|
2014-06-12 19:25:55 +04:00
|
|
|
}).then(function () {
|
2016-01-25 20:50:04 +03:00
|
|
|
migrateOps = migrateOps.concat(builder.getDeleteCommands(oldTables, schemaTables));
|
|
|
|
migrateOps = migrateOps.concat(builder.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) {
|
2016-01-25 20:50:04 +03:00
|
|
|
return commands.getIndexes(table).then(function (indexes) {
|
|
|
|
modifyUniCommands = modifyUniCommands.concat(builder.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) {
|
2016-01-25 20:50:04 +03:00
|
|
|
return commands.getColumns(table).then(function (columns) {
|
2016-02-07 19:23:24 +03:00
|
|
|
migrateOps = migrateOps.concat(builder.dropColumnCommands(table, columns));
|
2016-01-25 20:50:04 +03:00
|
|
|
migrateOps = migrateOps.concat(builder.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)) {
|
2016-02-14 22:36:49 +03:00
|
|
|
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 () {
|
2016-02-25 10:10:36 +03:00
|
|
|
fromVersion = process.env.FORCE_MIGRATION ? versioning.canMigrateFromVersion : fromVersion;
|
|
|
|
var versions = versioning.getMigrationVersions(fromVersion, toVersion);
|
|
|
|
// Finally, run any updates to the fixtures, including default settings, that are required
|
|
|
|
// for anything other than the from/current version (which we're already on)
|
|
|
|
return fixtures.update(versions.slice(1), modelOptions, logInfo);
|
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,
|
2016-02-14 12:04:43 +03:00
|
|
|
backupDatabase: backupDatabase,
|
2013-11-18 18:21:15 +04:00
|
|
|
migrateUp: migrateUp,
|
|
|
|
migrateUpFreshDb: migrateUpFreshDb
|
2014-08-17 10:17:23 +04:00
|
|
|
};
|