Code coverage report for upstream/lib/migration.js

Statements: 34.21% (13 / 38)      Branches: 22.22% (4 / 18)      Functions: 18.18% (2 / 11)      Lines: 34.21% (13 / 38)     

All files » upstream/lib/ » migration.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 951           1 1                       1 50 42 42                     1                                 1           1                               1                   1                   1    
var moment         = require("moment")
  , path           = require("path")
  , Utils          = require("./utils")
  , DataTypes      = require("./data-types")
  , QueryInterface = require("./query-interface")
 
module.exports = (function() {
  var Migration = function(migrator, p) {
    this.migrator       = migrator
    this.path           = path.normalize(p)
    this.filename       = Utils._.last(this.path.split(path.sep))
 
    var parsed          = Migration.parseFilename(this.filename)
 
    this.migrationId    = parsed.id
    this.date           = parsed.date;
    this.queryInterface = this.migrator.queryInterface
  }
 
  for (var methodName in QueryInterface.prototype) {
    if (QueryInterface.prototype.hasOwnProperty(methodName) && (typeof QueryInterface.prototype[methodName]) === 'function') {
      (function(methodName) {
        Migration.prototype[methodName] = function() {
          return this.queryInterface[methodName].apply(this.queryInterface, arguments)
        }
      })(methodName)
    }
  }
 
  ///////////////
  // static /////
  ///////////////
 
  Migration.parseFilename = function(s) {
    var matches = s.match(/^((\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2}))[-_].+/)
 
    if (matches === null) {
      throw new Error(s + ' is not a valid migration name! Use YYYYMMDDHHmmss-migration-name format.')
    }
 
    return {
      id: parseInt(matches[1], 10),
      date: moment(matches.slice(2, 8).join('-'), 'YYYYMMDDHHmmss')
    }
  }
 
  ///////////////
  // member /////
  ///////////////
 
  Object.defineProperty(Migration.prototype, 'migration', {
    get: function() {
      return require(this.path)
    }
  })
 
  Migration.prototype.execute = function(options) {
    return new Utils.CustomEventEmitter(function(emitter) {
      options = Utils._.extend({
        method: 'up'
      }, options || {})
 
      this.migration[options.method].call(null, this, DataTypes, function(err) {
        if (err) {
          emitter.emit('error', err)
        } else {
          emitter.emit('success', null)
        }
      })
    }.bind(this)).run()
  }
 
  Migration.prototype.isBefore = function(dateString, options) {
    options = Utils._.extend({
      withoutEquals: false
    }, options || {})
 
    var date = Migration.parseFilename(dateString.toString() + '-foo.js').date
 
    return options.withoutEqual ? (date > this.date) : (date >= this.date)
  }
 
  Migration.prototype.isAfter = function(dateString, options) {
    options = Utils._.extend({
      withoutEquals: false
    }, options || {})
 
    var date = Migration.parseFilename(dateString.toString() + '-foo.js').date
 
    return options.withoutEqual ? (date < this.date) : (date <= this.date)
  }
 
  return Migration
})()