2016-10-06 16:50:55 +03:00
|
|
|
var path = require('path'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
db = require('../db'),
|
|
|
|
errors = require('../../errors'),
|
2016-10-10 22:14:32 +03:00
|
|
|
i18n = require('../../i18n'),
|
|
|
|
ghostVersion = require('../../utils/ghost-version');
|
2016-10-06 16:50:55 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Database version has always two digits
|
|
|
|
* Database version is Ghost Version X.X
|
|
|
|
*
|
|
|
|
* @TODO: remove alpha text!
|
|
|
|
* @TODO: extend database validation
|
|
|
|
*/
|
|
|
|
function validateDatabaseVersion(version) {
|
|
|
|
if (version === null) {
|
|
|
|
throw new errors.DatabaseNotSeededError({
|
|
|
|
message: i18n.t('errors.data.versioning.index.databaseNotSeeded')
|
|
|
|
});
|
2014-06-01 03:53:21 +04:00
|
|
|
}
|
|
|
|
|
2016-10-06 16:50:55 +03:00
|
|
|
if (!version.match(/\d\.\d/gi)) {
|
|
|
|
throw new errors.DatabaseVersionError({
|
|
|
|
message: 'Your database version is not compatible with Ghost 1.0.0 Alpha (master branch)',
|
|
|
|
context: 'Want to keep your DB? Use Ghost < 1.0.0 or the "stable" branch. Otherwise please delete your DB and restart Ghost',
|
|
|
|
help: 'More information on the Ghost 1.0.0 Alpha at https://support.ghost.org/v1-0-alpha'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return version;
|
2014-06-01 03:53:21 +04:00
|
|
|
}
|
|
|
|
|
2016-10-06 16:50:55 +03:00
|
|
|
/**
|
|
|
|
* If the database version is null, the database was never seeded.
|
|
|
|
* The seed migration script will set your database to current Ghost Version.
|
|
|
|
*/
|
2014-06-01 03:53:21 +04:00
|
|
|
function getDatabaseVersion() {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex.schema.hasTable('settings').then(function (exists) {
|
2016-10-06 16:50:55 +03:00
|
|
|
if (!exists) {
|
|
|
|
return Promise.reject(new errors.DatabaseNotPopulatedError({
|
|
|
|
message: i18n.t('errors.data.versioning.index.databaseNotPopulated')
|
|
|
|
}));
|
2014-06-01 03:53:21 +04:00
|
|
|
}
|
2016-07-15 19:22:41 +03:00
|
|
|
|
2016-10-06 16:50:55 +03:00
|
|
|
return db.knex('settings')
|
|
|
|
.where('key', 'databaseVersion')
|
|
|
|
.first('value')
|
|
|
|
.then(function (version) {
|
|
|
|
return validateDatabaseVersion(version.value);
|
|
|
|
});
|
2014-06-01 03:53:21 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-06 16:50:55 +03:00
|
|
|
function getNewestDatabaseVersion() {
|
2016-10-10 22:14:32 +03:00
|
|
|
return ghostVersion.safe;
|
2014-06-01 03:53:21 +04:00
|
|
|
}
|
|
|
|
|
2016-10-06 16:50:55 +03:00
|
|
|
/**
|
|
|
|
* Database version cannot set from outside.
|
|
|
|
* If this function get called, we set the database version to your current Ghost version.
|
|
|
|
*/
|
|
|
|
function setDatabaseVersion(transaction) {
|
|
|
|
return (transaction || db.knex)('settings')
|
|
|
|
.where('key', 'databaseVersion')
|
|
|
|
.update({
|
|
|
|
value: getNewestDatabaseVersion()
|
|
|
|
});
|
2016-02-25 10:10:36 +03:00
|
|
|
}
|
|
|
|
|
2016-10-06 16:50:55 +03:00
|
|
|
/**
|
|
|
|
* return the versions which need migration
|
|
|
|
* when on 1.1 and we update to 1.4, we expect [1.2, 1.3, 1.4]
|
|
|
|
*/
|
2016-02-25 10:10:36 +03:00
|
|
|
function getMigrationVersions(fromVersion, toVersion) {
|
|
|
|
var versions = [],
|
|
|
|
i;
|
2016-07-14 13:59:42 +03:00
|
|
|
|
2016-10-06 16:50:55 +03:00
|
|
|
for (i = (fromVersion * 10) + 1; i <= toVersion * 10; i += 1) {
|
|
|
|
versions.push((i / 10).toString());
|
2016-02-25 10:10:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return versions;
|
|
|
|
}
|
|
|
|
|
2016-03-14 20:39:00 +03:00
|
|
|
/**
|
|
|
|
* ### Get Version Tasks
|
|
|
|
* Tries to require a directory matching the version number
|
|
|
|
*
|
|
|
|
* This was split from update to make testing easier
|
|
|
|
*
|
|
|
|
* @param {String} version
|
|
|
|
* @param {String} relPath
|
2016-07-14 13:59:42 +03:00
|
|
|
* @param {Function} logger
|
2016-03-14 20:39:00 +03:00
|
|
|
* @returns {Array}
|
|
|
|
*/
|
2016-07-14 13:59:42 +03:00
|
|
|
function getVersionTasks(version, relPath, logger) {
|
2016-03-14 20:39:00 +03:00
|
|
|
var tasks = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
tasks = require(path.join(relPath, version));
|
|
|
|
} catch (e) {
|
2016-07-14 13:59:42 +03:00
|
|
|
logger.info('No tasks found for version', version);
|
2016-03-14 20:39:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return tasks;
|
|
|
|
}
|
|
|
|
|
2016-07-14 13:59:42 +03:00
|
|
|
function getUpdateDatabaseTasks(version, logger) {
|
|
|
|
return getVersionTasks(version, '../migration/', logger);
|
2016-03-14 20:39:00 +03:00
|
|
|
}
|
|
|
|
|
2016-07-14 13:59:42 +03:00
|
|
|
function getUpdateFixturesTasks(version, logger) {
|
|
|
|
return getVersionTasks(version, '../migration/fixtures/', logger);
|
2016-03-14 20:39:00 +03:00
|
|
|
}
|
|
|
|
|
2014-06-01 03:53:21 +04:00
|
|
|
module.exports = {
|
2016-10-06 16:50:55 +03:00
|
|
|
canMigrateFromVersion: '1.0',
|
2016-07-15 19:22:41 +03:00
|
|
|
getNewestDatabaseVersion: getNewestDatabaseVersion,
|
2014-06-01 03:53:21 +04:00
|
|
|
getDatabaseVersion: getDatabaseVersion,
|
2016-02-25 10:10:36 +03:00
|
|
|
setDatabaseVersion: setDatabaseVersion,
|
2016-03-14 20:39:00 +03:00
|
|
|
getMigrationVersions: getMigrationVersions,
|
|
|
|
getUpdateDatabaseTasks: getUpdateDatabaseTasks,
|
|
|
|
getUpdateFixturesTasks: getUpdateFixturesTasks
|
2014-09-10 08:06:24 +04:00
|
|
|
};
|