diff sat_pubsub/pgsql_storage.py @ 344:8cf1be9572f8

pgsql: check schema version and exit with a message asking to upgrade if it's not the current one
author Goffi <goffi@goffi.org>
date Sun, 20 Aug 2017 22:54:58 +0200
parents b49f75a26156
children 3bbab2173ebc
line wrap: on
line diff
--- a/sat_pubsub/pgsql_storage.py	Sun Aug 20 18:41:21 2017 +0200
+++ b/sat_pubsub/pgsql_storage.py	Sun Aug 20 22:54:58 2017 +0200
@@ -55,6 +55,7 @@
 
 from zope.interface import implements
 
+from twisted.internet import reactor
 from twisted.words.protocols.jabber import jid
 from twisted.python import log
 
@@ -74,6 +75,7 @@
 # parseXml manage str, but we get unicode
 parseXml = lambda unicode_data: generic.parseXml(unicode_data.encode('utf-8'))
 PEP_COL_NAME = 'pep'
+CURRENT_VERSION = '2'
 
 
 def withPEP(query, values, pep, recipient):
@@ -116,6 +118,19 @@
 
     def __init__(self, dbpool):
         self.dbpool = dbpool
+        d = self.dbpool.runQuery("SELECT value FROM metadata WHERE key='version'")
+        d.addCallbacks(self._checkVersion, self._versionEb)
+
+    def _checkVersion(self, row):
+        version = row[0].value
+        if version != CURRENT_VERSION:
+            logging.error("Bad database schema version ({current}), please upgrade to {needed}".format(
+                current=version, needed=CURRENT_VERSION))
+            reactor.stop()
+
+    def _versionEb(self, failure):
+        logging.error("Can't check schema version: {reason}".format(reason=failure))
+        reactor.stop()
 
     def _buildNode(self, row):
         """Build a note class from database result row"""