migrator.js
3.41 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var fs = require("fs")
, path = require("path")
, moment = require("moment")
, Utils = require("./utils")
, DataTypes = require("./data-types")
module.exports = (function() {
var Migrator = function(sequelize, options) {
this.sequelize = 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) {
var execMigrations = function(from) {
var nonMigratedFiles = getMigrationsFilesSince.call(self, from)
nonMigratedFiles.forEach(function(file) {
executeMigration.call(self, self.options.path + '/' + file)
})
emitter.emit('success')
}
if(self.options.from) {
execMigrations(self.options.from)
} else {
getLastMigrationId.call(self).success(function(migrationId) {
migrationId = migrationId || '19700101000000'
execMigrations.call(self, migrationId)
})
}
}).run()
}
Migrator.prototype.createTable = function() {
}
Migrator.prototype.dropTable = function() {
}
Migrator.prototype.renameTable = function() {
}
Migrator.prototype.addColumn = function() {
}
Migrator.prototype.removeColumn = function() {
}
Migrator.prototype.changeColumn = function() {
}
Migrator.prototype.renameColumn = function() {
}
Migrator.prototype.addIndex = function() {
}
Migrator.prototype.removeIndex = function() {
}
// private
var executeMigration = function(path, method) {
var migration = require(path)
migration[method || 'up'](this)
}
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)
})
}
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) {
var formattedString = getFormattedDateString(s)
, date = moment(formattedString, "YYYYMMDDHHmmss")
return date
}
var getMigrationsFilesSince = function(sinceString) {
var result = []
, since = stringToDate(sinceString)
var undoneMigrationFiles = fs.readdirSync(this.options.path).filter(function(file) {
var fileDateString = file.split("-")[0]
, fileDate = stringToDate(fileDateString)
return fileDate.diff(since) > 0
})
return undoneMigrationFiles
}
return Migrator
})()