Refactor metadata upgrade version range calculation to use generator
This commit is contained in:
parent
bed3f551fd
commit
b67b4b2ef6
1 changed files with 15 additions and 10 deletions
|
@ -32,6 +32,19 @@ class StructuredDict(dict):
|
||||||
if migrate:
|
if migrate:
|
||||||
self.migrate(target_version)
|
self.migrate(target_version)
|
||||||
|
|
||||||
|
def _upgrade_version_range(self, start_version, end_version):
|
||||||
|
after_starting_version = False
|
||||||
|
for version, schema, migration in self._versions:
|
||||||
|
if not after_starting_version:
|
||||||
|
if version == self.version:
|
||||||
|
after_starting_version = True
|
||||||
|
continue
|
||||||
|
|
||||||
|
yield version, schema, migration
|
||||||
|
|
||||||
|
if end_version and version == end_version:
|
||||||
|
break
|
||||||
|
|
||||||
def validate(self, version):
|
def validate(self, version):
|
||||||
jsonschema.validate(self, self._schemas[version])
|
jsonschema.validate(self, self._schemas[version])
|
||||||
|
|
||||||
|
@ -39,19 +52,11 @@ class StructuredDict(dict):
|
||||||
if target_version:
|
if target_version:
|
||||||
assert self._versions.index(target_version) > self.versions.index(self.version), "Current version is above target version"
|
assert self._versions.index(target_version) > self.versions.index(self.version), "Current version is above target version"
|
||||||
|
|
||||||
above_starting_version = False
|
for version, schema, migration in self._upgrade_version_range(self.version, target_version):
|
||||||
for version, schema, migration in self._versions:
|
migration(self)
|
||||||
if not above_starting_version:
|
|
||||||
if version == self.version:
|
|
||||||
above_starting_version = True
|
|
||||||
continue
|
|
||||||
|
|
||||||
migration()
|
|
||||||
try:
|
try:
|
||||||
self.validate(version)
|
self.validate(version)
|
||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
raise ValidationError, "Could not migrate to version %s due to validation error: %s" % (version, e.message)
|
raise ValidationError, "Could not migrate to version %s due to validation error: %s" % (version, e.message)
|
||||||
|
|
||||||
self.version = version
|
self.version = version
|
||||||
if target_version and version == target_version:
|
|
||||||
break
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue