]> git.decadent.org.uk Git - dak.git/blobdiff - dak/update_db.py
Use correct db_name for MD5 hash
[dak.git] / dak / update_db.py
index 928396c531d702e252f2da63f59df5795d24fab4..61a1089210295e5ab759d7858e59edc5f4bfb58e 100755 (executable)
@@ -37,15 +37,17 @@ import os
 import apt_pkg
 import time
 import errno
+from glob import glob
+from re import findall
 
 from daklib import utils
 from daklib.config import Config
 from daklib.dak_exceptions import DBUpdateError
+from daklib.daklog import Logger
 
 ################################################################################
 
 Cnf = None
-required_database_schema = 42
 
 ################################################################################
 
@@ -100,26 +102,46 @@ Updates dak's database schema to the lastest version. You should disable crontab
             print "No configuration table found, assuming dak database revision to be pre-zero"
             return -1
 
+################################################################################
+
+    def get_transaction_id(self):
+        '''
+        Returns the current transaction id as a string.
+        '''
+        cursor = self.db.cursor()
+        cursor.execute("SELECT txid_current();")
+        id = cursor.fetchone()[0]
+        cursor.close()
+        return id
+
 ################################################################################
 
     def update_db(self):
         # Ok, try and find the configuration table
         print "Determining dak database revision ..."
         cnf = Config()
+        logger = Logger('update-db')
+        modules = []
 
         try:
             # Build a connect string
-            connect_str = "dbname=%s"% (cnf["DB::Name"])
-            if cnf["DB::Host"] != '': connect_str += " host=%s" % (cnf["DB::Host"])
-            if cnf["DB::Port"] != '-1': connect_str += " port=%d" % (int(cnf["DB::Port"]))
+            if cnf.has_key("DB::Service"):
+                connect_str = "service=%s" % cnf["DB::Service"]
+            else:
+                connect_str = "dbname=%s"% (cnf["DB::Name"])
+                if cnf.has_key("DB::Host") and cnf["DB::Host"] != '':
+                    connect_str += " host=%s" % (cnf["DB::Host"])
+                if cnf.has_key("DB::Port") and cnf["DB::Port"] != '-1':
+                    connect_str += " port=%d" % (int(cnf["DB::Port"]))
 
             self.db = psycopg2.connect(connect_str)
 
-        except:
-            print "FATAL: Failed connect to database"
-            pass
+        except Exception as e:
+            print "FATAL: Failed connect to database (%s)" % str(e)
+            sys.exit(1)
 
         database_revision = int(self.get_db_rev())
+        logger.log(['transaction id before update: %s' % self.get_transaction_id()])
 
         if database_revision == -1:
             print "dak database schema predates update-db."
@@ -135,25 +157,45 @@ Updates dak's database schema to the lastest version. You should disable crontab
             self.update_db_to_zero()
             database_revision = 0
 
+        dbfiles = glob(os.path.join(os.path.dirname(__file__), 'dakdb/update*.py'))
+        required_database_schema = max(map(int, findall('update(\d+).py', " ".join(dbfiles))))
+
         print "dak database schema at %d" % database_revision
         print "dak version requires schema %d"  % required_database_schema
 
-        if database_revision == required_database_schema:
+        if database_revision < required_database_schema:
+            print "\nUpdates to be applied:"
+            for i in range(database_revision, required_database_schema):
+                i += 1
+                dakdb = __import__("dakdb", globals(), locals(), ['update'+str(i)])
+                update_module = getattr(dakdb, "update"+str(i))
+                print "Update %d: %s" % (i, next(s for s in update_module.__doc__.split("\n") if s))
+                modules.append((update_module, i))
+            prompt = "\nUpdate database? (y/N) "
+            answer = utils.our_raw_input(prompt)
+            if answer.upper() != 'Y':
+                sys.exit(0)
+        else:
             print "no updates required"
+            logger.log(["no updates required"])
             sys.exit(0)
 
-        for i in range (database_revision, required_database_schema):
-            print "updating database schema from %d to %d" % (database_revision, i+1)
+        for module in modules:
+            (update_module, i) = module
             try:
-                dakdb = __import__("dakdb", globals(), locals(), ['update'+str(i+1)])
-                update_module = getattr(dakdb, "update"+str(i+1))
                 update_module.do_update(self)
-            except DBUpdateError, e:
+                message = "updated database schema from %d to %d" % (database_revision, i)
+                print message
+                logger.log([message])
+            except DBUpdateError as e:
                 # Seems the update did not work.
-                print "Was unable to update database schema from %d to %d." % (database_revision, i+1)
+                print "Was unable to update database schema from %d to %d." % (database_revision, i)
                 print "The error message received was %s" % (e)
+                logger.log(["DB Schema upgrade failed"])
+                logger.close()
                 utils.fubar("DB Schema upgrade failed")
             database_revision += 1
+        logger.close()
 
 ################################################################################
 
@@ -164,9 +206,9 @@ Updates dak's database schema to the lastest version. You should disable crontab
             if not cnf.has_key("Update-DB::Options::%s" % (i)):
                 cnf["Update-DB::Options::%s" % (i)] = ""
 
-        arguments = apt_pkg.ParseCommandLine(cnf.Cnf, arguments, sys.argv)
+        arguments = apt_pkg.parse_commandline(cnf.Cnf, arguments, sys.argv)
 
-        options = cnf.SubTree("Update-DB::Options")
+        options = cnf.subtree("Update-DB::Options")
         if options["Help"]:
             self.usage()
         elif arguments:
@@ -174,9 +216,12 @@ Updates dak's database schema to the lastest version. You should disable crontab
             self.usage(exit_code=1)
 
         try:
-            lock_fd = os.open(cnf["Dinstall::LockFile"], os.O_RDWR | os.O_CREAT)
-            fcntl.lockf(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
-        except IOError, e:
+            if os.path.isdir(cnf["Dir::Lock"]):
+                lock_fd = os.open(os.path.join(cnf["Dir::Lock"], 'dinstall.lock'), os.O_RDWR | os.O_CREAT)
+                fcntl.lockf(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
+            else:
+                utils.warn("Lock directory doesn't exist yet - not locking")
+        except IOError as e:
             if errno.errorcode[e.errno] == 'EACCES' or errno.errorcode[e.errno] == 'EAGAIN':
                 utils.fubar("Couldn't obtain lock; assuming another 'dak process-unchecked' is already running.")