[pkg-fso-commits] [SCM] FSO frameworkd Debian packaging branch, debian, updated. upstream/0.9.5.5-717-g0f98819
Daniele Ricci
daniele.athome at gmail.com
Sat Aug 6 08:20:10 UTC 2011
The following commit has been merged in the debian branch:
commit e6b8aad3c179bb6acdbbb9c2d8c35f5a5249b376
Author: Daniele Ricci <daniele.athome at gmail.com>
Date: Tue Oct 19 10:56:50 2010 +0200
PIM: database upgrader for version 2.0 to 2.1
Signed-off-by: Daniele Ricci <daniele.athome at gmail.com>
diff --git a/tools/update-db-2.1.sql b/framework/subsystems/opimd/db/upgrade-2.1.sql
similarity index 86%
copy from tools/update-db-2.1.sql
copy to framework/subsystems/opimd/db/upgrade-2.1.sql
index 4fd68ac..e1efabb 100644
--- a/tools/update-db-2.1.sql
+++ b/framework/subsystems/opimd/db/upgrade-2.1.sql
@@ -4,3 +4,5 @@ DELETE FROM messages_boolean WHERE field_name = 'MessageRead' AND value = '1';
UPDATE messages_boolean SET value = NOT value WHERE field_name = 'MessageRead' OR field_name = 'MessageSent';
-- rename MessageRead and MessageSent to New
UPDATE messages_boolean SET field_name = 'New' WHERE field_name = 'MessageRead' OR field_name = 'MessageSent';
+-- update version info
+REPLACE INTO info VALUES('version', '2.1');
diff --git a/framework/subsystems/opimd/db_handler.py b/framework/subsystems/opimd/db_handler.py
index 9af432b..7a26d47 100644
--- a/framework/subsystems/opimd/db_handler.py
+++ b/framework/subsystems/opimd/db_handler.py
@@ -42,6 +42,7 @@ import framework.patterns.tasklet as tasklet
from framework.config import config, rootdir
import re
+import db_upgrade
try:
import phoneutils
@@ -76,6 +77,7 @@ def regex_matches(string, pattern):
return 0
def dict_factory(description, row, skip_field = None):
+ """Used for creating column-based dictionaries from simple resultset rows (ie lists)"""
d = {}
for idx, col in enumerate(description):
if col[0] != skip_field:
@@ -85,7 +87,6 @@ def dict_factory(description, row, skip_field = None):
rootdir = os.path.join( rootdir, 'opim' )
_SQLITE_FILE_NAME = os.path.join(rootdir,'pim.db')
-_SQLITE_DATABASE_VERSION = '2.1'
class DbHandler(object):
con = None
@@ -156,16 +157,15 @@ class DbHandler(object):
cur.execute(self.get_create_type_index(type))
self.con.commit()
- cur.execute("SELECT value FROM info WHERE field_name = 'version'")
- version_info = cur.fetchone()
- if version_info == None:
- cur.execute("INSERT INTO info VALUES(?, ?)", ('version', _SQLITE_DATABASE_VERSION))
- self.con.commit()
- elif version_info[0] != _SQLITE_DATABASE_VERSION:
- raise Exception("Database version mismatch, needed %s, current is %s" % (_SQLITE_DATABASE_VERSION, version_info[0]))
+ check, version = db_upgrade.check_version(cur)
+
+ if check == db_upgrade.DB_UNSUPPORTED:
+ raise Exception("Unsupported database version %s" % (version))
+ elif check == db_upgrade.DB_NEEDS_UPGRADE:
+ db_upgrade.upgrade(version, cur, self.con)
cur.close()
-
+
except Exception, exp:
logger.error("""The following errors occured when trying to init db: %s\n%s""", _SQLITE_FILE_NAME, str(exp))
raise
diff --git a/framework/subsystems/opimd/db_upgrade.py b/framework/subsystems/opimd/db_upgrade.py
index 412ad96..3e6b48b 100644
--- a/framework/subsystems/opimd/db_upgrade.py
+++ b/framework/subsystems/opimd/db_upgrade.py
@@ -33,7 +33,7 @@
2.1 - MessageSent and MessageRead changed to use only New for both
"""
-import os
+import sys, os
import sqlite3
import logging
@@ -48,9 +48,56 @@ DB_VERSIONS = (
"2.1"
)
+# values returned by check_version
DB_OK=0 # database is ok
DB_NEEDS_UPGRADE=1 # database needs upgrade
DB_UNSUPPORTED=2 # database version not supported (too new)
-def check_version(conn, cur):
- return DB_NEEDS_UPGRADE
+def check_version(cur):
+ """Checks if the database is supported and if it needs to be upgraded."""
+ cur.execute("SELECT value FROM info WHERE field_name = 'version'")
+ version_info = cur.fetchone()
+
+ # no version info -- try to upgrade it to the latest
+ if version_info == None or len(version_info) == 0:
+ return (DB_NEEDS_UPGRADE, None)
+
+ version = version_info[0]
+ try:
+ ver_index = DB_VERSIONS.index(version)
+ except:
+ # unknown version (too new for us?)
+ return (DB_UNSUPPORTED, version)
+
+ if ver_index < len(DB_VERSIONS) - 1:
+ return (DB_NEEDS_UPGRADE, version)
+
+ # current version - no upgrade needed
+ return (DB_OK, version)
+
+def upgrade(version, cur, con):
+ """Upgrades the database to the latest version.
+ @param version the current database version.
+ @return True if the database has been upgraded, False if it doesn't need any
+ otherwise throws an exception
+ """
+ latest = DB_VERSIONS[-1]
+
+ # just to be sure
+ if version == latest: return False
+
+ base_path = os.path.dirname(__file__)
+
+ # begin to run upgrade script from the current version to the latest one
+ version_index = DB_VERSIONS.index(version) if version != None else 0
+ for i in range(version_index, len(DB_VERSIONS)):
+ try:
+ sql = open(os.path.join(base_path, 'db', 'upgrade-%s.sql' % (DB_VERSIONS[i])), 'r')
+ except:
+ continue
+
+ cur.executescript(sql.read())
+ sql.close()
+ con.commit()
+
+ return True
--
FSO frameworkd Debian packaging
More information about the pkg-fso-commits
mailing list