0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/server/data/schema/versioning.js
Hannah Wolfe 0ad93c3df2 Rewrite DB update to be explicit
refs #6301

- Replace builder & automated database upgrade with a set of explicit tasks
- Ensure the tasks can only happen if they need to
- Remove some duplicate code between fixture & db upgrades (more to do)
- Add tests
2016-03-17 21:17:19 +00:00

116 lines
3.6 KiB
JavaScript

var path = require('path'),
db = require('../db'),
errors = require('../../errors'),
i18n = require('../../i18n'),
defaultSettings = require('./default-settings'),
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
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 errors.rejectError(new Error(
i18n.t('errors.data.versioning.index.dbVersionNotRecognized')
));
}
return version.value;
});
}
return errors.rejectError(new Error(
i18n.t('errors.data.versioning.index.settingsTableDoesNotExist')
));
});
}
function setDatabaseVersion() {
return db.knex('settings')
.where('key', 'databaseVersion')
.update({value: 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;
}
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/'})
);
}
/**
* ### 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} logInfo
* @returns {Array}
*/
function getVersionTasks(version, relPath, logInfo) {
var tasks = [];
try {
tasks = require(path.join(relPath, version));
} catch (e) {
logInfo('No tasks found for version', version);
}
return tasks;
}
function getUpdateDatabaseTasks(version, logInfo) {
return getVersionTasks(version, '../migration/', logInfo);
}
function getUpdateFixturesTasks(version, logInfo) {
return getVersionTasks(version, '../migration/fixtures/', logInfo);
}
module.exports = {
canMigrateFromVersion: '003',
showCannotMigrateError: showCannotMigrateError,
getDefaultDatabaseVersion: getDefaultDatabaseVersion,
getDatabaseVersion: getDatabaseVersion,
setDatabaseVersion: setDatabaseVersion,
getMigrationVersions: getMigrationVersions,
getUpdateDatabaseTasks: getUpdateDatabaseTasks,
getUpdateFixturesTasks: getUpdateFixturesTasks
};