Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Commit

Permalink
adding preDrop before tables drops and postBuild after tables sync
Browse files Browse the repository at this point in the history
  • Loading branch information
goldo committed Dec 22, 2015
1 parent ac8e660 commit 2cb4d4b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 10 additions & 3 deletions lib/drop.js
@@ -1,6 +1,7 @@
'use strict';

var Promise = require('bluebird');
var _ = require('lodash');
var Resolver = require('./resolver');

/**
Expand All @@ -10,16 +11,22 @@ var Resolver = require('./resolver');
module.exports = drop;

/**
* Drop schemas tables.
* Drop schemas tables. If it exists, it calls `preDrop` beforehand
*
* @param {[Schemas]} schemas
* @return {Promise}
*/

function drop(schemas) {
var resolver = new Resolver(schemas);
// Reduce force sequential execution.
return Promise.reduce(resolver.resolve().reverse(), dropSchema.bind(this), []);
var knex = this.knex;
return Promise.map(schemas || [], function(schema) {
return (schema.preDrop || _.noop)(knex);
})
.then(function () {
// Reduce force sequential execution.
return Promise.reduce(resolver.resolve().reverse(), dropSchema.bind(this), []);
}.bind(this));
}

/**
Expand Down
10 changes: 8 additions & 2 deletions lib/sync.js
Expand Up @@ -11,7 +11,7 @@ var Resolver = require('./resolver');
module.exports = sync;

/**
* Synchronize given schemas with database.
* Synchronize given schemas with database. If it exists, it calls `postBuild` afterwards
*
* @param {[Schemas]} schemas
* @return {Promise}
Expand All @@ -20,7 +20,13 @@ module.exports = sync;
function sync(schemas) {
var resolver = new Resolver(schemas);
// Reduce force sequential execution.
return Promise.reduce(resolver.resolve(), syncSchema.bind(this), []);
return Promise.resolve(Promise.reduce(resolver.resolve(), syncSchema.bind(this), []))
.tap(function () {
var knex = this.knex;
return Promise.map(schemas || [], function(schema) {
return (schema.postBuild || _.noop)(knex);
});
}.bind(this));
}

/**
Expand Down

0 comments on commit 2cb4d4b

Please sign in to comment.