# HG changeset patch # User osimons # Date 1251214548 0 # Node ID 5467873fc0dca6afea245fa50753c086410a7e4f # Parent 731b2edbd0dc612022fd450ea286aede6360ad3d 0.6dev: Fix for #214 whereby creating the new index in the upgrade would fail if duplicates aleady exist (`UNIQUE`). In case of duplicates, the duplicates are now printed with information on how to resolve manually. Re-closes #214. Thanks to Hodgestar for providing the patch! diff --git a/bitten/upgrades.py b/bitten/upgrades.py --- a/bitten/upgrades.py +++ b/bitten/upgrades.py @@ -14,6 +14,7 @@ import os import sys +from trac.core import TracError from trac.db import DatabaseManager from trac.util.text import to_unicode import codecs @@ -361,9 +362,41 @@ def add_config_platform_rev_index_to_build(env, db): """Adds a unique index on (config, platform, rev) to the bitten_build table. Also drops the old index on bitten_build that serves no real purpose anymore.""" - cursor = db.cursor() - cursor.execute("CREATE UNIQUE INDEX bitten_build_config_rev_platform_idx ON bitten_build (config,rev,platform)") - cursor.execute("DROP INDEX bitten_build_config_rev_slave_idx") + # check for existing duplicates + duplicates_cursor = db.cursor() + build_cursor = db.cursor() + + duplicates_cursor.execute("SELECT config, rev, platform FROM bitten_build GROUP BY config, rev, platform HAVING COUNT(config) > 1") + duplicates_exist = False + for config, rev, platform in duplicates_cursor.fetchall(): + if not duplicates_exist: + duplicates_exist = True + print "\nConfig Name, Revision, Platform :: []" + print "--------------------------------------------------------" + + build_cursor.execute("SELECT id FROM bitten_build WHERE config='%s' AND rev='%s' AND platform='%s'" % (config, rev, platform)) + build_ids = [row[0] for row in build_cursor.fetchall()] + print "%s, %s, %s :: %s" % (config, rev, platform, build_ids) + + if duplicates_exist: + print "--------------------------------------------------------\n" + print "Duplicate builds found. You can remove the builds you don't want to" + print "keep by using this one-line command:\n" + print "$ python -c \"from bitten.model import Build; from trac.env import Environment; " \ + "Build.fetch(Environment('%s'), ).delete()\"" % env.path + print "\n...where is the id of the build to remove.\n" + print "Upgrades cannot be performed until conflicts are resolved." + print "The upgrade script will now exit with an error:\n" + + duplicates_cursor.close() + build_cursor.close() + + if not duplicates_exist: + cursor = db.cursor() + cursor.execute("CREATE UNIQUE INDEX bitten_build_config_rev_platform_idx ON bitten_build (config,rev,platform)") + cursor.execute("DROP INDEX bitten_build_config_rev_slave_idx") + else: + raise TracError('') map = { 2: [add_log_table],