migrator.spec.js
7.47 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
var config = require("./config/config")
, Sequelize = require("../index")
, sequelize = new Sequelize(config.database, config.username, config.password, { logging: false })
, Helpers = new (require("./config/helpers"))(sequelize)
, Migrator = require("../lib/migrator")
, _ = Sequelize.Utils._
describe('Migrator', function() {
var migrator = null
, SequelizeMeta = null
var setup = function(_options) {
Helpers.async(function(done) {
var options = Sequelize.Utils._.extend({
path: __dirname + '/assets/migrations'
}, _options || {})
migrator = new Migrator(sequelize, options)
migrator
.findOrCreateSequelizeMetaModel({ force: true })
.success(function(_SequelizeMeta) {
SequelizeMeta = _SequelizeMeta
done()
})
.error(function(err) { console.log(err) })
})
}
var reset = function() {
migrator = null
Helpers.dropAllTables()
}
beforeEach(reset)
afterEach(reset)
describe('getUndoneMigrations', function() {
it("returns no files if timestamps are after the files timestamp", function() {
setup({ from: 20120101010101 })
Helpers.async(function(done) {
migrator.getUndoneMigrations(function(err, migrations) {
expect(err).toBeFalsy()
expect(migrations.length).toEqual(0)
done()
})
})
})
it("returns only files between from and to", function() {
setup({ from: 19700101000000, to: 20111117063700 })
Helpers.async(function(done) {
migrator.getUndoneMigrations(function(err, migrations) {
expect(err).toBeFalsy()
expect(migrations.length).toEqual(1)
expect(_.last(migrations).filename).toEqual('20111117063700-createPerson.js')
done()
})
})
})
it("returns also the file which is exactly options.from or options.to", function() {
setup({ from: 20111117063700, to: 20111123060700 })
Helpers.async(function(done) {
migrator.getUndoneMigrations(function(err, migrations) {
expect(err).toBeFalsy()
expect(migrations.length).toEqual(2)
expect(migrations[0].filename).toEqual('20111117063700-createPerson.js')
expect(migrations[1].filename).toEqual('20111123060700-addBirthdateToPerson.js')
done()
})
})
})
it("returns all files to options.to if no options.from is defined", function() {
setup({ to: 20111123060700 })
Helpers.async(function(done) {
migrator.getUndoneMigrations(function(err, migrations) {
expect(err).toBeFalsy()
expect(migrations.length).toEqual(2)
done()
})
})
})
it("returns all files from last migration id stored in database", function() {
setup()
Helpers.async(function(done) {
SequelizeMeta.create({ lastMigrationId: '20111117063700' }).success(function() {
migrator.getUndoneMigrations(function(err, migrations) {
expect(err).toBeFalsy()
expect(migrations.length).toEqual(4)
expect(migrations[0].filename).toEqual('20111123060700-addBirthdateToPerson.js')
done()
})
})
})
})
})
describe('migrations', function() {
beforeEach(function() {
setup({ from: 20111117063700, to: 20111117063700 })
Helpers.async(function(done) {
migrator.migrate().success(done).error(function(err) { console.log(err) })
})
})
describe('executions', function() {
it("executes migration #20111117063700 and correctly creates the table", function() {
Helpers.async(function(done) {
sequelize.getQueryInterface().showAllTables().success(function(tableNames) {
tableNames = tableNames.filter(function(e){ return e != 'SequelizeMeta' })
expect(tableNames.length).toEqual(1)
expect(tableNames[0]).toEqual('Person')
done()
})
})
})
it("executes migration #20111117063700 correctly up (createTable) and downwards (dropTable)", function() {
Helpers.async(function(done) {
sequelize.getQueryInterface().showAllTables().success(function(tableNames) {
tableNames = tableNames.filter(function(e){ return e != 'SequelizeMeta' })
expect(tableNames.length).toEqual(1)
done()
})
})
Helpers.async(function(done) {
migrator.migrate({ method: 'down' }).success(function() {
sequelize.getQueryInterface().showAllTables().success(function(tableNames) {
tableNames = tableNames.filter(function(e){ return e != 'SequelizeMeta' })
expect(tableNames.length).toEqual(0)
done()
}).error(function(err){ console.log(err); done() })
}).error(function(err){ console.log(err); done() })
})
})
it("executes the empty migration #20111130161100", function() {
Helpers.async(function(done) {
setup({ from: 20111130161100, to: 20111130161100})
done()
})
Helpers.async(function(done) {
migrator.migrate().success(done).error(function(err) { console.log(err) })
// this migration isn't actually testing anything but
// should not timeout
})
})
})
describe('renameTable', function() {
it("executes migration #20111205064000 and renames a table", function() {
Helpers.async(function(done) {
sequelize.getQueryInterface().showAllTables().success(function(tableNames) {
tableNames = tableNames.filter(function(e){ return e != 'SequelizeMeta' })
expect(tableNames.length).toEqual(1)
expect(tableNames[0]).toEqual('Person')
done()
})
})
setup({from: 20111205064000, to: 20111205064000})
Helpers.async(function(done) {
migrator.migrate().success(done).error(function(err) { console.log(err) })
})
Helpers.async(function(done) {
sequelize.getQueryInterface().showAllTables().success(function(tableNames) {
tableNames = tableNames.filter(function(e){ return e != 'SequelizeMeta' })
expect(tableNames.length).toEqual(1)
expect(tableNames[0]).toEqual('User')
done()
})
})
})
})
describe('addColumn', function() {
it('adds a column to the user table', function() {
setup({from: 20111205064000, to: 20111205162700})
Helpers.async(function(done) {
migrator.migrate().success(done).error(function(err) { console.log(err) })
})
Helpers.async(function(done) {
sequelize.getQueryInterface().describeTable('User').success(function(data) {
var signature = data.filter(function(hash){ return hash.Field == 'signature' })[0]
, isAdmin = data.filter(function(hash){ return hash.Field == 'isAdmin' })[0]
, shopId = data.filter(function(hash){ return hash.Field == 'shopId' })[0]
expect(signature.Field).toEqual('signature')
expect(signature.Null).toEqual('NO')
expect(isAdmin.Field).toEqual('isAdmin')
expect(isAdmin.Null).toEqual('NO')
expect(isAdmin.Default).toEqual('0')
expect(shopId.Field).toEqual('shopId')
expect(shopId.Null).toEqual('YES')
done()
}).error(function(err) {
console.log(err)
})
})
})
})
})
})