Repository: ambari Updated Branches: refs/heads/branch-2.0.maint 524a8e303 -> a21a69e73
AMBARI-11063. Can not upgrade ambari 2.0.0->2.0.1 on sles113 (aonishuk) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/a21a69e7 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/a21a69e7 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/a21a69e7 Branch: refs/heads/branch-2.0.maint Commit: a21a69e73418031c5a32b213c1fd6202d86ca1a2 Parents: 524a8e3 Author: Andrew Onishuk <aonis...@hortonworks.com> Authored: Tue May 12 15:17:29 2015 +0300 Committer: Andrew Onishuk <aonis...@hortonworks.com> Committed: Tue May 12 15:17:29 2015 +0300 ---------------------------------------------------------------------- .../apache/ambari/server/orm/DBAccessor.java | 11 ++++ .../ambari/server/orm/DBAccessorImpl.java | 21 +++++++- .../server/upgrade/UpgradeCatalog200.java | 55 +++++++++++++++----- .../server/upgrade/UpgradeCatalog200Test.java | 2 + 4 files changed, 74 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/a21a69e7/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java index 2b01c72..c39a806 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java @@ -352,6 +352,17 @@ public interface DBAccessor { public DatabaseSession getNewDatabaseSession(); /** + * Gets list of index names from database metadata + * @param tableName + * the name of the table (not {@code null}). + * @param unique + * list only unique indexes (not {@code null}). + * @return the string list of index names + * @throws SQLException + */ + public List<String> getIndexesList(String tableName, boolean unique) throws SQLException; + + /** * Gets the column's SQL type * * @param tableName http://git-wip-us.apache.org/repos/asf/ambari/blob/a21a69e7/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java index 279c78f..b40ca27 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessorImpl.java @@ -180,7 +180,7 @@ public class DBAccessorImpl implements DBAccessor { boolean result = false; DatabaseMetaData metaData = getDatabaseMetaData(); - ResultSet res = metaData.getTables(null, null, convertObjectName(tableName), new String[] { "TABLE" }); + ResultSet res = metaData.getTables(null, null, convertObjectName(tableName), new String[]{"TABLE"}); if (res != null) { try { @@ -335,7 +335,7 @@ public class DBAccessorImpl implements DBAccessor { String referenceColumn, boolean ignoreFailure) throws SQLException { addFKConstraint(tableName, constraintName, new String[]{keyColumn}, referenceTableName, - new String[]{referenceColumn}, false, ignoreFailure); + new String[]{referenceColumn}, false, ignoreFailure); } @Override public void addFKConstraint(String tableName, String constraintName, @@ -627,6 +627,23 @@ public class DBAccessorImpl implements DBAccessor { return new DatabaseSessionImpl(login); } + @Override + public List<String> getIndexesList(String tableName, boolean unique) + throws SQLException{ + ResultSet rs = getDatabaseMetaData().getIndexInfo(null, null, tableName, unique, false); + List<String> index_list = new ArrayList<String>(); + if (rs != null){ + try{ + while (rs.next()) { + index_list.add(rs.getString("index_name")); + } + }finally { + rs.close(); + } + } + return index_list; + } + public int getColumnType(String tableName, String columnName) throws SQLException { String query = "SELECT " + columnName + " FROM " + tableName; http://git-wip-us.apache.org/repos/asf/ambari/blob/a21a69e7/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog200.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog200.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog200.java index dd6d6dd..4e444b9 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog200.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog200.java @@ -18,6 +18,7 @@ package org.apache.ambari.server.upgrade; +import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; @@ -137,15 +138,15 @@ public class UpgradeCatalog200 extends AbstractUpgradeCatalog { // add security_type to clusters dbAccessor.addColumn("clusters", new DBColumnInfo( - "security_type", String.class, 32, SecurityType.NONE.toString(), false)); + "security_type", String.class, 32, SecurityType.NONE.toString(), false)); // add security_state to various tables dbAccessor.addColumn("hostcomponentdesiredstate", new DBColumnInfo( - "security_state", String.class, 32, SecurityState.UNSECURED.toString(), false)); + "security_state", String.class, 32, SecurityState.UNSECURED.toString(), false)); dbAccessor.addColumn("hostcomponentstate", new DBColumnInfo( - "security_state", String.class, 32, SecurityState.UNSECURED.toString(), false)); + "security_state", String.class, 32, SecurityState.UNSECURED.toString(), false)); dbAccessor.addColumn("servicedesiredstate", new DBColumnInfo( - "security_state", String.class, 32, SecurityState.UNSECURED.toString(), false)); + "security_state", String.class, 32, SecurityState.UNSECURED.toString(), false)); // Alter column : make viewinstanceproperty.value & viewinstancedata.value // nullable @@ -156,6 +157,26 @@ public class UpgradeCatalog200 extends AbstractUpgradeCatalog { } /** + * Add new sequence to ambari_sequences. + * @param seqName name of sequence + * @param seqDefaultValue initial value for the sequence + * @throws SQLException + */ + private void addSequence(String seqName, int seqDefaultValue) throws SQLException{ + // check if sequence is already in the database + ResultSet rs = dbAccessor.executeSelect(String.format("SELECT COUNT(*) from ambari_sequences where sequence_name='%s'", seqName)); + if (rs != null) { + try { + if (rs.next() && rs.getInt(1) == 0) { + dbAccessor.executeQuery(String.format("INSERT INTO ambari_sequences(sequence_name, sequence_value) VALUES('%s', %d)", seqName, seqDefaultValue), false); + } + } finally { + rs.close(); + } + } + } + + /** * Execute all of the alert DDL updates. * * @throws AmbariException @@ -201,9 +222,16 @@ public class UpgradeCatalog200 extends AbstractUpgradeCatalog { columns.add(new DBColumnInfo("upgrade_package", String.class, 255, null, false)); columns.add(new DBColumnInfo("repositories", char[].class, null, null, false)); dbAccessor.createTable("repo_version", columns, "repo_version_id"); - dbAccessor.executeQuery("INSERT INTO ambari_sequences(sequence_name, sequence_value) VALUES('repo_version_id_seq', 0)", false); - dbAccessor.executeQuery("ALTER TABLE repo_version ADD CONSTRAINT UQ_repo_version_display_name UNIQUE (display_name)"); - dbAccessor.executeQuery("ALTER TABLE repo_version ADD CONSTRAINT UQ_repo_version_stack_version UNIQUE (stack, version)"); + + addSequence("repo_version_id_seq", 0); + + List<String> indexes = dbAccessor.getIndexesList("repo_version", true); + if (!indexes.contains("uq_repo_version_display_name")) { + dbAccessor.executeQuery("ALTER TABLE repo_version ADD CONSTRAINT UQ_repo_version_display_name UNIQUE (display_name)"); + } + if (!indexes.contains("uq_repo_version_stack_version")) { + dbAccessor.executeQuery("ALTER TABLE repo_version ADD CONSTRAINT UQ_repo_version_stack_version UNIQUE (stack, version)"); + } // New columns dbAccessor.addColumn("hostcomponentstate", new DBAccessor.DBColumnInfo("upgrade_state", @@ -216,7 +244,7 @@ public class UpgradeCatalog200 extends AbstractUpgradeCatalog { Integer.class, 1, 0, false)); dbAccessor.addColumn("stage", new DBAccessor.DBColumnInfo("skippable", - Integer.class, 1, 0, false)); + Integer.class, 1, 0, false)); // New tables columns = new ArrayList<DBColumnInfo>(); @@ -243,8 +271,8 @@ public class UpgradeCatalog200 extends AbstractUpgradeCatalog { dbAccessor.addFKConstraint("host_version", "FK_host_version_repovers_id", "repo_version_id", "repo_version", "repo_version_id", false); // New sequences - dbAccessor.executeQuery("INSERT INTO ambari_sequences(sequence_name, sequence_value) VALUES('cluster_version_id_seq', 0)", false); - dbAccessor.executeQuery("INSERT INTO ambari_sequences(sequence_name, sequence_value) VALUES('host_version_id_seq', 0)", false); + addSequence("cluster_version_id_seq", 0); + addSequence("host_version_id_seq", 0); // upgrade tables columns = new ArrayList<DBColumnInfo>(); @@ -257,7 +285,8 @@ public class UpgradeCatalog200 extends AbstractUpgradeCatalog { dbAccessor.createTable("upgrade", columns, "upgrade_id"); dbAccessor.addFKConstraint("upgrade", "fk_upgrade_cluster_id", "cluster_id", "clusters", "cluster_id", false); dbAccessor.addFKConstraint("upgrade", "fk_upgrade_request_id", "request_id", "request", "request_id", false); - dbAccessor.executeQuery("INSERT INTO ambari_sequences(sequence_name, sequence_value) VALUES('upgrade_id_seq', 0)", false); + addSequence("upgrade_id_seq", 0); + columns = new ArrayList<DBColumnInfo>(); columns.add(new DBAccessor.DBColumnInfo("upgrade_group_id", Long.class, null, null, false)); @@ -266,7 +295,7 @@ public class UpgradeCatalog200 extends AbstractUpgradeCatalog { columns.add(new DBAccessor.DBColumnInfo("group_title", String.class, 1024, "", false)); dbAccessor.createTable("upgrade_group", columns, "upgrade_group_id"); dbAccessor.addFKConstraint("upgrade_group", "fk_upgrade_group_upgrade_id", "upgrade_id", "upgrade", "upgrade_id", false); - dbAccessor.executeQuery("INSERT INTO ambari_sequences(sequence_name, sequence_value) VALUES('upgrade_group_id_seq', 0)", false); + addSequence("upgrade_group_id_seq", 0); columns = new ArrayList<DBColumnInfo>(); @@ -279,7 +308,7 @@ public class UpgradeCatalog200 extends AbstractUpgradeCatalog { columns.add(new DBAccessor.DBColumnInfo("item_text", String.class, 1024, null, true)); dbAccessor.createTable("upgrade_item", columns, "upgrade_item_id"); dbAccessor.addFKConstraint("upgrade_item", "fk_upg_item_upgrade_group_id", "upgrade_group_id", "upgrade_group", "upgrade_group_id", false); - dbAccessor.executeQuery("INSERT INTO ambari_sequences(sequence_name, sequence_value) VALUES('upgrade_item_id_seq', 0)", false); + addSequence("upgrade_item_id_seq", 0); } private void createArtifactTable() throws SQLException { http://git-wip-us.apache.org/repos/asf/ambari/blob/a21a69e7/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog200Test.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog200Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog200Test.java index 76552bc..beb9516 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog200Test.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog200Test.java @@ -39,6 +39,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -128,6 +129,7 @@ public class UpgradeCatalog200Test { ResultSet resultSet = createNiceMock(ResultSet.class); expect(configuration.getDatabaseUrl()).andReturn(Configuration.JDBC_IN_MEMORY_URL).anyTimes(); + expect(dbAccessor.getIndexesList("repo_version", Boolean.TRUE)).andReturn(new ArrayList<String>()).anyTimes(); Capture<DBAccessor.DBColumnInfo> alertDefinitionIgnoreColumnCapture = new Capture<DBAccessor.DBColumnInfo>(); Capture<DBAccessor.DBColumnInfo> alertDefinitionDescriptionColumnCapture = new Capture<DBAccessor.DBColumnInfo>();