migration.js 2.9 KB

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