24
24
applied at any time by calling in the API directly. Once applied, a
25
25
migration's version string is registered so it will not be applied again.
27
We see that the base migration is already applied.
27
We see that the base migration, as well as subsequent standard migrations, are
29
30
>>> from mailman.model.version import Version
30
31
>>> results = config.db.store.find(Version, component='schema')
31
32
>>> results.count()
33
>>> base = results.one()
34
>>> print base.component
36
>>> print base.version
34
>>> versions = sorted(result.version for result in results)
35
>>> for version in versions:
55
56
... migrations_path: migrations
59
.. Clean this up at the end of the doctest.
62
... from mailman.config import config
63
... config.pop('migrations')
64
... shutil.rmtree(tempdir)
65
>>> cleanups.append(cleanup)
58
67
Here is an example migrations module. The key part of this interface is the
59
68
``upgrade()`` method, which takes four arguments:
70
79
>>> with open(os.path.join(path, '__init__.py'), 'w') as fp:
72
>>> with open(os.path.join(path, 'mm_20120211000000.py'), 'w') as fp:
81
>>> with open(os.path.join(path, 'mm_20129999000000.py'), 'w') as fp:
73
82
... print >> fp, """
74
83
... from __future__ import unicode_literals
75
84
... from mailman.model.version import Version
86
95
>>> for result in sorted(result.version for result in results):
90
100
>>> test = config.db.store.find(Version, component='test').one()
91
101
>>> print test.version
94
104
Migrations will only be loaded once.
96
>>> with open(os.path.join(path, 'mm_20120211000001.py'), 'w') as fp:
106
>>> with open(os.path.join(path, 'mm_20129999000001.py'), 'w') as fp:
97
107
... print >> fp, """
98
108
... from __future__ import unicode_literals
99
109
... from mailman.model.version import Version
115
125
>>> for result in sorted(result.version for result in results):
120
131
>>> test = config.db.store.find(Version, component='test')
121
132
>>> for marker in sorted(marker.version for marker in test):
126
137
We do not get an 802 marker because the migration has already been loaded.
130
141
>>> for result in sorted(result.version for result in results):
135
147
>>> test = config.db.store.find(Version, component='test')
136
148
>>> for marker in sorted(marker.version for marker in test):
141
.. Clean up the temporary directory::
143
>>> config.pop('migrations')
144
>>> sys.path.remove(tempdir)
146
>>> shutil.rmtree(tempdir)
157
It's possible (mostly for testing purposes) to only do a partial upgrade, by
158
providing a timestamp to `load_migrations()`. To demonstrate this, we add two
159
additional migrations, intended to be applied in sequential order.
161
>>> from shutil import copyfile
162
>>> from mailman.testing.helpers import chdir
163
>>> with chdir(path):
164
... copyfile('mm_20129999000000.py', 'mm_20129999000002.py')
165
... copyfile('mm_20129999000000.py', 'mm_20129999000003.py')
166
... copyfile('mm_20129999000000.py', 'mm_20129999000004.py')
168
Now, only migrate to the ...03 timestamp.
170
>>> config.db.load_migrations('20129999000003')
172
You'll notice that the ...04 version is not present.
174
>>> results = config.db.store.find(Version, component='schema')
175
>>> for result in sorted(result.version for result in results):