From: Michael Casadevall Date: Tue, 30 Dec 2008 02:35:04 +0000 (-0500) Subject: Added update system for the database, and the first update script X-Git-Url: https://git.decadent.org.uk/gitweb/?p=dak.git;a=commitdiff_plain;h=a10398019d83fb13fbe230c944ed51470a35df34 Added update system for the database, and the first update script Signed-off-by: Michael Casadevall --- diff --git a/ChangeLog b/ChangeLog index d656a267..bb145f4b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,15 @@ * dak/process_unchecked.py - Added new REJECT for DM-Upload-Allowed not being set and clarified NMU reject message. + * dak/update_db.py - Added a update-database mechanism. New database updates + can be added by simply adding a simple upgrade script to dakdb + It probably has some bugs, but it can update git HEAD 12-08-2008 + to DB revision 1 without any issues. + + * dak/dakdb/1.py - Adds DM tables + + * dak/import_keyring - Fixed an argument typo on the help screen + 2008-12-28 Frank Lichtenheld * dak/override.py (main): Handle source-only packages better diff --git a/dak/dak.py b/dak/dak.py index 9dfd026b..e8a7df03 100755 --- a/dak/dak.py +++ b/dak/dak.py @@ -144,6 +144,8 @@ def init(): "Sync PostgreSQL users with passwd file"), ("init-db", "Update the database to match the conf file"), + ("update-db", + "Updates databae schema to latest revision"), ("init-dirs", "Initial setup of the archive"), ("make-maintainers", diff --git a/dak/dakdb/__init__.py b/dak/dakdb/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/dak/dakdb/update1.py b/dak/dakdb/update1.py new file mode 100644 index 00000000..a7ceb20a --- /dev/null +++ b/dak/dakdb/update1.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python + +# Debian Archive Kit Database Update Script +# Copyright (C) 2008 Michael Casadevall + +# 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 +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +################################################################################ + +# really, if we want to screw ourselves, let's find a better way. +# rm -rf /srv/ftp.debian.org + +################################################################################ + +import psycopg2 + +################################################################################ + +def do_update(self): + print "Adding DM fields to database" + + try: + c = self.db.cursor() + c.execute("ALTER TABLE source ADD COLUMN dm_upload_allowed BOOLEAN DEFAULT 'no' NOT NULL;") + c.execute("ALTER TABLE uid ADD COLUMN debian_maintainer BOOLEAN DEFAULT 'false' NOT NULL;") + + print "Migrating DM data to source table. This might take some time ..." + + c.execute("UPDATE source SET dm_upload_allowed = 't' WHERE id = (SELECT source FROM src_uploaders);") + c.execute("UPDATE config SET value = '1' WHERE name = 'db_revision'") + self.db.commit() + + print "REMINDER: Remember to run the updated byhand-dm crontab to update Debian Maintainer information" + + except psycopg2.ProgrammingError, msg: + self.db.rollback() + print "FATAL: Unable to apply DM table update 1!" + print "Error Message: " + str(msg) + print "Database changes have been rolled back." diff --git a/dak/import_keyring.py b/dak/import_keyring.py index d6bdde9a..25f72e40 100755 --- a/dak/import_keyring.py +++ b/dak/import_keyring.py @@ -173,7 +173,7 @@ def usage (exit_code=0): -h, --help show this help and exit. -L, --import-ldap-users generate uid entries for keyring from LDAP -U, --generate-users FMT generate uid entries from keyring as FMT""" - -d, --debian-maintainer mark generated uids as debian-maintainers + -D, --debian-maintainer mark generated uids as debian-maintainers sys.exit(exit_code) diff --git a/dak/update_db.py b/dak/update_db.py new file mode 100755 index 00000000..cda7aeb3 --- /dev/null +++ b/dak/update_db.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python + +# Debian Archive Kit Database Update Script +# Copyright (C) 2008 Michael Casadevall + +# 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 +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +################################################################################ + +# when do you have it written? +# Ganneff, after you make my debian account +# blackmail wont work +# damn it + +################################################################################ + +import psycopg2, sys +import apt_pkg +import time +from daklib import database +from daklib import utils + +################################################################################ + +Cnf = None +projectB = None +required_database_schema = 1 + +################################################################################ + +class UpdateDB: + def usage (self, exit_code=0): + print """Usage: dak update-db +Updates dak's database schema to the lastest version. You should disable crontabs while this is running + + -h, --help show this help and exit.""" + sys.exit(exit_code) + + +################################################################################ + def update_db_to_zero(self): + # This function will attempt to update a pre-zero database schema to zero + + # First, do the sure thing, and create the configuration table + try: + print "Creating configuration table ..." + c = self.db.cursor() + c.execute("""CREATE TABLE config ( + id SERIAL PRIMARY KEY NOT NULL, + name TEXT UNIQUE NOT NULL, + value TEXT + );""") + c.execute("INSERT INTO config VALUES ( nextval('config_id_seq'), 'db_revision', '0')"); + self.db.commit() + + except psycopg2.ProgrammingError: + self.db.rollback() + print "Failed to create configuration table." + print "Can the projectB user CREATE TABLE?" + print "" + print "Aborting update." + sys.exit(-255) + +################################################################################ + + 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';"); + return c.fetchone()[0] + + except psycopg2.ProgrammingError: + # Whoops .. no config table ... + self.db.rollback() + print "No configuration table found, assuming dak database revision to be pre-zero" + return -1 + +################################################################################ + + def update_db(self): + # Ok, try and find the configuration table + print "Determining dak database revision ..." + + try: + self.db = psycopg2.connect("dbname='" + Cnf["DB::Name"] + "' host='" + Cnf["DB::Host"] + "' port='" + str(Cnf["DB::Port"]) + "'") + + except: + print "FATAL: Failed connect to database" + pass + + database_revision = int(self.get_db_rev()) + + if database_revision == -1: + print "dak database schema predates update-db." + print "" + print "This script will attempt to upgrade it to the lastest, but may fail." + 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) + print "" + print "Attempting to upgrade pre-zero database to zero" + + 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) + + 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) + database_revision /+ 1 + +################################################################################ + + def init (self): + global Cnf, projectB + + Cnf = utils.get_conf() + 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)] = "" + + arguments = apt_pkg.ParseCommandLine(Cnf, arguments, sys.argv) + + options = Cnf.SubTree("Update-DB::Options") + if options["Help"]: + usage() + elif arguments: + utils.warn("dak update-db takes no arguments.") + usage(exit_code=1) + + self.update_db() + +################################################################################ + +if __name__ == '__main__': + app = UpdateDB() + app.init() + +def main(): + app = UpdateDB() + app.init() diff --git a/setup/init_pool.sql b/setup/init_pool.sql index bbb82e74..1e363940 100644 --- a/setup/init_pool.sql +++ b/setup/init_pool.sql @@ -38,7 +38,6 @@ CREATE TABLE uid ( id SERIAL PRIMARY KEY, uid TEXT UNIQUE NOT NULL, name TEXT - debian_maintainer BOOLEAN NOT NULL, ); CREATE TABLE keyrings ( @@ -85,7 +84,6 @@ CREATE TABLE source ( file INT4 UNIQUE NOT NULL, -- REFERENCES files install_date TIMESTAMP NOT NULL, sig_fpr INT4 NOT NULL, -- REFERENCES fingerprint - dm-upload-allowed BOOLEAN NOT NULL, unique (source, version) );