migrator.js
2.92 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
const fs = require("fs")
, path = require("path")
, moment = require("moment")
var Utils = require("./utils")
, DataTypes = require("./data-types")
, QueryInterface = require("./query-interface")
module.exports = (function() {
var Migrator = function(sequelize, options) {
this.sequelize = sequelize
this.queryInterface = new QueryInterface(this.sequelize)
this.options = Utils._.extend({
path: __dirname + '/../migrations',
from: null,
to: null
}, options || {})
}
Migrator.prototype.migrate = function() {
var self = this
return new Utils.CustomEventEmitter(function(emitter) {
self.getUndoneMigrations(function(migrations) {
emitter.emit('success')
console.log(migrations)
})
}).run()
}
Migrator.prototype.getUndoneMigrations = function(callback) {
var undoneMigrations = fs.readdirSync(this.options.path)
if(this.options.from) {
var fromDate = stringToDate(this.options.from.toString())
undoneMigrations = undoneMigrations.filter(function(file) {
var fileDate = stringToDate(file.split("-")[0])
return (fileDate >= fromDate)
})
}
if(this.options.to) {
var toDate = stringToDate(this.options.to.toString())
undoneMigrations = undoneMigrations.filter(function(file) {
var fileDate = stringToDate(file.split("-")[0])
return (fileDate <= toDate)
})
}
callback && callback(undoneMigrations)
}
// private
var executeMigration = function(path, method) {
var migration = require(path)
migration[method || 'up'](this.queryInterface, DataTypes)
}
var getLastMigrationId = function() {
var self = this
return new Utils.CustomEventEmitter(function(emitter) {
var SequelizeMeta = self.sequelize.modelManager.getModel('SequelizeMeta')
var findLastMigrationId = function() {
SequelizeMeta.find({ order: 'id DESC' }).success(function(sequelizeMeta) {
emitter.emit('success', sequelizeMeta ? sequelizeMeta.lastMigrationId : null)
}).error(function(err) { emitter.emit('failure', err) })
}
if(SequelizeMeta) {
findLastMigrationId()
} else {
SequelizeMeta = self.sequelize.define('SequelizeMeta', {
lastMigrationId: DataTypes.STRING
})
SequelizeMeta
.sync()
.success(function() { findLastMigrationId() })
.error(function(err) { emitter.emit('failure', err) })
}
}).run()
}
var getFormattedDateString = function(s) {
var result = null
try {
result = s.match(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/).slice(1, 6).join('-')
} catch(e) {
throw new Error(s + ' is no valid migration timestamp format! Use YYYYMMDDHHmmss!')
}
return result
}
var stringToDate = function(s) {
return moment(getFormattedDateString(s), "YYYYMMDDHHmmss")
}
return Migrator
})()