-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests_migrations.coffee
278 lines (233 loc) · 8.02 KB
/
tests_migrations.coffee
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
class MigrationTest extends Document
# Other fields:
# test (which is later on renamed to "renamed")
@Meta
name: 'MigrationTest'
class Migration1 extends Document.PatchMigration
name: "Migration 1"
MigrationTest.addMigration new Migration1()
class Migration2 extends Document.PatchMigration
name: "Migration 2"
MigrationTest.addMigration new Migration2()
MigrationTest.renameCollectionMigration 'OlderMigrationTests', 'OldMigrationTests'
class Migration3 extends Document.MinorMigration
name: "Migration 3"
MigrationTest.addMigration new Migration3()
class Migration4 extends Document.MajorMigration
name: "Migration 4"
forward: (document, collection, currentSchema, newSchema) ->
count = collection.update {_schema: currentSchema}, {$rename: {test: 'renamed'}, $set: {_schema: newSchema}}, {multi: true}
counts = super document, collection, currentSchema, newSchema
counts.migrated += count
counts.all += count
counts
backward: (document, collection, currentSchema, oldSchema) ->
count = collection.update {_schema: currentSchema}, {$rename: {renamed: 'test'}, $set: {_schema: oldSchema}}, {multi: true}
counts = super document, collection, currentSchema, oldSchema
counts.migrated += count
counts.all += count
counts
MigrationTest.addMigration new Migration4()
MigrationTest.renameCollectionMigration 'OldMigrationTests', 'MigrationTests'
class Migration5 extends Document.MajorMigration
name: "Migration 5"
MigrationTest.addMigration new Migration5()
class Migration6 extends Document.MinorMigration
name: "Migration 6"
MigrationTest.addMigration new Migration6()
class Migration7 extends Document.MinorMigration
name: "Migration 7"
MigrationTest.addMigration new Migration7()
class Migration8 extends Document.PatchMigration
name: "Migration 8"
MigrationTest.addMigration new Migration8()
# Store away for testing.
_TestMigrationTest = MigrationTest
class MigrationTest extends MigrationTest
@Meta
name: 'MigrationTest'
replaceParent: true
if Meteor.isServer
# Initialize the database.
MigrationTest.documents.remove {}
testDefinition = (test, count) ->
test.equal MigrationTest.Meta._name, 'MigrationTest'
test.equal MigrationTest.Meta.parent, _TestMigrationTest.Meta
test.equal MigrationTest.Meta.document, MigrationTest
test.equal MigrationTest.Meta.collection._name, 'MigrationTests'
test.equal MigrationTest.Meta.schema, '5.2.1'
test.equal _.pluck(MigrationTest.Meta.migrations, 'name'), ["Migration 1", "Migration 2", "Renaming collection from 'OlderMigrationTests' to 'OldMigrationTests'", "Migration 3", "Migration 4", "Renaming collection from 'OldMigrationTests' to 'MigrationTests'", "Migration 5", "Migration 6", "Migration 7", "Migration 8"]
test.equal _.size(MigrationTest.Meta.fields), 0
migrations = Document.Migrations.find().fetch()
migrations = for migration in migrations
delete migration._id
delete migration.timestamp
migration
test.equal migrations, [
serial: 1
migrationName: null
oldCollectionName: null
newCollectionName: null
oldVersion: null
newVersion: null
migrated: 0
all: 0
,
serial: 2
migrationName: 'Migration 1'
oldCollectionName: 'OlderMigrationTests'
newCollectionName: 'OlderMigrationTests'
oldVersion: '1.0.0'
newVersion: '1.0.1'
migrated: 0
all: count
,
serial: 3
migrationName: 'Migration 2'
oldCollectionName: 'OlderMigrationTests'
newCollectionName: 'OlderMigrationTests'
oldVersion: '1.0.1'
newVersion: '1.0.2'
migrated: 0
all: count
,
serial: 4
migrationName: 'Renaming collection from \'OlderMigrationTests\' to \'OldMigrationTests\''
oldCollectionName: 'OlderMigrationTests'
newCollectionName: 'OldMigrationTests'
oldVersion: '1.0.2'
newVersion: '2.0.0'
migrated: count
all: count
,
serial: 5
migrationName: 'Migration 3'
oldCollectionName: 'OldMigrationTests'
newCollectionName: 'OldMigrationTests'
oldVersion: '2.0.0'
newVersion: '2.1.0'
migrated: 0
all: count
,
serial: 6
migrationName: 'Migration 4'
oldCollectionName: 'OldMigrationTests'
newCollectionName: 'OldMigrationTests'
oldVersion: '2.1.0'
newVersion: '3.0.0'
migrated: count
all: count
,
serial: 7
migrationName: 'Renaming collection from \'OldMigrationTests\' to \'MigrationTests\''
oldCollectionName: 'OldMigrationTests'
newCollectionName: 'MigrationTests'
oldVersion: '3.0.0'
newVersion: '4.0.0'
migrated: count
all: count
,
serial: 8
migrationName: 'Migration 5'
oldCollectionName: 'MigrationTests'
newCollectionName: 'MigrationTests'
oldVersion: '4.0.0'
newVersion: '5.0.0'
migrated: 0
all: count
,
serial: 9
migrationName: 'Migration 6'
oldCollectionName: 'MigrationTests'
newCollectionName: 'MigrationTests'
oldVersion: '5.0.0'
newVersion: '5.1.0'
migrated: 0
all: count
,
serial: 10
migrationName: 'Migration 7'
oldCollectionName: 'MigrationTests'
newCollectionName: 'MigrationTests'
oldVersion: '5.1.0'
newVersion: '5.2.0'
migrated: 0
all: count
,
serial: 11
migrationName: 'Migration 8'
oldCollectionName: 'MigrationTests'
newCollectionName: 'MigrationTests'
oldVersion: '5.2.0'
newVersion: '5.2.1'
migrated: 0
all: count
]
unless Document.migrationsDisabled
testAsyncMulti 'peerdb-migrations - migrations', [
(test, expect) ->
db = MongoInternals.defaultRemoteCollectionDriver().mongo.db
test.isTrue db
# Reset migrated count.
Document.Migrations.update({}, {$set: {migrated: 0, all: 0}}, {multi: true})
# We ignore any error.
db.dropCollection 'OlderMigrationTests', Meteor.bindEnvironment expect(->), (e) -> throw e
db.dropCollection 'OldMigrationTests', Meteor.bindEnvironment expect(->), (e) -> throw e
db.dropCollection 'MigrationTests', Meteor.bindEnvironment expect(->), (e) -> throw e
testDefinition test, 0
# We should be able to call migrate multiple times.
MigrationTest.migrateForward(MigrationTest.Meta.migrations[MigrationTest.Meta.migrations.length - 1])
testDefinition test, 0
# We ignore any error.
db.dropCollection 'OlderMigrationTests', Meteor.bindEnvironment expect(->), (e) -> throw e
db.dropCollection 'OldMigrationTests', Meteor.bindEnvironment expect(->), (e) -> throw e
db.dropCollection 'MigrationTests', Meteor.bindEnvironment expect(->), (e) -> throw e
,
(test, expect) ->
Document.Migrations.remove serial: $ne: 1
collection = new DirectCollection 'OlderMigrationTests'
id = Random.id()
collection.insert {_id: id, _schema: '1.0.0', test: 'test field'}
MigrationTest.migrateForward(MigrationTest.Meta.migrations[MigrationTest.Meta.migrations.length - 1])
docs = MigrationTest.documents.find({},
# So that we can use test.equal.
transform: null
).fetch()
test.equal docs,
[
_id: id
_schema: '5.2.1'
renamed: 'test field'
]
testDefinition test, 1
# We should be able to call migrate multiple times.
MigrationTest.migrateForward(MigrationTest.Meta.migrations[MigrationTest.Meta.migrations.length - 1])
testDefinition test, 1
docs = MigrationTest.documents.find({},
# So that we can use test.equal.
transform: null
).fetch()
test.equal docs,
[
_id: id
_schema: '5.2.1'
renamed: 'test field'
]
# Insert without schema.
MigrationTest.documents.insert {renamed: 'another field'}
# Wait for observer to set the schema field.
Meteor.setTimeout expect(), 200 # ms
,
(test, expect) ->
docs = MigrationTest.documents.find({renamed: 'another field'},
# So that we can use test.equal.
transform: null,
fields:
_id: 0
).fetch()
test.equal docs,
[
_schema: '5.2.1'
renamed: 'another field'
]
]