[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Make schema a constant, near the version



commit 19b8393652a3cba6741d1051d49af02b735c883e
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Tue Aug 29 13:19:10 2017 -0700

    Make schema a constant, near the version
    
    Placing the schema version with the schema itself will help us remember to bump
    it when the later changes.
---
 stem/cached_tor_manual.sqlite | Bin 217088 -> 218112 bytes
 stem/manual.py                |  41 ++++++++++++++++++++++++++++-------------
 2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/stem/cached_tor_manual.sqlite b/stem/cached_tor_manual.sqlite
index bc50d083..898b09bc 100644
Binary files a/stem/cached_tor_manual.sqlite and b/stem/cached_tor_manual.sqlite differ
diff --git a/stem/manual.py b/stem/manual.py
index 21eb68ee..366b0ea9 100644
--- a/stem/manual.py
+++ b/stem/manual.py
@@ -84,7 +84,18 @@ Category = stem.util.enum.Enum('GENERAL', 'CLIENT', 'RELAY', 'DIRECTORY', 'AUTHO
 GITWEB_MANUAL_URL = 'https://gitweb.torproject.org/tor.git/plain/doc/tor.1.txt'
 CACHE_PATH = os.path.join(os.path.dirname(__file__), 'cached_tor_manual.sqlite')
 DATABASE = None  # cache database connections
-SCHEMA_VERSION = 1  # version of the schema used by our sqlite cache
+
+SCHEMA_VERSION = 1  # version of our scheme, bump this if you change the following
+SCHEMA = (
+  'CREATE TABLE schema(version NUMBER)',
+  'INSERT INTO schema(version) VALUES (%i)' % SCHEMA_VERSION,
+
+  'CREATE TABLE metadata(name TEXT, synopsis TEXT, description TEXT, man_commit TEXT, stem_commit TEXT)',
+  'CREATE TABLE commandline(name TEXT PRIMARY KEY, description TEXT)',
+  'CREATE TABLE signals(name TEXT PRIMARY KEY, description TEXT)',
+  'CREATE TABLE files(name TEXT PRIMARY KEY, description TEXT)',
+  'CREATE TABLE torrc(key TEXT PRIMARY KEY, name TEXT, category TEXT, usage TEXT, summary TEXT, description TEXT, position NUMBER)',
+)
 
 CATEGORY_SECTIONS = OrderedDict((
   ('GENERAL OPTIONS', Category.GENERAL),
@@ -396,13 +407,15 @@ class Manual(object):
 
     with sqlite3.connect(path) as conn:
       try:
-        name, synopsis, description, man_commit, stem_commit, schema = conn.execute('SELECT name, synopsis, description, man_commit, stem_commit, schema FROM metadata').fetchone()
+        schema = conn.execute('SELECT version FROM schema').fetchone()[0]
+
+        if schema != SCHEMA_VERSION:
+          raise SchemeMismatch("Stem's current manual schema version is %s, but %s was version %s" % (SCHEMA_VERSION, path, schema), schema, SCHEMA_VERSION)
+
+        name, synopsis, description, man_commit, stem_commit = conn.execute('SELECT name, synopsis, description, man_commit, stem_commit FROM metadata').fetchone()
       except sqlite3.OperationalError as exc:
         raise IOError('Failed to read database metadata from %s: %s' % (path, exc))
 
-      if schema != SCHEMA_VERSION:
-        raise SchemeMismatch("Stem's current manual schema version is %s, but %s was version %s" % (SCHEMA_VERSION, path, schema), schema, SCHEMA_VERSION)
-
       commandline = dict(conn.execute('SELECT name, description FROM commandline').fetchall())
       signals = dict(conn.execute('SELECT name, description FROM signals').fetchall())
       files = dict(conn.execute('SELECT name, description FROM files').fetchall())
@@ -553,14 +566,16 @@ class Manual(object):
       return self._save_as_config(path)
 
   def _save_as_sqlite(self, path):
-    with sqlite3.connect(path + '.new') as conn:
-      conn.execute('CREATE TABLE metadata(name TEXT, synopsis TEXT, description TEXT, man_commit TEXT, stem_commit TEXT, schema NUMBER)')
-      conn.execute('CREATE TABLE commandline(name TEXT PRIMARY KEY, description TEXT)')
-      conn.execute('CREATE TABLE signals(name TEXT PRIMARY KEY, description TEXT)')
-      conn.execute('CREATE TABLE files(name TEXT PRIMARY KEY, description TEXT)')
-      conn.execute('CREATE TABLE torrc(key TEXT PRIMARY KEY, name TEXT, category TEXT, usage TEXT, summary TEXT, description TEXT, position NUMBER)')
+    tmp_path = path + '.new'
+
+    if os.path.exists(tmp_path):
+      os.remove(tmp_path)
+
+    with sqlite3.connect(tmp_path) as conn:
+      for cmd in SCHEMA:
+        conn.execute(cmd)
 
-      conn.execute('INSERT INTO metadata(name, synopsis, description, man_commit, stem_commit, schema) VALUES (?,?,?,?,?,?)', (self.name, self.synopsis, self.description, self.man_commit, self.stem_commit, SCHEMA_VERSION))
+      conn.execute('INSERT INTO metadata(name, synopsis, description, man_commit, stem_commit) VALUES (?,?,?,?,?)', (self.name, self.synopsis, self.description, self.man_commit, self.stem_commit))
 
       for k, v in self.commandline_options.items():
         conn.execute('INSERT INTO commandline(name, description) VALUES (?,?)', (k, v))
@@ -577,7 +592,7 @@ class Manual(object):
     if os.path.exists(path):
       os.remove(path)
 
-    os.rename(path + '.new', path)
+    os.rename(tmp_path, path)
 
   def _save_as_config(self, path):
     conf = stem.util.conf.Config()



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits