0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00
ghost/core/server/data/schema/versioning.js
Katharina Irrgang 748c664b78 feature: maintenance mode (#7019)
closes #6976
- add maintenance mode when running migrations
- refactor update/populate migrations
2016-07-15 17:22:41 +01:00

106 lines
3.3 KiB
JavaScript

var path = require('path'),
Promise = require('bluebird'),
db = require('../db'),
errors = require('../../errors'),
i18n = require('../../i18n'),
defaultSettings = require('./default-settings'),
defaultDatabaseVersion;
// Newest 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 getNewestDatabaseVersion() {
if (!defaultDatabaseVersion) {
// This be the current version according to the software
defaultDatabaseVersion = defaultSettings.core.databaseVersion.defaultValue;
}
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() {
return db.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 db.knex('settings')
.where('key', 'databaseVersion')
.first('value')
.then(function (version) {
if (!version || isNaN(version.value)) {
return Promise.reject(new errors.DatabaseVersion(i18n.t('errors.data.versioning.index.dbVersionNotRecognized')));
}
return version.value;
});
}
return Promise.reject(new errors.DatabaseNotPopulated(i18n.t('errors.data.versioning.index.databaseNotPopulated')));
});
}
function setDatabaseVersion(transaction, version) {
return (transaction || db.knex)('settings')
.where('key', 'databaseVersion')
.update({value: version || defaultDatabaseVersion});
}
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;
}
/**
* ### 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
* @param {Function} logger
* @returns {Array}
*/
function getVersionTasks(version, relPath, logger) {
var tasks = [];
try {
tasks = require(path.join(relPath, version));
} catch (e) {
logger.info('No tasks found for version', version);
}
return tasks;
}
function getUpdateDatabaseTasks(version, logger) {
return getVersionTasks(version, '../migration/', logger);
}
function getUpdateFixturesTasks(version, logger) {
return getVersionTasks(version, '../migration/fixtures/', logger);
}
module.exports = {
canMigrateFromVersion: '003',
getNewestDatabaseVersion: getNewestDatabaseVersion,
getDatabaseVersion: getDatabaseVersion,
setDatabaseVersion: setDatabaseVersion,
getMigrationVersions: getMigrationVersions,
getUpdateDatabaseTasks: getUpdateDatabaseTasks,
getUpdateFixturesTasks: getUpdateFixturesTasks
};