manager.py :  » Build » Buildbot » buildbot-0.8.0 » buildbot » db » schema » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Build » Buildbot 
Buildbot » buildbot 0.8.0 » buildbot » db » schema » manager.py
from twisted.python import reflect

# note that schema modules are not loaded unless an upgrade is taking place

CURRENT_VERSION = 5

class DBSchemaManager(object):
    """
    This class is responsible for managing the database schema and upgrading it
    as necessary.  This includes both the *actual* database and the old pickle
    database, as migrations move data between the two.

    Note that this class is *entirely synchronous*!  Performing any other operations
    while changing the schema is just asking for trouble.
    """
    def __init__(self, spec, basedir):
        self.spec = spec
        self.basedir = basedir
        self.dbapi = self.spec.get_dbapi()

    def get_db_version(self, conn=None):
        """
        Get the current schema version for this database
        """
        close_conn = False
        if not conn:
            conn = self.spec.get_sync_connection()
            close_conn = True
        c = conn.cursor()
        try:
            try:
                c.execute("SELECT version FROM version")
                rows = c.fetchall()
                assert len(rows) == 1, "%i rows in version table! (should only be 1)" % len(rows)
                return rows[0][0]
            except (self.dbapi.OperationalError, self.dbapi.ProgrammingError):
                # no version table = version 0
                return 0
        finally:
            if close_conn:
                conn.close()

    def get_current_version(self):
        """
        Get the current db version for this release of buildbot
        """
        return CURRENT_VERSION

    def is_current(self):
        """
        Is this database current?
        """
        return self.get_db_version() == self.get_current_version()

    def upgrade(self, quiet=False):
        """
        Upgrade this database to the current version
        """
        conn = self.spec.get_sync_connection()
        try:
            while self.get_db_version() < self.get_current_version():
                next_version = self.get_db_version() + 1
                next_version_module = reflect.namedModule("buildbot.db.schema.v%d" % next_version)
                upg = next_version_module.Upgrader(self.dbapi, conn, self.basedir, quiet)
                upg.upgrade()
                conn.commit()
                assert self.get_db_version() == next_version
        finally:
            conn.close()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.