Ghost/core/server/data/migration/fixtures/populate.js
Katharina Irrgang 9fad7f1d69 use migration runner for init db (#7502)
refs #7489

* 🎨  protect error when creating owner
* 🎨  reset migration table
- temporary solution, see TODO's
* 🎨  use sephiroth in bootUp script
- do not populate the database
- ask sephiroth for database state
- do seeding manually (this will be removed in next seeding PR)
* 🎨  rewrite createTableIfNotExists because it causes error when running twice
- see knex issue
- hasTable and createTable
- indexes can cause trouble when calling them twice
* 🎨  tests: populate db in test env
- when forking db
- when starting ghost()
- this basically affects only the functional tests
* 🎨  server spec test adaption
- we now throw an error when database is not populated, instead of populating the database
* 🎨   migration spec adaption
- reset database now deletes migration table
- we will move the reset script into sephiroth and then we make it pretty
* 🎨  error creation adaption in bootUp
* 🎨  fixes
- sephiroth error handling
- fix tests
2016-10-11 13:37:11 +01:00

99 lines
2.9 KiB
JavaScript

// # Populate Fixtures
// This module handles populating fixtures on a fresh install.
// This is done automatically, by reading the fixtures.json file
// All models, and relationships inside the file are then setup.
var Promise = require('bluebird'),
models = require('../../../models'),
coreUtils = require('../../../utils'),
fixtureUtils = require('./utils'),
fixtures = require('./fixtures'),
// private
addAllModels,
addAllRelations,
createOwner,
// public
populate;
/**
* ### Add All Models
* Sequentially calls add on all the models specified in fixtures.json
*
* @returns {Promise<*>}
*/
addAllModels = function addAllModels(modelOptions) {
return Promise.mapSeries(fixtures.models, function (model) {
return fixtureUtils.addFixturesForModel(model, modelOptions);
});
};
/**
* ### Add All Relations
* Sequentially calls add on all the relations specified in fixtures.json
*
* @returns {Promise|Array}
*/
addAllRelations = function addAllRelations(modelOptions) {
return Promise.mapSeries(fixtures.relations, function (model) {
return fixtureUtils.addFixturesForRelation(model, modelOptions);
});
};
/**
* ### Create Owner
* Creates the user fixture and gives it the owner role.
* By default, users are given the Author role, making it hard to do this using the fixture system
*
* @param {{info: logger.info, warn: logger.warn}} logger
* @returns {Promise<*>}
*/
createOwner = function createOwner(logger, modelOptions) {
var user = {
name: 'Ghost Owner',
email: 'ghost@ghost.org',
status: 'inactive',
password: coreUtils.uid(50)
};
return models.Role.findOne({name: 'Owner'}, modelOptions).then(function (ownerRole) {
if (ownerRole) {
user.roles = [ownerRole.id];
return models.User
.findOne({name: 'Ghost Owner', status: 'all'}, modelOptions)
.then(function (exists) {
if (exists) {
logger.warn('Skipping: Creating owner');
return;
}
logger.info('Creating owner');
return models.User.add(user, modelOptions);
});
}
});
};
/**
* ## Populate
* Sequentially creates all models, in the order they are specified, and then
* creates all the relationships, also maintaining order.
*
* @param {{info: logger.info, warn: logger.warn}} logger
* @returns {Promise<*>}
*/
populate = function populate(logger, modelOptions) {
logger.info('Running fixture populations');
return addAllModels(modelOptions)
.then(function () {
return addAllRelations(modelOptions);
})
.then(function () {
return createOwner(logger, modelOptions);
});
};
module.exports = populate;