2016-03-13 23:49:30 +03:00
|
|
|
// # Populate
|
|
|
|
// Create a brand new database for a new install of ghost
|
2016-07-15 19:22:41 +03:00
|
|
|
var Promise = require('bluebird'),
|
2016-09-30 16:05:17 +03:00
|
|
|
_ = require('lodash'),
|
2016-03-13 23:49:30 +03:00
|
|
|
commands = require('../schema').commands,
|
2016-10-06 16:50:55 +03:00
|
|
|
versioning = require('../schema').versioning,
|
2016-03-13 23:49:30 +03:00
|
|
|
fixtures = require('./fixtures'),
|
2016-09-30 16:05:17 +03:00
|
|
|
db = require('../../data/db'),
|
2016-10-04 18:33:43 +03:00
|
|
|
logging = require('../../logging'),
|
2016-10-06 16:50:55 +03:00
|
|
|
models = require('../../models'),
|
2016-10-04 18:33:43 +03:00
|
|
|
errors = require('../../errors'),
|
2016-07-15 19:22:41 +03:00
|
|
|
schema = require('../schema').tables,
|
2016-03-13 23:49:30 +03:00
|
|
|
schemaTables = Object.keys(schema),
|
2016-07-15 19:22:41 +03:00
|
|
|
populate, logger;
|
|
|
|
|
|
|
|
logger = {
|
|
|
|
info: function info(message) {
|
2016-10-04 18:33:43 +03:00
|
|
|
logging.info('Migrations:' + message);
|
2016-07-15 19:22:41 +03:00
|
|
|
},
|
|
|
|
warn: function warn(message) {
|
2016-10-04 18:33:43 +03:00
|
|
|
logging.warn('Skipping Migrations:' + message);
|
2016-07-15 19:22:41 +03:00
|
|
|
}
|
|
|
|
};
|
2016-03-13 23:49:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ## Populate
|
|
|
|
* Uses the schema to determine table structures, and automatically creates each table in order
|
|
|
|
*/
|
2016-07-15 19:22:41 +03:00
|
|
|
populate = function populate(options) {
|
|
|
|
options = options || {};
|
2016-03-13 23:49:30 +03:00
|
|
|
|
2016-07-15 19:22:41 +03:00
|
|
|
var tablesOnly = options.tablesOnly,
|
|
|
|
modelOptions = {
|
|
|
|
context: {
|
|
|
|
internal: true
|
|
|
|
}
|
2016-09-30 16:05:17 +03:00
|
|
|
};
|
2016-07-15 19:22:41 +03:00
|
|
|
|
|
|
|
logger.info('Creating tables...');
|
2016-03-13 23:49:30 +03:00
|
|
|
|
2016-09-30 16:05:17 +03:00
|
|
|
return db.knex.transaction(function populateDatabaseInTransaction(transaction) {
|
|
|
|
return Promise.mapSeries(schemaTables, function createTable(table) {
|
|
|
|
logger.info('Creating table: ' + table);
|
|
|
|
return commands.createTable(table, transaction);
|
2016-10-06 16:50:55 +03:00
|
|
|
}).then(function () {
|
|
|
|
// @TODO:
|
|
|
|
// - key: migrations-kate
|
|
|
|
// - move to seed
|
|
|
|
return models.Settings.populateDefaults(_.merge({}, {transacting: transaction}, modelOptions));
|
|
|
|
}).then(function () {
|
|
|
|
// @TODO:
|
|
|
|
// - key: migrations-kate
|
|
|
|
// - move to seed
|
|
|
|
return versioning.setDatabaseVersion(transaction);
|
2016-09-30 16:05:17 +03:00
|
|
|
}).then(function populateFixtures() {
|
|
|
|
if (tablesOnly) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-13 23:49:30 +03:00
|
|
|
|
2016-09-30 16:05:17 +03:00
|
|
|
return fixtures.populate(logger, _.merge({}, {transacting: transaction}, modelOptions));
|
|
|
|
});
|
|
|
|
}).catch(function populateDatabaseError(err) {
|
|
|
|
logger.warn('rolling back...');
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.GhostError({err: err, context: 'Unable to populate database!'}));
|
2016-03-13 23:49:30 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = populate;
|