소스 검색

Add static method to detect db version

oz123 10 년 전
부모
커밋
7749a23848
2개의 변경된 파일13개의 추가작업 그리고 4개의 파일을 삭제
  1. 9 2
      pwman/data/convertdb.py
  2. 4 2
      pwman/tests/test_converter.py

+ 9 - 2
pwman/data/convertdb.py

@@ -186,12 +186,19 @@ class DBConverter(object):
             self.newdb_name = '.new-%s'.join(os.path.splitext(self.dbname))
 
     @staticmethod
-    def detect_db_version(self, filename):
+    def detect_db_version(filename):
         """
         This method should accept a pwman db file name, and it should try to
         detect which database version it is.
         """
-        pass
+        con = sqlite.connect(filename)
+        cur = con.cursor()
+        cur.execute("SELECT DBVERSION FROM DBVERSION")
+        row = cur.fetchone()
+        if not row:
+            return "0.3"  # pragma: no cover
+        else:
+            return row[0]
 
     @staticmethod
     def invoke_converter(dbversion, future_version):

+ 4 - 2
pwman/tests/test_converter.py

@@ -17,7 +17,7 @@
 # Copyright (C) 2012 Oz Nahum <nahumoz@gmail.com>
 # ============================================================================
 # pylint: disable=I0011
-
+from __future__ import print_function
 import sys
 import os
 sys.path.insert(0, os.getcwd())
@@ -34,6 +34,7 @@ import pwman.util.config as config
 from pwman import default_config
 import cPickle
 from test_tools import SetupTester, DummyCallback4
+from pwman.data.convertdb import DBConverter
 import copy
 import unittest
 
@@ -403,7 +404,6 @@ class TestConverter(unittest.TestCase):
 if __name__ == '__main__':
     tester = CreateTestDataBases()
     tester.run()
-
     # afther the test databases are created, invoking
     # pwman3 -d konverter-v0.5.db
     # initiates an OldCrypto instance, which can not
@@ -411,3 +411,5 @@ if __name__ == '__main__':
     # However, this database has the false database version
     # select DBVERSION from DBVERSION
     # returns -> 0.4, which is wrong. This should be 0.5
+    assert "0.4" == DBConverter.detect_db_version('konverter-v0.4.db')
+    assert "0.5" == DBConverter.detect_db_version('konverter-v0.5.db')