2014-05-31 18:53:21 -05:00
|
|
|
var _ = require('lodash'),
|
2016-02-12 11:56:27 +00:00
|
|
|
db = require('../db'),
|
2014-05-31 18:53:21 -05:00
|
|
|
errors = require('../../errors'),
|
2015-11-12 13:29:45 +01:00
|
|
|
i18n = require('../../i18n'),
|
2016-01-25 17:50:04 +00:00
|
|
|
defaultSettings = require('./default-settings'),
|
2014-05-31 18:53:21 -05:00
|
|
|
|
|
|
|
initialVersion = '000',
|
|
|
|
defaultDatabaseVersion;
|
|
|
|
|
|
|
|
// Default Database Version
|
|
|
|
// The migration version number according to the hardcoded default settings
|
|
|
|
// This is the version the database should be at or migrated to
|
|
|
|
function getDefaultDatabaseVersion() {
|
|
|
|
if (!defaultDatabaseVersion) {
|
|
|
|
// This be the current version according to the software
|
2014-06-17 10:36:47 -05:00
|
|
|
defaultDatabaseVersion = defaultSettings.core.databaseVersion.defaultValue;
|
2014-05-31 18:53:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return defaultDatabaseVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Database Current Version
|
|
|
|
// The migration version number according to the database
|
|
|
|
// This is what the database is currently at and may need to be updated
|
|
|
|
function getDatabaseVersion() {
|
2016-02-12 11:56:27 +00:00
|
|
|
return db.knex.schema.hasTable('settings').then(function (exists) {
|
2014-05-31 18:53:21 -05:00
|
|
|
// Check for the current version from the settings table
|
|
|
|
if (exists) {
|
|
|
|
// Temporary code to deal with old databases with currentVersion settings
|
2016-02-12 11:56:27 +00:00
|
|
|
return db.knex('settings')
|
2014-05-31 18:53:21 -05:00
|
|
|
.where('key', 'databaseVersion')
|
|
|
|
.orWhere('key', 'currentVersion')
|
|
|
|
.select('value')
|
|
|
|
.then(function (versions) {
|
|
|
|
var databaseVersion = _.reduce(versions, function (memo, version) {
|
|
|
|
if (isNaN(version.value)) {
|
2015-11-12 13:29:45 +01:00
|
|
|
errors.throwError(i18n.t('errors.data.versioning.index.dbVersionNotRecognized'));
|
2014-05-31 18:53:21 -05:00
|
|
|
}
|
|
|
|
return parseInt(version.value, 10) > parseInt(memo, 10) ? version.value : memo;
|
|
|
|
}, initialVersion);
|
|
|
|
|
|
|
|
if (!databaseVersion || databaseVersion.length === 0) {
|
|
|
|
// we didn't get a response we understood, assume initialVersion
|
|
|
|
databaseVersion = initialVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
return databaseVersion;
|
|
|
|
});
|
|
|
|
}
|
2015-11-12 13:29:45 +01:00
|
|
|
throw new Error(i18n.t('errors.data.versioning.index.settingsTableDoesNotExist'));
|
2014-05-31 18:53:21 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setDatabaseVersion() {
|
2016-02-12 11:56:27 +00:00
|
|
|
return db.knex('settings')
|
2014-05-31 18:53:21 -05:00
|
|
|
.where('key', 'databaseVersion')
|
2014-09-10 00:06:24 -04:00
|
|
|
.update({value: defaultDatabaseVersion});
|
2014-05-31 18:53:21 -05:00
|
|
|
}
|
|
|
|
|
2016-02-25 08:10:36 +01:00
|
|
|
function pad(num, width) {
|
|
|
|
return Array(Math.max(width - String(num).length + 1, 0)).join(0) + num;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMigrationVersions(fromVersion, toVersion) {
|
|
|
|
var versions = [],
|
|
|
|
i;
|
|
|
|
for (i = parseInt(fromVersion, 10); i <= toVersion; i += 1) {
|
|
|
|
versions.push(pad(i, 3));
|
|
|
|
}
|
|
|
|
|
|
|
|
return versions;
|
|
|
|
}
|
|
|
|
|
|
|
|
function showCannotMigrateError() {
|
|
|
|
return errors.logAndRejectError(
|
|
|
|
i18n.t('errors.data.versioning.index.cannotMigrate.error'),
|
|
|
|
i18n.t('errors.data.versioning.index.cannotMigrate.context'),
|
|
|
|
i18n.t('common.seeLinkForInstructions', {link: 'http://support.ghost.org/how-to-upgrade/'})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-31 18:53:21 -05:00
|
|
|
module.exports = {
|
2016-02-25 08:10:36 +01:00
|
|
|
canMigrateFromVersion: '003',
|
|
|
|
showCannotMigrateError: showCannotMigrateError,
|
2014-05-31 18:53:21 -05:00
|
|
|
getDefaultDatabaseVersion: getDefaultDatabaseVersion,
|
|
|
|
getDatabaseVersion: getDatabaseVersion,
|
2016-02-25 08:10:36 +01:00
|
|
|
setDatabaseVersion: setDatabaseVersion,
|
|
|
|
getMigrationVersions: getMigrationVersions
|
2014-09-10 00:06:24 -04:00
|
|
|
};
|