def setVersion(self, version, skipCommit=False): assert (self.dbh) if isinstance(version, int): version = sqllib.DBversion(version) elif isinstance(version, tuple): version = sqllib.DBversion(*version) assert (isinstance(version, sqllib.DBversion)) c = self.cursor() crtVersion = self.getVersion() # test if we have the old style database version and update if crtVersion > 0 and crtVersion.minor == 0: c.execute("select * from DatabaseVersion") ret = c.fetchone() if len(ret) == 1: # old style, one number c.execute("drop table DatabaseVersion") crtVersion = 0 # mark for re-creation # do not allow "going back" assert (version >= crtVersion) if crtVersion == 0: # indicates table is not there c.execute( "CREATE TABLE DatabaseVersion (version INTEGER, minor INTEGER)" ) c.execute( "INSERT INTO DatabaseVersion (version, minor) VALUES (?,?)", (version.major, version.minor)) if not skipCommit: self.commit() self.tables['DatabaseVersion'] = [] return version c.execute("UPDATE DatabaseVersion SET version = ?, minor = ?", (version.major, version.minor)) if not skipCommit: self.commit() return version
def _dbVersion(self, v): if isinstance(v, sqllib.DBversion): return v elif isinstance(v, int): return sqllib.DBversion(v) elif isinstance(v, tuple): return sqllib.DBversion(*v) raise RuntimeError("Invalid DBversion specification", DBversion)
def getDatabaseVersion(db): if isinstance(db, Database): return db.getVersion() cu = db.cursor() try: cu.execute("select * from DatabaseVersion") except: return sqllib.DBversion(0) ret = cu.fetchone() if ret.has_key("minor"): return sqllib.DBversion(ret["version"], ret["minor"]) return sqllib.DBversion(ret["version"])
def getVersion(self, raiseOnError=False): """ Get the current schema version. If the version table is not present, return a zero version. @param raiseOnError: If set, raise instead of returning zero if the table is missing. @type raiseOnError: C{bool} @rtype L{DBversion<conary.dbstore.sqllib.DBversion>} """ assert (self.dbh) # If self.tables is non-empty, loadSchema() has probably been # called, so we can do a fast (and non-intrusive) check for # our table. if self.tables and 'DatabaseVersion' not in self.tables: self.version = sqllib.DBversion(0, 0) return self.version # Otherwise, the schema might not be loaded so use a try/except # pattern. # DatabaseVersion canbe an old style table that has only a version column # or it could be a new style version that has (version, minor) columns # or it can be a mint table that has (version, timestamps) columns c = self.cursor() used_savepoint = None if self.savepoints and self.inTransaction(): used_savepoint = 'getversion_save' self.transaction(used_savepoint) try: c.execute("select * from DatabaseVersion limit 1") except sqlerrors.InvalidTable: if raiseOnError: raise if self.savepoints: self.rollback(used_savepoint) self.version = sqllib.DBversion(0, 0) return self.version # keep compatibility with old style table versioning ret = c.fetchone_dict() if ret is None: # no version record found... self.version = sqllib.DBversion(0, 0) elif ret.has_key("minor"): # assume new style self.version = sqllib.DBversion(ret["version"], ret["minor"]) else: # assume mint/old style c.execute("select max(version) from DatabaseVersion") self.version = sqllib.DBversion(c.fetchone()[0]) return self.version
def getVersion(self): try: return BaseDatabase.getVersion(self, raiseOnError=True) except sqlerrors.InvalidTable: # Postgres is so nice that it ruined the current # transaction because of the missing table, so roll back. self.dbh.rollback() self.version = sqllib.DBversion(0, 0) return self.version