[llvm-commits] [LNT] r163167 - /lnt/trunk/lnt/server/db/migrate.py

Michael Gottesman mgottesman at apple.com
Tue Sep 4 14:54:55 PDT 2012


Author: mgottesman
Date: Tue Sep  4 16:54:55 2012
New Revision: 163167

URL: http://llvm.org/viewvc/llvm-project?rev=163167&view=rev
Log:
[LNT] migrate.py: Update the update function so that it works correctly for PostgresSQL.

Specifically, I refactored out the method of detecting the lack of existance of a schema
so that we can use different methods for different databases. Additionally, due to the
transaction isolation properties of PostgreSQL, we need to ensure that after we create
our table we begin a new transaction since otherwise we will be unable to see any of
the new tables and thus will fail to insert data into said tables.

Modified:
    lnt/trunk/lnt/server/db/migrate.py

Modified: lnt/trunk/lnt/server/db/migrate.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/migrate.py?rev=163167&r1=163166&r2=163167&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/migrate.py (original)
+++ lnt/trunk/lnt/server/db/migrate.py Tue Sep  4 16:54:55 2012
@@ -175,21 +175,37 @@
     # Create a session for our queries.
     session = sqlalchemy.orm.sessionmaker(engine)()
 
+    def will_not_handle_error(e):
+        message = e.orig.message
+        if 'no such table' in message:
+            return False
+        if 'relation' in message and 'does not exist' in message:
+            return False
+        return True
+
     # Load all the information from the versions tables. We just do the query
     # and handle the exception if the table hasn't been defined yet (for
     # databases before versioning started).
     try:
         version_list = session.query(SchemaVersion).all()
-    except sqlalchemy.exc.OperationalError,e:
+    except (sqlalchemy.exc.OperationalError,
+            sqlalchemy.exc.ProgrammingError) as e:
+
         # Filter on the DB-API error message. This is a bit flimsy, but works
-        # for SQLite at least.
-        if 'no such table' not in str(e.orig):
+        # for SQLite and PostgresSQL at least.
+        if will_not_handle_error(e):
             raise
 
         # Create the SchemaVersion table.
         Base.metadata.create_all(engine)
         session.commit()
 
+        # Certain databases like PostgreSQL use ``isolated''
+        # transactions, i.e., we will be unable to insert data. Thus
+        # to ensure that no matter the settings, i.e. whether or not
+        # autocommit is set, we create a new session.
+        session = sqlalchemy.orm.sessionmaker(engine)()
+
         version_list = []
     versions = dict((v.name, v)
                     for v in version_list)





More information about the llvm-commits mailing list