Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

I think I've seen this ORM somewhere before...

Changeset 149

Show
Ignore:
Timestamp:
02/01/06 00:10:16
Author:
fumanchu
Message:

Moved assertion of storage out of schema.upgrade_to_1 and into schema.assert_storage. This allows new installs to immediately be the latest version.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/schemas.py

    r148 r149  
    2929        self.arena = arena 
    3030        arena.register(DeployedVersion) 
    31         store = arena.storage(DeployedVersion) 
    32         if not store.has_storage(DeployedVersion): 
    33             store.create_storage(DeployedVersion) 
     31        if not arena.has_storage(DeployedVersion): 
     32            arena.create_storage(DeployedVersion) 
    3433        self._deployed_unit = None 
    3534     
    36     def _get_deployed_unit(self): 
     35    def deployed_unit(self, default=None): 
     36        """Retrieve our DeployedVersion unit, optionally creating it.""" 
    3737        if self._deployed_unit is None: 
    3838            box = self.arena.new_sandbox() 
    39             self._deployed_unit = box.unit(DeployedVersion, ID=self.guid) 
    40             if self._deployed_unit is None: 
    41                 self._deployed_unit = DeployedVersion(ID=self.guid, Version=0) 
    42                 box.memorize(self._deployed_unit) 
     39            du = box.unit(DeployedVersion, ID=self.guid) 
     40            if du is None: 
     41                if default is None: 
     42                    raise errors.DejavuError("Missing DeployedVersion unit.") 
     43                else: 
     44                    du = DeployedVersion(ID=self.guid, Version=default) 
     45                    box.memorize(du) 
     46            self._deployed_unit = du 
    4347        return self._deployed_unit 
    4448     
     49    def versioned(self): 
     50        """Return True if this installation has a DeployedVersion, false otherwise.""" 
     51        box = self.arena.new_sandbox() 
     52        return bool(box.unit(DeployedVersion, ID=self.guid)) 
     53     
    4554    def _get_dep(self): 
    46         return self._get_deployed_unit().Version 
     55        # Note that we default the Version to latest. 
     56        # This allows new installs to skip all the upgrade steps, 
     57        # and just use the latest class definitions when they call 
     58        # assert_storage. However, it means that if you deploy your 
     59        # apps for a while without a Schema, and then introduce one 
     60        # later, you must manually decrement DeployedVersion from 
     61        # "latest" to the actual deployed version *before* running 
     62        # your app for the first time (or things will break due to 
     63        # the difference between the latest and deployed schema). 
     64        return self.deployed_unit(self.latest).Version 
    4765     
    4866    def _set_dep(self, newvalue): 
    49         depunit = self._get_deployed_unit(
     67        depunit = self.deployed_unit(newvalue
    5068        depunit.Version = newvalue 
    5169        # Skip the sandbox, so we can save without repress 
     
    5977            version = self.latest 
    6078         
     79        # Run upgrade_to_X methods. 
     80        if self.deployed > version: 
     81            raise errors.DejavuError("Deployed version is greater than latest version.") 
    6182        for step in range(self.deployed, version): 
    6283            self.stage = step 
     
    7192        pass 
    7293     
    73     def upgrade_to_1(self): 
     94    def assert_storage(self): 
     95        """Assert that each class has storage reserved for it. 
     96         
     97        You may choose to call this method in an admin script (at the 
     98        discretion of the deployer), or on application startup. If you 
     99        call this method on every application start, you should do it 
     100        after calling upgrade(). Once all of the upgrade methods are done, 
     101        you can then safely use this method to assert the _final_ schemas 
     102        for all classes. Note that, if the DeployedVersion unit was just 
     103        created (our actual deployed version was unknown), then some of 
     104        the upgrade methods may fail. However, they could fail just as 
     105        easily if you run assert_storage before the upgrade methods. 
     106        """ 
     107         
    74108        classes = self.arena._registered_classes.keys() 
    75109        if DeployedVersion in classes: 
     
    77111         
    78112        for cls in classes: 
    79             self.arena.create_storage(cls) 
     113            if not self.arena.has_storage(cls): 
     114                self.arena.create_storage(cls)