1
"""Migrate data for the mongodb instance.
3
Loads migrations from the latest available in the versions directory.
10
class DataStore(object):
11
"""Communicate with the data store to determine version status."""
13
def __init__(self, db):
14
"""Talk to the data store
16
:param db: The mongo db connection.
21
def current_version(self):
22
"""Return the current version of the data store."""
23
return self.db.version.find_one({
27
def version_datastore(self):
28
"""Init the data to track the current version in the datastore.
30
The data store starts out with version 0.
32
Raises exception if the store is already versioned.
34
if self.current_version:
35
raise('Data store is already versioned: ' + self.current_version)
36
self.db.version.insert({'_id': 'version', 'version': 0})
39
class Versions(object):
40
FILENAME_WITH_VERSION = re.compile(r'^(\d{3,}).*')
42
def _next_ver_num(self, use_timestamp_numbering):
43
return self.latest + 1
45
def _version_path(self, ver):
46
"""Returns path of file in versions repository"""
47
return os.path.join(self.path, str(ver))
49
def __init__(self, path):
50
"""Collect version scripts and store them in self.versions
53
# Create temporary list of files, allowing skipped version numbers.
54
files = os.listdir(path)
58
match = self.FILENAME_WITH_VERSION.match(name)
60
num = int(match.group(1))
63
pass # Must be a helper file or something, let's ignore it.
66
version_numbers = versions.keys()
67
version_numbers.sort()
69
self.version_indexes = version_numbers
70
for idx in version_numbers:
71
self.versions[idx] = versions[idx]
73
def create_new_python_version(self, description, **k):
74
"""Create Python files for new version"""
75
ver = self._next_ver_num(k.pop('use_timestamp_numbering', False))
76
extra = str_to_filename(description)
81
elif not extra.startswith('_'):
84
filename = '%03d%s.py' % (ver, extra)
85
filepath = self._version_path(filename)
87
script.PythonScript.create(filepath, **k)
88
self.versions[ver] = Version(ver, self.path, [filename])
92
""":returns: Latest version in Collection"""
93
return self.version_indexes[-1]