0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00
ghost/core/server/data/sephiroth/lib/utils.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

139 lines
3.7 KiB
JavaScript

var path = require('path'),
_ = require('lodash'),
fs = require('fs'),
database = require('./database'),
errors = require('./errors'),
logging = require('../../../logging');
exports.readTasks = function readTasks(absolutePath) {
var files = [],
tasks = [];
try {
files = fs.readdirSync(absolutePath);
_.each(files, function (file) {
tasks.push({
execute: require(path.join(absolutePath, file)),
name: file
});
});
return tasks;
} catch (err) {
throw new errors.SephirothError({err: err});
}
};
exports.createTransaction = function createTransaction(callback) {
return database.knex.transaction(callback);
};
/**
* each migration file get's saved into the database
* @TODO: add version
*/
exports.preTask = function preTask(options) {
options = options || {};
var localDatabase = options.transacting,
task = options.task,
type = options.type;
return (localDatabase || database.knex)('migrations')
.then(function (migrations) {
if (!migrations.length) {
return;
}
if (_.find(migrations, {name: task, type: type})) {
throw new errors.MigrationExistsError();
}
})
.catch(function (err) {
// CASE: table does not exist
if (err.errno === 1 || err.errno === 1146) {
logging.info('Creating table: migrations');
return (localDatabase || database.knex).schema.createTable('migrations', function (table) {
table.string('name');
table.string('type');
});
}
throw err;
});
};
/**
* write migration key
*/
exports.postTask = function postTask(options) {
options = options || {};
var localDatabase = options.transacting,
task = options.task,
type = options.type;
return (localDatabase || database.knex)('migrations')
.insert({
name: task,
type: type
});
};
/**
* DB health depends on the amount of executed init scripts right now
*
* @TODO:
* - alternative for checking length of init scripts?
*/
exports.isDatabaseOK = function isDatabaseOK(options) {
options = options || {};
var localDatabase = options.transacting,
initPath = exports.getPath({type: 'init'}),
dbInitTasksLength = exports.readTasks(initPath).length;
return (localDatabase || database.knex)('migrations')
.then(function (migrations) {
if (_.filter(migrations, {type: 'init'}).length === dbInitTasksLength) {
return;
}
throw new errors.DatabaseIsNotOkError({
message: 'Please run node core/server/data/sephiroth/bin/sephiroth init.',
code: 'DB_NOT_INITIALISED'
});
})
.catch(function (err) {
if (err.errno === 1 || err.errno === 1146) {
throw new errors.DatabaseIsNotOkError({
message: 'Please run node core/server/data/sephiroth/bin/sephiroth init.',
code: 'MIGRATION_TABLE_IS_MISSING'
});
}
throw new errors.SephirothError({
err: err
});
});
};
/**
* @TODO:
* - make migrationPath configureable
*/
exports.getPath = function getPath(options) {
options = options || {};
var migrationsPath = path.join(__dirname, '../../migrations');
switch (options.type) {
case 'init':
migrationsPath += '/init';
break;
}
return migrationsPath;
};