0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/core/server/data/migration/populate.js
Katharina Irrgang d81bc91bd2 Error creation (#7477)
refs #7116, refs #2001

- Changes the way Ghost errors are implemented to benefit from proper inheritance
- Moves all error definitions into a single file
- Changes the error constructor to take an options object, rather than needing the arguments to be passed in the correct order.
- Provides a wrapper so that any errors that haven't already been converted to GhostErrors get converted before they are displayed.

Summary of changes:

* 🐛  set NODE_ENV in config handler
*   add GhostError implementation (core/server/errors.js)
  - register all errors in one file
  - inheritance from GhostError
  - option pattern
* 🔥  remove all error files
*   wrap all errors into GhostError in case of HTTP
* 🎨  adaptions
  - option pattern for errors
  - use GhostError when needed
* 🎨  revert debug deletion and add TODO for error id's
2016-10-06 13:27:35 +01:00

56 lines
1.7 KiB
JavaScript

// # Populate
// Create a brand new database for a new install of ghost
var Promise = require('bluebird'),
_ = require('lodash'),
commands = require('../schema').commands,
fixtures = require('./fixtures'),
db = require('../../data/db'),
logging = require('../../logging'),
errors = require('../../errors'),
schema = require('../schema').tables,
schemaTables = Object.keys(schema),
populate, logger;
logger = {
info: function info(message) {
logging.info('Migrations:' + message);
},
warn: function warn(message) {
logging.warn('Skipping Migrations:' + message);
}
};
/**
* ## Populate
* Uses the schema to determine table structures, and automatically creates each table in order
*/
populate = function populate(options) {
options = options || {};
var tablesOnly = options.tablesOnly,
modelOptions = {
context: {
internal: true
}
};
logger.info('Creating tables...');
return db.knex.transaction(function populateDatabaseInTransaction(transaction) {
return Promise.mapSeries(schemaTables, function createTable(table) {
logger.info('Creating table: ' + table);
return commands.createTable(table, transaction);
}).then(function populateFixtures() {
if (tablesOnly) {
return;
}
return fixtures.populate(logger, _.merge({}, {transacting: transaction}, modelOptions));
});
}).catch(function populateDatabaseError(err) {
logger.warn('rolling back...');
return Promise.reject(new errors.GhostError({err: err, context: 'Unable to populate database!'}));
});
};
module.exports = populate;