X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=dak%2Fupdate_db.py;h=ecdd99a79bf2ef0d76d96be91ba446c27ce8ec30;hb=c55d086c0c8463fcd2d8dcee2dcc6c414ee36d91;hp=8bb88f65c41a71685a7b2aff385bd82fba374992;hpb=cd5b29ddfd8de263c085f494b9573d683913f6f3;p=dak.git diff --git a/dak/update_db.py b/dak/update_db.py index 8bb88f65..ecdd99a7 100755 --- a/dak/update_db.py +++ b/dak/update_db.py @@ -1,7 +1,11 @@ #!/usr/bin/env python -""" Database Update Main Script """ +""" Database Update Main Script + +@contact: Debian FTP Master # Copyright (C) 2008 Michael Casadevall +@license: GNU General Public License version 2 or later +""" # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -26,18 +30,22 @@ ################################################################################ -import psycopg2, sys, fcntl, os +import psycopg2 +import sys +import fcntl +import os import apt_pkg import time import errno -from daklib import database + from daklib import utils +from daklib.config import Config +from daklib.dak_exceptions import DBUpdateError ################################################################################ Cnf = None -projectB = None -required_database_schema = 4 +required_database_schema = 24 ################################################################################ @@ -78,8 +86,6 @@ Updates dak's database schema to the lastest version. You should disable crontab ################################################################################ def get_db_rev(self): - global projectB - # We keep database revision info the config table # Try and access it @@ -99,12 +105,13 @@ Updates dak's database schema to the lastest version. You should disable crontab def update_db(self): # Ok, try and find the configuration table print "Determining dak database revision ..." + cnf = Config() 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"])) + 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"])) self.db = psycopg2.connect(connect_str) @@ -128,50 +135,53 @@ Updates dak's database schema to the lastest version. You should disable crontab self.update_db_to_zero() database_revision = 0 - print "dak database schema at " + str(database_revision) - print "dak version requires schema " + str(required_database_schema) + print "dak database schema at %d" % database_revision + print "dak version requires schema %d" % required_database_schema if database_revision == required_database_schema: print "no updates required" sys.exit(0) for i in range (database_revision, required_database_schema): - print "updating databse schema from " + str(database_revision) + " to " + str(i+1) - dakdb = __import__("dakdb", globals(), locals(), ['update'+str(i+1)]) - update_module = getattr(dakdb, "update"+str(i+1)) - update_module.do_update(self) + print "updating database schema from %d to %d" % (database_revision, i+1) + 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: + # 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) + utils.fubar("DB Schema upgrade failed") database_revision += 1 ################################################################################ def init (self): - global Cnf, projectB - - Cnf = utils.get_conf() + cnf = Config() arguments = [('h', "help", "Update-DB::Options::Help")] for i in [ "help" ]: - if not Cnf.has_key("Update-DB::Options::%s" % (i)): - Cnf["Update-DB::Options::%s" % (i)] = "" + if not cnf.has_key("Update-DB::Options::%s" % (i)): + cnf["Update-DB::Options::%s" % (i)] = "" - arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv) + arguments = apt_pkg.ParseCommandLine(cnf.Cnf, arguments, sys.argv) - options = Cnf.SubTree("Update-DB::Options") + options = cnf.SubTree("Update-DB::Options") if options["Help"]: self.usage() elif arguments: utils.warn("dak update-db takes no arguments.") self.usage(exit_code=1) - - self.update_db() - try: - lock_fd = os.open(Cnf["Dinstall::LockFile"], os.O_RDWR | os.O_CREAT) + 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 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.") + self.update_db() + ################################################################################