Ghost/core/server/data/migration/populate.js
Katharina Irrgang e2e83a0f7b Migration: New database versioning (#7499)
refs #7489

- new database versioning scheme which is based upon the Ghost version, and so easier to reason about
- massive refactor of all the version related code 

Summary of changes:

*   new error: DatabaseNotSeeded
* 🎨  change versioning module
  - versioning is based on Ghost Version
* 🎨  change bootUp file
  - add big picture description
  - version error get's trigger from versioning module
* 🎨  default setting for database version is null
  - very important change: this is caused by the big picture
  - see bootUp description
  - the database version get's set by the seed script later
  - db version is by default null
  - 1. population happens (we ensure that this has finished, by checking if each table exists)   
  - 2. seeds happening (we ensure that seeds happend if database version is set to X.X)
* 🎨  temporary change for population logic
  - set database version after population happens
  - ensure population of default settings happend before
  - both: get's removed in next iteration
* 🎨  adapt tests && mark TODO's
* 🎨  err instance checking
2016-10-06 14:50:55 +01:00

69 lines
2.2 KiB
JavaScript

// # Populate
// Create a brand new database for a new install of ghost
var Promise = require('bluebird'),
_ = require('lodash'),
commands = require('../schema').commands,
versioning = require('../schema').versioning,
fixtures = require('./fixtures'),
db = require('../../data/db'),
logging = require('../../logging'),
models = require('../../models'),
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 () {
// @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);
}).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;