Add basic function and trigger migrations

Added the ability to define migrations that create, drop, and rename
both functions and triggers (since triggers need functions).  This
allows migrations to handle more of the requirements of setting up
the underlying SQL database behavior.

This version currently only provides support on postgresql.
Merge remote-tracking branch 'upstream/master' into basic_function_and_trigger_migrations
Custom event emitter uses setImmediate over process.nextTick if supported. Closes https://github.com/sequelize/sequelize/issues/869
Forgot to move the tick variable to the top of the file in custom event emitters.
Initial commit for hooks.. need to stash this for another PR
Adds hooks / callbacks / lifecycle events

This commit adds the ability of hooks for the DAOFactory. The following
hooks (in their order of operations) are:

(1) beforeValidate(dao, fn)

(-) validate

(2) afterValidate(dao, fn)

(3) beforeBulkCreate(daos, fields, fn)
    beforeBulkDestroy(daos, fields, fn)
    beforeBulkUpdate(daos, fields, fn)

(4) beforeCreate(dao, fn)
    beforeDestroy(dao, fn)
    beforeUpdate(dao, fn)

(-) create / destroy / update

(5) afterCreate(dao, fn)
    aftreDestroy(dao, fn)
    afterUpdate(dao, fn)

(6) afterBulkCreate(daos, fields, fn)
    afterBulkDestory(daos, fields, fn)
    afterBulkUpdate(daos, fields, fn)

There's a new file called hooks.js which works very similar to
mixins.js which just extends a prototype.

You can add the hooks like so...

... via .define():

var User = sequelize.define('User', {
  username: DataTypes.STRING,
  mood: {
    type: DataTypes.ENUM,
    values: ['happy', 'sad', 'neutral']
  }
}, {
  hooks: {
    beforeValidate: function(user, fn) {
      user.mood = 'happy'
      fn(null, user)
    },
    afterValidate: function(user, fn) {
      user.username = 'Toni'
      fn(null, user)
    }
  }
})

... via .hook() method

var User = sequelize.define('User', {
  username: DataTypes.STRING,
  mood: {
    type: DataTypes.ENUM,
    values: ['happy', 'sad', 'neutral']
  }
})

User.hook('beforeValidate', function(user, fn) {
  user.mood = 'happy'
  fn(null, user)
})

User.hook('afterValidate', function(user, fn) {
  user.username = 'Toni'
  fn(null, user)
})

... via direct method:

var User = sequelize.define('User', {
  username: DataTypes.STRING,
  mood: {
    type: DataTypes.ENUM,
    values: ['happy', 'sad', 'neutral']
  }
})

User.beforeValidate(function(user, fn) {
  user.mood = 'happy'
  fn(null, user)
})

User.afterValidate(function(user, fn) {
  user.username = 'Toni'
  fn(null, user)
})

Quick example:

User.beforeCreate(function(user, fn) {
  if (user.accessLevel > 10 && user.username !== "Boss") {
    return fn("You can't grant this user that level!")
  }

  return fn()
})

User.create({
  username: 'Not a Boss',
  accessLevel: 20
}).error(function(err) {
  console.log(err) // You can't grant this user that level!
})

As of right now, each hook will process in the order they where
implemented / added to the factory.

To invoke the hooks simply run...

Model.runHooks.call(Model.options.hooks.<hook>, <args>, <callback>)

Some model hooks have two or three paramters sent to each hook
depending on it's type.

Model.beforeBulkCreate(function(records, fields, fn) {
  // records = the first argument sent to .bulkCreate
  // fields = the second argument sent to .bulkCreate
})

Model.bulkCreate([
  {username: 'Toni'}, // part of records argument
  {username: 'Tobi'} // part of records argument
], ['username'] /* part of fields argument */)

Model.beforeBulkUpdate(function(attributes, where, fn) {
  // attributes = first argument sent to Model.update
  // where = second argument sent to Model.update
})

Model.update(
  {gender: 'Male'} /*attribures argument*/,
  {username: 'Tom'} /*where argument*/
)

Model.beforeBulkDestroy(function(whereClause, fn) {
  // whereClause = first argument sent to Model.destroy
})

Model.destroy({username: 'Tom'} /*whereClause argument*/)

For 1.7.x backwards compatibility, I've added a new method called
.hookValidate() since .validate() is a synchronous function. All of
Sequelize's API functions will invoke .hookValidate(), but if you
utilize the .validate() function outside of Sequelize then you'll
need to update your code if you want to run before/afterValidate hooks.

Sequelzie 2.0.x will not need this change simply because it's
.validate() method is already asynchronous. However, it will have the
.hookValdate() function in order to make the transition from 1.7 to 2.0
smoother and easier. Eventually we'll want to deprecate this function.

In addition to this commit, I've also completed the following tasks:

Move validation of enum attribute value to validate method

I had to complete that task in order to get the validate hooks
working properly.

And the last thing, I fixed executables.test.js if your DB didn't use
the default values for config/config.js, this was causing errors for me
on my local machine.
Merged master and forgot to Object.keys() for skipping validations.
Fixed a small hooks undefined problem.
Unit tests for function and trigger migrations
pulled in upstream master changes to simplify merge/pull request
Merge branch 'master' of https://github.com/sequelize/sequelize into basic_function_and_trigger_migrations
Updated number of undone migrations for undone migration test
Support for setting columns with the value of another column or a function
reset travis
reset travis
Use function as default value
Create uuid extension if not exists
node-mysql options
Fixed failing tests, tried to match style guidelines
Merge pull request #928 from janmeier/updateWithColumn

Update with column