import errors import units __all__ = ['DeployedVersion', 'Schema', ] class DeployedVersion(units.Unit): """Which schema version the current deployment has reached.""" ID = units.UnitProperty(unicode) Version = units.UnitProperty(int) class Schema(object): guid = "" latest = 0 def __init__(self, arena): self.arena = arena arena.register(DeployedVersion) store = arena.storage(DeployedVersion) if not store.has_storage(DeployedVersion): store.create_storage(DeployedVersion) self._deployed_unit = None def _get_deployed_unit(self): if self._deployed_unit is None: box = self.arena.new_sandbox() self._deployed_unit = box.unit(DeployedVersion, ID=self.guid) if self._deployed_unit is None: self._deployed_unit = DeployedVersion(ID=self.guid, Version=0) box.memorize(self._deployed_unit) return self._deployed_unit def _get_dep(self): return self._get_deployed_unit().Version def _set_dep(self, newvalue): depunit = self._get_deployed_unit() depunit.Version = newvalue # Skip the sandbox, so we can save without repress self.arena.storage(DeployedVersion).save(depunit, forceSave=True) deployed = property(_get_dep, _set_dep, doc="Deployed version") def upgrade(self, version=None): """Upgrade deployment to version arg [latest]. Idempotent.""" if version is None: version = self.latest for step in range(self.deployed, version): procedure = getattr(self, "upgrade_to_%s" % (step + 1), None) if procedure: procedure() self.deployed = step + 1 def upgrade_to_0(self): pass def upgrade_to_1(self): classes = self.arena._registered_classes.keys() if DeployedVersion in classes: classes.remove(DeployedVersion) for cls in classes: self.arena.create_storage(cls)