2014-06-01 03:53:21 +04:00
|
|
|
var _ = require('lodash'),
|
|
|
|
errors = require('../../errors'),
|
2014-05-16 06:29:42 +04:00
|
|
|
config = require('../../config'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../../i18n'),
|
2016-01-25 20:50:04 +03:00
|
|
|
defaultSettings = require('./default-settings'),
|
2014-06-01 03:53:21 +04: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 19:36:47 +04:00
|
|
|
defaultDatabaseVersion = defaultSettings.core.databaseVersion.defaultValue;
|
2014-06-01 03:53:21 +04: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() {
|
2014-07-17 18:33:21 +04:00
|
|
|
var knex = config.database.knex;
|
2014-05-16 06:29:42 +04:00
|
|
|
|
2014-06-01 03:53:21 +04:00
|
|
|
return knex.schema.hasTable('settings').then(function (exists) {
|
|
|
|
// Check for the current version from the settings table
|
|
|
|
if (exists) {
|
|
|
|
// Temporary code to deal with old databases with currentVersion settings
|
|
|
|
return knex('settings')
|
|
|
|
.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 15:29:45 +03:00
|
|
|
errors.throwError(i18n.t('errors.data.versioning.index.dbVersionNotRecognized'));
|
2014-06-01 03:53:21 +04: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 15:29:45 +03:00
|
|
|
throw new Error(i18n.t('errors.data.versioning.index.settingsTableDoesNotExist'));
|
2014-06-01 03:53:21 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setDatabaseVersion() {
|
2014-07-17 18:33:21 +04:00
|
|
|
return config.database.knex('settings')
|
2014-06-01 03:53:21 +04:00
|
|
|
.where('key', 'databaseVersion')
|
2014-09-10 08:06:24 +04:00
|
|
|
.update({value: defaultDatabaseVersion});
|
2014-06-01 03:53:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getDefaultDatabaseVersion: getDefaultDatabaseVersion,
|
|
|
|
getDatabaseVersion: getDatabaseVersion,
|
|
|
|
setDatabaseVersion: setDatabaseVersion
|
2014-09-10 08:06:24 +04:00
|
|
|
};
|