Track schemas better
authorGregory Szorc <gps@mozilla.com>
Thu, 25 Jul 2013 17:06:12 -0700
changeset 34 9bc323bcc74f4fc7e84d11fe9d30c53929c937d8
parent 33 82e14d3466b7c817a04996ec7ebccae47c20925b
child 35 741341c5d5cd2d3002826f51222ec62003276420
push id18
push usergszorc@mozilla.com
push dateFri, 26 Jul 2013 00:06:16 +0000
Track schemas better
mozautomation/changetracker.py
--- a/mozautomation/changetracker.py
+++ b/mozautomation/changetracker.py
@@ -17,43 +17,44 @@ class ChangeTracker(object):
 
     def __init__(self, path):
         self._db = sqlite3.connect(path)
 
         if not self._schema_current():
             self._create_schema()
 
     def _schema_current(self):
-        return self._db.execute('SELECT COUNT(*) FROM SQLITE_MASTER WHERE '
-            'name="trees"').fetchone()[0] == 1
+        return self._db.execute('PRAGMA user_version').fetchone()[0] == 1
 
     def _create_schema(self):
         with self._db:
-            self._db.execute('CREATE TABLE trees ('
+            self._db.execute('CREATE TABLE IF NOT EXISTS trees ('
                 'id INTEGER PRIMARY KEY AUTOINCREMENT, '
                 'name TEXT, '
                 'url TEXT '
                 ')')
 
-            self._db.execute('CREATE TABLE pushes ('
+            self._db.execute('CREATE TABLE IF NOT EXISTS pushes ('
                 'push_id INTEGER, '
                 'tree_id INTEGER, '
                 'time INTEGER, '
                 'user TEXT, '
                 'PRIMARY KEY (push_id, tree_id) '
                 ')')
 
-            self._db.execute('CREATE TABLE changeset_pushes ('
+            self._db.execute('CREATE TABLE IF NOT EXISTS changeset_pushes ('
                 'changeset TEXT, '
                 'head_changeset TEXT, '
                 'push_id INTEGER, '
                 'tree_id INTEGER, '
                 'UNIQUE (changeset, tree_id) '
                 ')')
 
+            self._db.execute('PRAGMA user_version=1')
+
     def tree_id(self, tree, url=None):
         with self._db:
             field = self._db.execute('SELECT id FROM trees WHERE name=? LIMIT 1',
                 [tree]).fetchone()
 
             if field:
                 return field[0]