X-Git-Url: https://git.decadent.org.uk/gitweb/?p=dak.git;a=blobdiff_plain;f=dak%2Fupdate_db.py;h=61a1089210295e5ab759d7858e59edc5f4bfb58e;hp=89fc174ecd0f2857be05a0b3ded4fcc2400acc00;hb=17c5cab4eb8d5181ec7a81267a4e2e6b43c0fc65;hpb=18e927e99a111f93e75ec99fba7c8024a27fe733 diff --git a/dak/update_db.py b/dak/update_db.py index 89fc174e..61a10892 100755 --- a/dak/update_db.py +++ b/dak/update_db.py @@ -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 = 67 ################################################################################ @@ -120,6 +121,7 @@ Updates dak's database schema to the lastest version. You should disable crontab print "Determining dak database revision ..." cnf = Config() logger = Logger('update-db') + modules = [] try: # Build a connect string @@ -134,7 +136,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,25 +157,39 @@ 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): + 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) - message = "updated database schema from %d to %d" % (database_revision, i+1) + message = "updated database schema from %d to %d" % (database_revision, i) 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 "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() @@ -190,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: @@ -205,7 +221,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.")