]> git.decadent.org.uk Git - dak.git/blobdiff - dak/update_db.py
update-db: automatically find the schema nunmber to upgrade to
[dak.git] / dak / update_db.py
index 62f0889b8abe93713851731ff0051e1f742c3161..6f162cbfff98447b05e2fea8bf9976f696be1991 100755 (executable)
@@ -37,6 +37,8 @@ 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
@@ -46,7 +48,6 @@ from daklib.daklog import Logger
 ################################################################################
 
 Cnf = None
-required_database_schema = 64
 
 ################################################################################
 
@@ -134,7 +135,7 @@ Updates dak's database schema to the lastest version. You should disable crontab
 
             self.db = psycopg2.connect(connect_str)
 
-        except Exception, e:
+        except Exception as e:
             print "FATAL: Failed connect to database (%s)" % str(e)
             sys.exit(1)
 
@@ -155,10 +156,18 @@ 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 = int(max(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:
+            prompt = "Update 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)
@@ -171,7 +180,7 @@ Updates dak's database schema to the lastest version. You should disable crontab
                 message = "updated database schema from %d to %d" % (database_revision, i+1)
                 print message
                 logger.log([message])
-            except DBUpdateError, e:
+            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 "The error message received was %s" % (e)
@@ -190,9 +199,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:
@@ -205,7 +214,7 @@ Updates dak's database schema to the lastest version. You should disable crontab
                 fcntl.lockf(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
             else:
                 utils.warn("Lock directory doesn't exist yet - not locking")
-        except IOError, e:
+        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.")