<s> from base_drv import BaseDatabase as Database <EOL> import sqllib , sqlerrors <EOL> def getDatabaseVersion ( db ) : <EOL> if isinstance ( db , Database ) : <EOL> return db . getVersion ( ) <EOL> cu = db . cursor ( ) <EOL> try : <EOL> cu . execute ( "<STR_LIT>" ) <EOL> except : <EOL> return sqllib . DBversion ( <NUM_LIT:0> ) <EOL> ret = cu . fetchone ( ) <EOL> if ret . has_key ( "<STR_LIT>" ) : <EOL> return sqllib . DBversion ( ret [ "<STR_LIT:version>" ] , ret [ "<STR_LIT>" ] ) <EOL> return sqllib . DBversion ( ret [ "<STR_LIT:version>" ] ) <EOL> class SchemaMigration : <EOL> Version = <NUM_LIT:0> <EOL> def __init__ ( self , db ) : <EOL> self . db = db <EOL> self . cu = db . cursor ( ) <EOL> self . version = db . getVersion ( ) <EOL> self . Version = self . _dbVersion ( self . Version ) <EOL> self . msg = "<STR_LIT>" % self . Version <EOL> assert ( self . canUpgrade ( ) ) <EOL> def _dbVersion ( self , v ) : <EOL> if isinstance ( v , sqllib . DBversion ) : <EOL> return v <EOL> elif isinstance ( v , int ) : <EOL> return sqllib . DBversion ( v ) <EOL> elif isinstance ( v , tuple ) : <EOL> return sqllib . DBversion ( * v ) <EOL> raise RuntimeError ( "<STR_LIT>" , DBversion ) <EOL> def canUpgrade ( self ) : <EOL> if self . version . major == self . Version . major : <EOL> return self . version . minor <= self . Version . minor <EOL> if self . version . major == self . Version . major - <NUM_LIT:1> : <EOL> return True <EOL> return False <EOL> def migrate ( self ) : <EOL> return False <EOL> def message ( self , msg = None ) : <EOL> pass <EOL> def __migrate ( self , toVer , func , skipCommit ) : <EOL> self . message ( "<STR_LIT>" % ( self . version , toVer ) ) <EOL> if not skipCommit : <EOL> self . db . transaction ( ) <EOL> try : <EOL> if not func ( ) : <EOL> raise sqlerrors . SchemaVersionError ( <EOL> "<STR_LIT>" % ( <EOL> self . version , toVer ) , self . version , self . Version ) <EOL> self . db . setVersion ( toVer , skipCommit = True ) <EOL> except : <EOL> if not skipCommit : <EOL> self . db . rollback ( ) <EOL> raise <EOL> else : <EOL> if not skipCommit : <EOL> self . db . commit ( ) <EOL> return toVer <EOL> def __call__ ( self , skipCommit = False ) : <EOL> if not self . canUpgrade ( ) : <EOL> return self . version <EOL> if self . version . major < self . Version . major : <EOL> toVer = self . _dbVersion ( self . Version . major ) <EOL> self . version = self . __migrate ( toVer , self . migrate , skipCommit ) <EOL> assert ( self . version . major == self . Version . major ) <EOL> while self . version . minor < self . Version . minor : <EOL> nextmin = self . version . minor + <NUM_LIT:1> <EOL> toVer = self . _dbVersion ( ( self . version . major , nextmin ) ) <EOL> func = getattr ( self , "<STR_LIT>" % ( nextmin , ) ) <EOL> self . version = self . __migrate ( toVer , func , skipCommit ) <EOL> self . message ( "<STR_LIT>" ) <EOL> return self . version </s>
