modules/core/dbutils/pom.xml | 2 modules/core/dbutils/src/main/java/org/rhq/core/db/DbUtilsI18NResourceKeys.java | 5 modules/core/dbutils/src/main/java/org/rhq/core/db/upgrade/BundleVersionOrderUpgradeTask.java | 140 ++++++++++ modules/core/dbutils/src/main/scripts/dbupgrade/db-upgrade.xml | 4 4 files changed, 150 insertions(+), 1 deletion(-)
New commits: commit 9759daf74bf650e13764d80ef8fc4f67d1b8d269 Author: John Mazzitelli mazz@redhat.com Date: Thu Feb 28 15:26:10 2013 -0500
[BZ 916380] add java dbupgrade step to fix bundle version ordering
diff --git a/modules/core/dbutils/pom.xml b/modules/core/dbutils/pom.xml index 109eb3a..a48f2a4 100644 --- a/modules/core/dbutils/pom.xml +++ b/modules/core/dbutils/pom.xml @@ -17,7 +17,7 @@ <description>Database schema setup, upgrade and other utilities</description>
<properties> - <db.schema.version>2.129</db.schema.version> + <db.schema.version>2.130</db.schema.version> <rhq.ds.type-mapping>${rhq.test.ds.type-mapping}</rhq.ds.type-mapping> <rhq.ds.server-name>${rhq.test.ds.server-name}</rhq.ds.server-name> <rhq.ds.db-name>${rhq.test.ds.db-name}</rhq.ds.db-name> diff --git a/modules/core/dbutils/src/main/java/org/rhq/core/db/DbUtilsI18NResourceKeys.java b/modules/core/dbutils/src/main/java/org/rhq/core/db/DbUtilsI18NResourceKeys.java index a200076..f74a823 100644 --- a/modules/core/dbutils/src/main/java/org/rhq/core/db/DbUtilsI18NResourceKeys.java +++ b/modules/core/dbutils/src/main/java/org/rhq/core/db/DbUtilsI18NResourceKeys.java @@ -32,6 +32,11 @@ import mazz.i18n.annotation.I18NResourceBundle; */ @I18NResourceBundle(baseName = "dbutils-messages", defaultLocale = "en") public interface DbUtilsI18NResourceKeys { + @I18NMessages({ @I18NMessage("Error: {0}"), @I18NMessage(value = "Fehler: {0}", locale = "de") }) + String ERROR = "DatabaseType.generic-error"; + + @I18NMessages({ @I18NMessage("Message: {0}"), @I18NMessage(value = "Nachricht: {0}", locale = "de") }) + String MESSAGE = "DatabaseType.generic-message";
@I18NMessages( { @I18NMessage("Executing the following SQL: {0}"), @I18NMessage(value = "Führe das nachfolgnde SQL aus: {0}", locale = "de") }) diff --git a/modules/core/dbutils/src/main/java/org/rhq/core/db/upgrade/BundleVersionOrderUpgradeTask.java b/modules/core/dbutils/src/main/java/org/rhq/core/db/upgrade/BundleVersionOrderUpgradeTask.java new file mode 100644 index 0000000..e3b00c3 --- /dev/null +++ b/modules/core/dbutils/src/main/java/org/rhq/core/db/upgrade/BundleVersionOrderUpgradeTask.java @@ -0,0 +1,140 @@ +/* + * RHQ Management Platform + * Copyright (C) 2005-2008 Red Hat, Inc. + * All rights reserved. + * + * 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 version 2 of the License. + * + * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package org.rhq.core.db.upgrade; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import java.util.TreeMap; + +import mazz.i18n.Logger; + +import org.apache.maven.artifact.versioning.ComparableVersion; + +import org.rhq.core.db.DatabaseType; +import org.rhq.core.db.DbUtilsI18NFactory; +import org.rhq.core.db.DbUtilsI18NResourceKeys; + +/** + * Database upgrade task 2.130 to fix BZ 916380 where bundle version orders got disordered when deleting bundles. + * + * @author John Mazzitelli + */ +public class BundleVersionOrderUpgradeTask implements DatabaseUpgradeTask { + + private static final Logger LOG = DbUtilsI18NFactory.getLogger(BundleVersionOrderUpgradeTask.class); + + public void execute(DatabaseType databaseType, Connection connection) throws SQLException { + List<Integer> bundleIds = getAllBundleIds(databaseType, connection); + if (bundleIds != null && !bundleIds.isEmpty()) { + for (Integer bundleId : bundleIds) { + List<VersionInfo> versionInfos = getBundleVersions(bundleId, databaseType, connection); + if (versionInfos != null && !versionInfos.isEmpty()) { + + // make sure we order the versions properly by comparing version strings + TreeMap<ComparableVersion, VersionInfo> ordered = new TreeMap<ComparableVersion, VersionInfo>(); + for (VersionInfo versionInfo : versionInfos) { + ComparableVersion comparableVersion = new ComparableVersion(versionInfo.version); + ordered.put(comparableVersion, versionInfo); + } + + // now make sure all version ordering is correct - starting from 0 and monotonically increasing + int expectedNext = 0; + for (VersionInfo orderedBundleVersion : ordered.values()) { + if (orderedBundleVersion.versionOrder != expectedNext) { + String sql = "UPDATE RHQ_BUNDLE_VERSION SET VERSION_ORDER=" + expectedNext + " WHERE ID=" + + orderedBundleVersion.id; + Statement s = null; + try { + LOG.debug(DbUtilsI18NResourceKeys.MESSAGE, + "Correcting version order for bundle version ID [" + orderedBundleVersion.id + "]"); + LOG.debug(DbUtilsI18NResourceKeys.EXECUTING_SQL, sql); + s = connection.createStatement(); + int rowsUpdated = s.executeUpdate(sql); + if (rowsUpdated != 1) { + LOG.error(DbUtilsI18NResourceKeys.ERROR, + "Failed to update version order for bundle version ID [" + + orderedBundleVersion.id + "]. Will continue but problems may persist."); + } + } finally { + databaseType.closeStatement(s); + } + } + expectedNext++; + } + } + } + } else { + LOG.debug(DbUtilsI18NResourceKeys.MESSAGE, "No bundles exist - nothing to fix."); + } + } + + private List<Integer> getAllBundleIds(DatabaseType databaseType, Connection connection) throws SQLException { + List<Integer> ids = new ArrayList<Integer>(); + Statement s = null; + try { + String sql = "SELECT ID FROM RHQ_BUNDLE"; + LOG.debug(DbUtilsI18NResourceKeys.EXECUTING_SQL, sql); + + s = connection.createStatement(); + ResultSet rs = s.executeQuery(sql); + + while (rs.next()) { + int id = rs.getInt(1); + ids.add(Integer.valueOf(id)); + } + } finally { + databaseType.closeStatement(s); + } + return ids; + } + + private List<VersionInfo> getBundleVersions(int bundleId, DatabaseType databaseType, Connection connection) + throws SQLException { + List<VersionInfo> infos = new ArrayList<VersionInfo>(); + Statement s = null; + try { + String sql = "SELECT ID, VERSION_ORDER, VERSION FROM RHQ_BUNDLE_VERSION WHERE BUNDLE_ID = " + bundleId; + LOG.debug(DbUtilsI18NResourceKeys.EXECUTING_SQL, sql); + + s = connection.createStatement(); + ResultSet rs = s.executeQuery(sql); + + while (rs.next()) { + VersionInfo info = new VersionInfo(); + info.id = rs.getInt(1); + info.versionOrder = rs.getInt(2); + info.version = rs.getString(3); + infos.add(info); + } + } finally { + databaseType.closeStatement(s); + } + return infos; + } + + private class VersionInfo { + int id; + int versionOrder; + String version; + } +} \ No newline at end of file diff --git a/modules/core/dbutils/src/main/scripts/dbupgrade/db-upgrade.xml b/modules/core/dbutils/src/main/scripts/dbupgrade/db-upgrade.xml index 936c7bf..33388ce 100644 --- a/modules/core/dbutils/src/main/scripts/dbupgrade/db-upgrade.xml +++ b/modules/core/dbutils/src/main/scripts/dbupgrade/db-upgrade.xml @@ -2010,6 +2010,10 @@ </schema-directSQL> </schemaSpec>
+ <!-- BZ 916380 --> + <schemaSpec version="2.130"> + <schema-javaTask className="BundleVersionOrderUpgradeTask" /> + </schemaSpec> </dbupgrade> </target>