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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from nose.tools import assert_raises
from pymongo import Connection
from mediagoblin.tests.tools import (
install_fixtures_simple, assert_db_meets_expected)
from mediagoblin.db.util import (
RegisterMigration, MigrationManager, ObjectId,
MissingCurrentMigration)
from mediagoblin.db.migrations import add_table_field
# This one will get filled with local migrations
TEST_MIGRATION_REGISTRY = {}
# this one won't get filled
TEST_EMPTY_MIGRATION_REGISTRY = {}
MIGRATION_DB_NAME = u'__mediagoblin_test_migrations__'
######################
# Fake test migrations
######################
@RegisterMigration(1, TEST_MIGRATION_REGISTRY)
def creature_add_magical_powers(database):
"""
Add lists of magical powers.
This defaults to [], an empty list. Since we haven't declared any
magical powers, all existing monsters, setting to an empty list is
fine.
"""
add_table_field(database, 'creatures', 'magical_powers', [])
@RegisterMigration(2, TEST_MIGRATION_REGISTRY)
def creature_rename_num_legs_to_num_limbs(database):
"""
It turns out we want to track how many limbs a creature has, not
just how many legs. We don't care about the ambiguous distinction
between arms/legs currently.
"""
# $rename not available till 1.7.2+, Debian Stable only includes
# 1.4.4... we should do renames manually for now :(
collection = database['creatures']
target = collection.find(
{'num_legs': {'$exists': True}})
for document in target:
# A lame manual renaming.
document['num_limbs'] = document.pop('num_legs')
collection.save(document)
@RegisterMigration(3, TEST_MIGRATION_REGISTRY)
def creature_remove_is_demon(database):
"""
It turns out we don't care much about whether creatures are demons
or not.
"""
database['creatures'].update(
{'is_demon': {'$exists': True}},
{'$unset': {'is_demon': 1}},
multi=True)
@RegisterMigration(4, TEST_MIGRATION_REGISTRY)
def level_exits_dict_to_list(database):
"""
For the sake of the indexes we want to write, and because we
intend to add more flexible fields, we want to move level exits
from like:
{'big_door': 'castle_level_id',
'trapdoor': 'dungeon_level_id'}
to like:
[{'name': 'big_door',
'exits_to': 'castle_level_id'},
{'name': 'trapdoor',
'exits_to': 'dungeon_level_id'}]
"""
collection = database['levels']
target = collection.find(
{'exits': {'$type': 3}})
for level in target:
new_exits = []
for exit_name, exits_to in level['exits'].items():
new_exits.append(
{'name': exit_name,
'exits_to': exits_to})
level['exits'] = new_exits
collection.save(level)
CENTIPEDE_OBJECTID = ObjectId()
WOLF_OBJECTID = ObjectId()
WIZARDSNAKE_OBJECTID = ObjectId()
UNMIGRATED_DBDATA = {
'creatures': [
{'_id': CENTIPEDE_OBJECTID,
'name': 'centipede',
'num_legs': 100,
'is_demon': False},
{'_id': WOLF_OBJECTID,
'name': 'wolf',
'num_legs': 4,
'is_demon': False},
# don't ask me what a wizardsnake is.
{'_id': WIZARDSNAKE_OBJECTID,
'name': 'wizardsnake',
'num_legs': 0,
'is_demon': True}],
'levels': [
{'_id': 'necroplex',
'name': 'The Necroplex',
'description': 'A complex full of pure deathzone.',
'exits': {
'deathwell': 'evilstorm',
'portal': 'central_park'}},
{'_id': 'evilstorm',
'name': 'Evil Storm',
'description': 'A storm full of pure evil.',
'exits': {}}, # you can't escape the evilstorm
{'_id': 'central_park',
'name': 'Central Park, NY, NY',
'description': "New York's friendly Central Park.",
'exits': {
'portal': 'necroplex'}}]}
EXPECTED_POST_MIGRATION_UNMIGRATED_DBDATA = {
'creatures': [
{'_id': CENTIPEDE_OBJECTID,
'name': 'centipede',
'num_limbs': 100,
'magical_powers': []},
{'_id': WOLF_OBJECTID,
'name': 'wolf',
'num_limbs': 4,
# kept around namely to check that it *isn't* removed!
'magical_powers': []},
{'_id': WIZARDSNAKE_OBJECTID,
'name': 'wizardsnake',
'num_limbs': 0,
'magical_powers': []}],
'levels': [
{'_id': 'necroplex',
'name': 'The Necroplex',
'description': 'A complex full of pure deathzone.',
'exits': [
{'name': 'deathwell',
'exits_to': 'evilstorm'},
{'name': 'portal',
'exits_to': 'central_park'}]},
{'_id': 'evilstorm',
'name': 'Evil Storm',
'description': 'A storm full of pure evil.',
'exits': []}, # you can't escape the evilstorm
{'_id': 'central_park',
'name': 'Central Park, NY, NY',
'description': "New York's friendly Central Park.",
'exits': [
{'name': 'portal',
'exits_to': 'necroplex'}]}]}
# We want to make sure that if we're at migration 3, migration 3
# doesn't get re-run.
SEMI_MIGRATED_DBDATA = {
'creatures': [
{'_id': CENTIPEDE_OBJECTID,
'name': 'centipede',
'num_limbs': 100,
'magical_powers': []},
{'_id': WOLF_OBJECTID,
'name': 'wolf',
'num_limbs': 4,
# kept around namely to check that it *isn't* removed!
'is_demon': False,
'magical_powers': [
'ice_breath', 'death_stare']},
{'_id': WIZARDSNAKE_OBJECTID,
'name': 'wizardsnake',
'num_limbs': 0,
'magical_powers': [
'death_rattle', 'sneaky_stare',
'slithery_smoke', 'treacherous_tremors'],
'is_demon': True}],
'levels': [
{'_id': 'necroplex',
'name': 'The Necroplex',
'description': 'A complex full of pure deathzone.',
'exits': {
'deathwell': 'evilstorm',
'portal': 'central_park'}},
{'_id': 'evilstorm',
'name': 'Evil Storm',
'description': 'A storm full of pure evil.',
'exits': {}}, # you can't escape the evilstorm
{'_id': 'central_park',
'name': 'Central Park, NY, NY',
'description': "New York's friendly Central Park.",
'exits': {
'portal': 'necroplex'}}]}
EXPECTED_POST_MIGRATION_SEMI_MIGRATED_DBDATA = {
'creatures': [
{'_id': CENTIPEDE_OBJECTID,
'name': 'centipede',
'num_limbs': 100,
'magical_powers': []},
{'_id': WOLF_OBJECTID,
'name': 'wolf',
'num_limbs': 4,
# kept around namely to check that it *isn't* removed!
'is_demon': False,
'magical_powers': [
'ice_breath', 'death_stare']},
{'_id': WIZARDSNAKE_OBJECTID,
'name': 'wizardsnake',
'num_limbs': 0,
'magical_powers': [
'death_rattle', 'sneaky_stare',
'slithery_smoke', 'treacherous_tremors'],
'is_demon': True}],
'levels': [
{'_id': 'necroplex',
'name': 'The Necroplex',
'description': 'A complex full of pure deathzone.',
'exits': [
{'name': 'deathwell',
'exits_to': 'evilstorm'},
{'name': 'portal',
'exits_to': 'central_park'}]},
{'_id': 'evilstorm',
'name': 'Evil Storm',
'description': 'A storm full of pure evil.',
'exits': []}, # you can't escape the evilstorm
{'_id': 'central_park',
'name': 'Central Park, NY, NY',
'description': "New York's friendly Central Park.",
'exits': [
{'name': 'portal',
'exits_to': 'necroplex'}]}]}
class TestMigrations(object):
def setUp(self):
# Set up the connection, drop an existing possible database
self.connection = Connection()
self.connection.drop_database(MIGRATION_DB_NAME)
self.db = Connection()[MIGRATION_DB_NAME]
self.migration_manager = MigrationManager(
self.db, TEST_MIGRATION_REGISTRY)
self.empty_migration_manager = MigrationManager(
self.db, TEST_EMPTY_MIGRATION_REGISTRY)
self.run_migrations = []
def tearDown(self):
self.connection.drop_database(MIGRATION_DB_NAME)
def _record_migration(self, migration_number, migration_func):
self.run_migrations.append((migration_number, migration_func))
def test_migrations_registered_and_sorted(self):
"""
Make sure that migrations get registered and are sorted right
in the migration manager
"""
assert TEST_MIGRATION_REGISTRY == {
1: creature_add_magical_powers,
2: creature_rename_num_legs_to_num_limbs,
3: creature_remove_is_demon,
4: level_exits_dict_to_list}
assert self.migration_manager.sorted_migrations == [
(1, creature_add_magical_powers),
(2, creature_rename_num_legs_to_num_limbs),
(3, creature_remove_is_demon),
(4, level_exits_dict_to_list)]
assert self.empty_migration_manager.sorted_migrations == []
def test_run_full_migrations(self):
"""
Make sure that running the full migration suite from 0 updates
everything
"""
self.migration_manager.set_current_migration(0)
assert self.migration_manager.database_current_migration() == 0
install_fixtures_simple(self.db, UNMIGRATED_DBDATA)
self.migration_manager.migrate_new(post_callback=self._record_migration)
assert self.run_migrations == [
(1, creature_add_magical_powers),
(2, creature_rename_num_legs_to_num_limbs),
(3, creature_remove_is_demon),
(4, level_exits_dict_to_list)]
assert_db_meets_expected(
self.db, EXPECTED_POST_MIGRATION_UNMIGRATED_DBDATA)
# Make sure the migration is recorded correctly
assert self.migration_manager.database_current_migration() == 4
# run twice! It should do nothing the second time.
# ------------------------------------------------
self.run_migrations = []
self.migration_manager.migrate_new(post_callback=self._record_migration)
assert self.run_migrations == []
assert_db_meets_expected(
self.db, EXPECTED_POST_MIGRATION_UNMIGRATED_DBDATA)
assert self.migration_manager.database_current_migration() == 4
def test_run_partial_migrations(self):
"""
Make sure that running full migration suite from 3 only runs
last migration
"""
self.migration_manager.set_current_migration(3)
assert self.migration_manager.database_current_migration() == 3
install_fixtures_simple(self.db, SEMI_MIGRATED_DBDATA)
self.migration_manager.migrate_new(post_callback=self._record_migration)
assert self.run_migrations == [
(4, level_exits_dict_to_list)]
assert_db_meets_expected(
self.db, EXPECTED_POST_MIGRATION_SEMI_MIGRATED_DBDATA)
# Make sure the migration is recorded correctly
assert self.migration_manager.database_current_migration() == 4
def test_migrations_recorded_as_latest(self):
"""
Make sure that if we don't have a migration_status
pre-recorded it's marked as the latest
"""
self.migration_manager.install_migration_version_if_missing()
assert self.migration_manager.database_current_migration() == 4
def test_no_migrations_recorded_as_zero(self):
"""
Make sure that if we don't have a migration_status
but there *are* no migrations that it's marked as 0
"""
self.empty_migration_manager.install_migration_version_if_missing()
assert self.empty_migration_manager.database_current_migration() == 0
def test_migrations_to_run(self):
"""
Make sure we get the right list of migrations to run
"""
self.migration_manager.set_current_migration(0)
assert self.migration_manager.migrations_to_run() == [
(1, creature_add_magical_powers),
(2, creature_rename_num_legs_to_num_limbs),
(3, creature_remove_is_demon),
(4, level_exits_dict_to_list)]
self.migration_manager.set_current_migration(3)
assert self.migration_manager.migrations_to_run() == [
(4, level_exits_dict_to_list)]
self.migration_manager.set_current_migration(4)
assert self.migration_manager.migrations_to_run() == []
def test_no_migrations_raises_exception(self):
"""
If we don't have the current migration set in the database,
this should error out.
"""
assert_raises(
MissingCurrentMigration,
self.migration_manager.migrations_to_run)
|