X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=dak%2Fupdate_db.py;h=8bb88f65c41a71685a7b2aff385bd82fba374992;hb=cd5b29ddfd8de263c085f494b9573d683913f6f3;hp=33d3ebb9e64a2d94239f6ba5e37da75bc52a35fd;hpb=e4d65e532a06cc2785514cb9c5ebed5e1ec97cc7;p=dak.git diff --git a/dak/update_db.py b/dak/update_db.py index 33d3ebb9..8bb88f65 100755 --- a/dak/update_db.py +++ b/dak/update_db.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -# Debian Archive Kit Database Update Script +""" Database Update Main Script """ # Copyright (C) 2008 Michael Casadevall # This program is free software; you can redistribute it and/or modify @@ -26,9 +26,10 @@ ################################################################################ -import psycopg2, sys +import psycopg2, sys, fcntl, os import apt_pkg import time +import errno from daklib import database from daklib import utils @@ -36,7 +37,7 @@ from daklib import utils Cnf = None projectB = None -required_database_schema = 1 +required_database_schema = 4 ################################################################################ @@ -52,7 +53,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 +64,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: @@ -84,7 +85,7 @@ Updates dak's database schema to the lastest version. You should disable crontab 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 +101,12 @@ 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"]) + 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" @@ -115,7 +121,7 @@ Updates dak's database schema to the lastest version. You should disable crontab print "Please make sure you have a database backup handy. If you don't, press Ctrl-C now!" print "" print "Continuing in five seconds ..." - #time.sleep(5) + time.sleep(5) print "" print "Attempting to upgrade pre-zero database to zero" @@ -134,7 +140,7 @@ Updates dak's database schema to the lastest version. You should disable crontab dakdb = __import__("dakdb", globals(), locals(), ['update'+str(i+1)]) update_module = getattr(dakdb, "update"+str(i+1)) update_module.do_update(self) - database_revision =+ 1 + database_revision += 1 ################################################################################ @@ -151,13 +157,22 @@ 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() + 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 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.") + + ################################################################################ if __name__ == '__main__':