X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=dak%2Fupdate_db.py;h=f35521f49c61d50bd5a6c5129569d66e3eefd53d;hb=731c2232d9964d0f441ff5ea386e5e9937aa77ab;hp=dc943503900a672920063f8fca40189d4a81d9e2;hpb=142e68f49635eca4e67f0aa9782c960b6cb734d2;p=dak.git diff --git a/dak/update_db.py b/dak/update_db.py index dc943503..f35521f4 100755 --- a/dak/update_db.py +++ b/dak/update_db.py @@ -1,7 +1,11 @@ #!/usr/bin/env python -# Debian Archive Kit Database Update 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,17 +30,21 @@ ################################################################################ -import psycopg2, sys, fcntl, os +import psycopg2 +import sys +import fcntl +import os import apt_pkg import time -from daklib import database +import errno + from daklib import utils +from daklib.dak_exceptions import DBUpdateError ################################################################################ Cnf = None -projectB = None -required_database_schema = 1 +required_database_schema = 15 ################################################################################ @@ -52,7 +60,7 @@ Updates dak's database schema to the lastest version. You should disable crontab ################################################################################ def update_db_to_zero(self): - # This function will attempt to update a pre-zero database schema to zero + """ This function will attempt to update a pre-zero database schema to zero """ # First, do the sure thing, and create the configuration table try: @@ -63,7 +71,7 @@ Updates dak's database schema to the lastest version. You should disable crontab name TEXT UNIQUE NOT NULL, value TEXT );""") - c.execute("INSERT INTO config VALUES ( nextval('config_id_seq'), 'db_revision', '0')"); + c.execute("INSERT INTO config VALUES ( nextval('config_id_seq'), 'db_revision', '0')") self.db.commit() except psycopg2.ProgrammingError: @@ -77,14 +85,12 @@ 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 try: c = self.db.cursor() - q = c.execute("SELECT value FROM config WHERE name = 'db_revision';"); + q = c.execute("SELECT value FROM config WHERE name = 'db_revision';") return c.fetchone()[0] except psycopg2.ProgrammingError: @@ -100,7 +106,13 @@ Updates dak's database schema to the lastest version. You should disable crontab print "Determining dak database revision ..." try: - self.db = psycopg2.connect("dbname='" + Cnf["DB::Name"] + "' host='" + Cnf["DB::Host"] + "' port='" + str(Cnf["DB::Port"]) + "'") + # Build a connect string +# connect_str = "dbname=%s"% (Cnf["DB::Name"]) + connect_str = "dbname=%s"% "projectbstew" +# 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) except: print "FATAL: Failed connect to database" @@ -130,16 +142,22 @@ Updates dak's database schema to the lastest version. You should disable crontab 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 " + str(database_revision) + " to " + str(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 %s to %s." % (str(database_revision), str(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 + global Cnf Cnf = utils.get_conf() arguments = [('h', "help", "Update-DB::Options::Help")] @@ -151,10 +169,10 @@ Updates dak's database schema to the lastest version. You should disable crontab options = Cnf.SubTree("Update-DB::Options") if options["Help"]: - usage() + self.usage() elif arguments: utils.warn("dak update-db takes no arguments.") - usage(exit_code=1) + self.usage(exit_code=1) self.update_db()