Ghost/core/server/data/schema/versioning.js
Katharina Irrgang 869a35c97d migrations: seeding is part of init db task (#7545)
* 🎨  move heart of fixtures to schema folder and change user model

- add fixtures.json to schema folder
- add fixture utils to schema folder
- keep all the logic!

--> FIXTURE.JSON
- add owner user with roles

--> USER MODEL
- add password as default
- findAll: allow querying inactive users when internal context (defaultFilters)
- findOne: do not remove values from original object!
- add: do not remove values from original object!

* 🔥  remove migrations key from default_settings.json

- this was a temporary invention for an older migration script
- sephiroth keep alls needed information in a migration collection

* 🔥   add code property to errors

- add code property to errors
- IMPORTANT: please share your opinion about that
- this is a copy paste behaviour of how node is doing that (errno, code etc.)
- so code specifies a GhostError

* 🎨  change error handling in versioning

- no need to throw specific database errors anymore (this was just a temporary solution)
- now: we are throwing real DatabaseVersionErrors
- specified by a code
- background: the versioning unit has not idea about seeding and population of the database
- it just throws what it knows --> database version does not exist or settings table does not exist

* 🎨  sephiroth optimisations

- added getPath function to get the path to init scripts and migration scripts
- migrationPath is still hardcoded (see TODO)
- tidy up database naming to transacting

*   migration init scripts are now complete

- 1. add tables
- 2. add fixtures
- 3. add default settings

* 🎨  important: make bootup script smaller!

- remove all TODO'S except of one
- no seeding logic in bootup script anymore 🕵🏻

*   sephiroth: allow params for init command

- param: skip (do not run this script)
- param: only (only run this script)
- very simple way

* 🎨  adapt tests and test env

- do not use migrate.populate anymore
- use sephiroth instead
- jscs/jshint

* 🎨  fix User model status checks
2016-10-12 16:18:57 +01:00

129 lines
3.6 KiB
JavaScript

var path = require('path'),
Promise = require('bluebird'),
db = require('../db'),
errors = require('../../errors'),
ghostVersion = require('../../utils/ghost-version');
/**
* 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.DatabaseVersionError({
code: 'VERSION_DOES_NOT_EXIST'
});
}
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;
}
/**
* If the database version is null, the database was never seeded.
* The seed migration script will set your database to current Ghost Version.
*/
function getDatabaseVersion(options) {
options = options || {};
return (options.transacting || db.knex).schema.hasTable('settings')
.then(function (exists) {
if (!exists) {
return Promise.reject(new errors.DatabaseVersionError({
code: 'SETTINGS_TABLE_DOES_NOT_EXIST'
}));
}
return (options.transacting || db.knex)('settings')
.where('key', 'databaseVersion')
.first('value')
.then(function (version) {
return validateDatabaseVersion(version ? version.value : null);
});
});
}
function getNewestDatabaseVersion() {
return ghostVersion.safe;
}
/**
* Database version cannot set from outside.
* If this function get called, we set the database version to your current Ghost version.
*/
function setDatabaseVersion(options) {
options = options || {};
return (options.transacting || db.knex)('settings')
.where('key', 'databaseVersion')
.update({
value: getNewestDatabaseVersion()
});
}
/**
* return the versions which need migration
* when on 1.1 and we update to 1.4, we expect [1.2, 1.3, 1.4]
*/
function getMigrationVersions(fromVersion, toVersion) {
var versions = [],
i;
for (i = (fromVersion * 10) + 1; i <= toVersion * 10; i += 1) {
versions.push((i / 10).toString());
}
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: '1.0',
getNewestDatabaseVersion: getNewestDatabaseVersion,
getDatabaseVersion: getDatabaseVersion,
setDatabaseVersion: setDatabaseVersion,
getMigrationVersions: getMigrationVersions,
getUpdateDatabaseTasks: getUpdateDatabaseTasks,
getUpdateFixturesTasks: getUpdateFixturesTasks
};