AMBARI-21169. Service and Patch Upgrade Catalog Changes for 2.6.0 (dlysnichenko)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/2307c9d4 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/2307c9d4 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/2307c9d4 Branch: refs/heads/branch-2.6 Commit: 2307c9d49946b37015087b132911a6d0f054ef1d Parents: 6c5faee Author: Lisnichenko Dmitro <dlysniche...@hortonworks.com> Authored: Thu Aug 10 19:09:57 2017 +0300 Committer: Lisnichenko Dmitro <dlysniche...@hortonworks.com> Committed: Thu Aug 10 19:09:57 2017 +0300 ---------------------------------------------------------------------- .../apache/ambari/server/orm/DBAccessor.java | 45 +++ .../ambari/server/orm/DBAccessorImpl.java | 77 +++++ .../server/orm/helpers/dbms/DbmsHelper.java | 47 +++ .../orm/helpers/dbms/GenericDbmsHelper.java | 25 ++ .../server/orm/helpers/dbms/H2Helper.java | 29 ++ .../server/orm/helpers/dbms/MySqlHelper.java | 29 ++ .../server/orm/helpers/dbms/OracleHelper.java | 28 ++ .../server/orm/helpers/dbms/PostgresHelper.java | 27 ++ .../server/upgrade/SchemaUpgradeHelper.java | 1 + .../server/upgrade/UpgradeCatalog260.java | 318 +++++++++++++++++++ 10 files changed, 626 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/2307c9d4/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 0313698..8f1a192 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 @@ -681,6 +681,51 @@ public interface DBAccessor { String targetTableName, DBColumnInfo targetColumn, String targetIDFieldName, Object initialValue) throws SQLException; /** + * Copy column from {@code targetTable} by matching + * table keys {@code sourceIDColumnName} and {@code targetIDColumnName} + * and condition {@code sourceConditionFieldName} = {@code condition} + * + * @param sourceTableName the source table name + * @param sourceColumn the source column name + * @param sourceIDFieldName the source id key filed name matched with {@code targetIDFieldName} + * @param targetTableName the target table name + * @param targetColumn the target column name + * @param targetIDFieldName the target id key name matched with {@code sourceIDFieldName} + * @param sourceConditionFieldName source key column name which should match {@code condition} + * @param condition value which should match {@code sourceConditionFieldName} + * @param initialValue initial value for null-contained cells + * @throws SQLException + */ + void copyColumnToAnotherTable(String sourceTableName, DBColumnInfo sourceColumn, String sourceIDFieldName, + String targetTableName, DBColumnInfo targetColumn, String targetIDFieldName, + String sourceConditionFieldName, String condition, Object initialValue) throws SQLException; + + + /** + * Copy column from {@code targetTable} by matching + * table keys {@code sourceIDColumnName} and {@code targetIDColumnName} + * and condition {@code sourceConditionFieldName} = {@code condition} + * + * @param sourceTableName the source table name + * @param sourceColumn the source column name + * @param sourceIDFieldName1 the source id key filed name matched with {@code targetIDFieldName1} + * @param sourceIDFieldName2 the source id key filed name matched with {@code targetIDFieldName2} + * @param sourceIDFieldName3 the source id key filed name matched with {@code targetIDFieldName3} + * @param targetTableName the target table name + * @param targetColumn the target column name + * @param targetIDFieldName1 the target id key name matched with {@code sourceIDFieldName1} + * @param targetIDFieldName2 the target id key name matched with {@code sourceIDFieldName2} + * @param targetIDFieldName3 the target id key name matched with {@code sourceIDFieldName3} + * @param sourceConditionFieldName source key column name which should match {@code condition} + * @param condition value which should match {@code sourceConditionFieldName} + * @param initialValue initial value for null-contained cells + * @throws SQLException + */ + void copyColumnToAnotherTable(String sourceTableName, DBColumnInfo sourceColumn, String sourceIDFieldName1, String sourceIDFieldName2, String sourceIDFieldName3, + String targetTableName, DBColumnInfo targetColumn, String targetIDFieldName1, String targetIDFieldName2, String targetIDFieldName3, + String sourceConditionFieldName, String condition, Object initialValue) throws SQLException; + + /** * Remove all rows from the table * @param tableName name of the table */ http://git-wip-us.apache.org/repos/asf/ambari/blob/2307c9d4/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 38d60e9..d693809 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 @@ -1444,6 +1444,83 @@ public class DBAccessorImpl implements DBAccessor { } /** + * {@inheritDoc} + */ + @Override + public void copyColumnToAnotherTable(String sourceTableName, DBColumnInfo sourceColumn, String sourceIDFieldName, + String targetTableName, DBColumnInfo targetColumn, String targetIDFieldName, + String sourceConditionFieldName, String condition, Object initialValue) throws SQLException { + + if (tableHasColumn(sourceTableName, sourceIDFieldName) && + tableHasColumn(sourceTableName, sourceColumn.getName()) && + tableHasColumn(sourceTableName, sourceConditionFieldName) && + tableHasColumn(targetTableName, targetIDFieldName) + ) { + + final String moveSQL = dbmsHelper.getCopyColumnToAnotherTableStatement(sourceTableName, sourceColumn.getName(), + sourceIDFieldName, targetTableName, targetColumn.getName(), targetIDFieldName, sourceConditionFieldName, condition); + final boolean isTargetColumnNullable = targetColumn.isNullable(); + + targetColumn.setNullable(true); // setting column nullable by default to move rows with null + + addColumn(targetTableName, targetColumn); + executeUpdate(moveSQL, false); + + if (initialValue != null) { + String updateSQL = dbmsHelper.getColumnUpdateStatementWhereColumnIsNull(convertObjectName(targetTableName), + convertObjectName(targetColumn.getName()), convertObjectName(targetColumn.getName())); + + executePreparedUpdate(updateSQL, initialValue); + } + + if (!isTargetColumnNullable) { + setColumnNullable(targetTableName, targetColumn.getName(), false); + } + } + } + + /** + * {@inheritDoc} + */ + @Override + public void copyColumnToAnotherTable(String sourceTableName, DBColumnInfo sourceColumn, String sourceIDFieldName1, String sourceIDFieldName2, String sourceIDFieldName3, + String targetTableName, DBColumnInfo targetColumn, String targetIDFieldName1, String targetIDFieldName2, String targetIDFieldName3, + String sourceConditionFieldName, String condition, Object initialValue) throws SQLException { + + if (tableHasColumn(sourceTableName, sourceIDFieldName1) && + tableHasColumn(sourceTableName, sourceIDFieldName2) && + tableHasColumn(sourceTableName, sourceIDFieldName3) && + tableHasColumn(sourceTableName, sourceColumn.getName()) && + tableHasColumn(sourceTableName, sourceConditionFieldName) && + tableHasColumn(targetTableName, targetIDFieldName1) && + tableHasColumn(targetTableName, targetIDFieldName2) && + tableHasColumn(targetTableName, targetIDFieldName3) + ) { + + final String moveSQL = dbmsHelper.getCopyColumnToAnotherTableStatement(sourceTableName, sourceColumn.getName(), + sourceIDFieldName1, sourceIDFieldName2, sourceIDFieldName3, targetTableName, targetColumn.getName(), + targetIDFieldName1, targetIDFieldName2, targetIDFieldName3, sourceConditionFieldName, condition); + final boolean isTargetColumnNullable = targetColumn.isNullable(); + + targetColumn.setNullable(true); // setting column nullable by default to move rows with null + + addColumn(targetTableName, targetColumn); + executeUpdate(moveSQL, false); + + if (initialValue != null) { + String updateSQL = dbmsHelper.getColumnUpdateStatementWhereColumnIsNull(convertObjectName(targetTableName), + convertObjectName(targetColumn.getName()), convertObjectName(targetColumn.getName())); + + executePreparedUpdate(updateSQL, initialValue); + } + + if (!isTargetColumnNullable) { + setColumnNullable(targetTableName, targetColumn.getName(), false); + } + } + } + + /** * Remove all rows from the table * * @param tableName name of the table http://git-wip-us.apache.org/repos/asf/ambari/blob/2307c9d4/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/DbmsHelper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/DbmsHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/DbmsHelper.java index 00b02f1..603c33b 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/DbmsHelper.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/DbmsHelper.java @@ -159,6 +159,53 @@ public interface DbmsHelper { String targetTable, String targetColumnName, String targetIDColumnName); /** + * Get's the {@code UPDATE} statement for {@code sourceTable} for copy column from {@code targetTable} by matching + * table keys {@code sourceIDColumnName} and {@code targetIDColumnName} + * and condition {@code sourceConditionFieldName} = {@code condition} + * + * @param sourceTable the source table name + * @param sourceColumnName the source column name + * @param sourceIDColumnName source key id column which would be used to match right rows for {@code targetTable} + * @param targetTable the destination table name + * @param targetColumnName the destination column name + * @param targetIDColumnName destination key id column name which should match {@code sourceIDColumnName} + * @param sourceConditionFieldName source key column name which should match {@code condition} + * @param condition value which should match {@code sourceConditionFieldName} + * @return + */ + String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName, + String sourceIDColumnName, String targetTable, + String targetColumnName, String targetIDColumnName, + String sourceConditionFieldName, String condition); + + /** + * Get's the {@code UPDATE} statement for {@code sourceTable} for copy column from {@code targetTable} by matching + * table keys {@code sourceIDColumnName} and {@code targetIDColumnName} + * and condition {@code sourceConditionFieldName} = {@code condition} + * + * @param sourceTable the source table name + * @param sourceColumnName the source column name + * @param sourceIDColumnName1 source key id column which would be used to math right rows for {@code targetTable} + * @param sourceIDColumnName2 source key id column which would be used to math right rows for {@code targetTable} + * @param sourceIDColumnName3 source key id column which would be used to math right rows for {@code targetTable} + * @param targetTable the destination table name + * @param targetColumnName the destination column name + * @param targetIDColumnName1 destination key id column name which should match {@code sourceIDColumnName1} + * @param targetIDColumnName2 destination key id column name which should match {@code sourceIDColumnName1} + * @param targetIDColumnName3 destination key id column name which should match {@code sourceIDColumnName1} + * @param sourceConditionFieldName source key column name which should match {@code condition} + * @param condition value which should match {@code sourceConditionFieldName} + * @return + */ + String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName, + String sourceIDColumnName1, String sourceIDColumnName2, + String sourceIDColumnName3, + String targetTable, String targetColumnName, + String targetIDColumnName1, String targetIDColumnName2, + String targetIDColumnName3, + String sourceConditionFieldName, String condition); + + /** * Gets whether the database platform supports adding contraints after the * {@code NULL} constraint. Some database, such as Oracle, don't allow this. * Unfortunately, EclipsLink hard codes the order of constraints. http://git-wip-us.apache.org/repos/asf/ambari/blob/2307c9d4/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/GenericDbmsHelper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/GenericDbmsHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/GenericDbmsHelper.java index 11cbcf9..34358a9 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/GenericDbmsHelper.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/GenericDbmsHelper.java @@ -86,6 +86,31 @@ public class GenericDbmsHelper implements DbmsHelper { throw new UnsupportedOperationException("Column copy is not supported for generic DB"); } + /** + * {@inheritDoc} + */ + @Override + public String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName, + String sourceIDColumnName, String targetTable, + String targetColumnName, String targetIDColumnName, + String sourceConditionFieldName, String condition) { + throw new UnsupportedOperationException("Column copy is not supported for generic DB"); + } + + /** + * {@inheritDoc} + */ + @Override + public String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName, + String sourceIDColumnName1, String sourceIDColumnName2, + String sourceIDColumnName3, + String targetTable, String targetColumnName, + String targetIDColumnName1, String targetIDColumnName2, + String targetIDColumnName3, + String sourceConditionFieldName, String condition) { + throw new UnsupportedOperationException("Column copy is not supported for generic DB"); + } + public StringBuilder writeAlterTableClause(StringBuilder builder, String tableName) { builder.append("ALTER TABLE ").append(tableName).append(" "); return builder; http://git-wip-us.apache.org/repos/asf/ambari/blob/2307c9d4/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/H2Helper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/H2Helper.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/H2Helper.java index 100c865..602117a 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/H2Helper.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/H2Helper.java @@ -81,4 +81,33 @@ public class H2Helper extends GenericDbmsHelper { return String.format("UPDATE %1$s a SET %3$s = (SELECT b.%4$s FROM %2$s b WHERE b.%6$s = a.%5$s LIMIT 1)", targetTable, sourceTable, targetColumnName, sourceColumnName, targetIDColumnName, sourceIDColumnName); } + + /** + * {@inheritDoc} + */ + @Override + public String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName, + String sourceIDColumnName1, String sourceIDColumnName2, + String sourceIDColumnName3, + String targetTable, String targetColumnName, + String targetIDColumnName1, String targetIDColumnName2, + String targetIDColumnName3, + String sourceConditionFieldName, String condition) { + return String.format("UPDATE %1$s a SET %3$s = (SELECT b.%4$s FROM %2$s b WHERE b.%8$s = a.%5$s AND b.%9$s = a.%6$s AND b.%10$s = a.%7$s AND b.%11$s = '%12$s' LIMIT 1)", + targetTable, sourceTable, targetColumnName, sourceColumnName, targetIDColumnName1, targetIDColumnName2, targetIDColumnName3, + sourceIDColumnName1, sourceIDColumnName2, sourceIDColumnName3, sourceConditionFieldName, condition); + } + + + /** + * {@inheritDoc} + */ + @Override + public String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName, + String sourceIDColumnName, String targetTable, + String targetColumnName, String targetIDColumnName, + String sourceConditionFieldName, String condition) { + return String.format("UPDATE %1$s a SET %3$s = (SELECT b.%4$s FROM %2$s b WHERE b.%6$s = a.%5$s AND b.%7$s = '%8$s' LIMIT 1)", + targetTable, sourceTable, targetColumnName, sourceColumnName, targetIDColumnName, sourceIDColumnName, sourceConditionFieldName, condition); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/2307c9d4/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/MySqlHelper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/MySqlHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/MySqlHelper.java index ad26520..2df4547 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/MySqlHelper.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/MySqlHelper.java @@ -105,4 +105,33 @@ public class MySqlHelper extends GenericDbmsHelper { targetTable, sourceTable, targetColumnName, sourceColumnName, targetIDColumnName, sourceIDColumnName); } + /** + * {@inheritDoc} + */ + @Override + public String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName, + String sourceIDColumnName, String targetTable, + String targetColumnName, String targetIDColumnName, + String sourceConditionFieldName, String condition) { + + return String.format("UPDATE %1$s AS a INNER JOIN %2$s AS b ON a.%5$s = b.%6$s AND b.%7$s = '%8$s' SET a.%3$s = b.%4$s", + targetTable, sourceTable, targetColumnName, sourceColumnName, targetIDColumnName, sourceIDColumnName, sourceConditionFieldName, condition); + } + + /** + * {@inheritDoc} + */ + @Override + public String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName, + String sourceIDColumnName1, String sourceIDColumnName2, + String sourceIDColumnName3, + String targetTable, String targetColumnName, + String targetIDColumnName1, String targetIDColumnName2, + String targetIDColumnName3, + String sourceConditionFieldName, String condition) { + return String.format("UPDATE %1$s AS a INNER JOIN %2$s AS b ON a.%5$s = b.%8$s AND a.%6$s = b.%9$s AND a.%7$s = b.%10$s AND b.%11$s = '%12$s' SET a.%3$s = b.%4$s", + targetTable, sourceTable, targetColumnName, sourceColumnName, targetIDColumnName1, targetIDColumnName2, targetIDColumnName3, + sourceIDColumnName1, sourceIDColumnName2, sourceIDColumnName3, sourceConditionFieldName, condition); + } + } http://git-wip-us.apache.org/repos/asf/ambari/blob/2307c9d4/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/OracleHelper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/OracleHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/OracleHelper.java index 73356d1..fa8ba02 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/OracleHelper.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/OracleHelper.java @@ -91,4 +91,32 @@ public class OracleHelper extends GenericDbmsHelper { return String.format("UPDATE %1$s a SET (a.%3$s) = (SELECT b.%4$s FROM %2$s b WHERE b.%6$s = a.%5$s and ROWNUM < 2)", targetTable, sourceTable, targetColumnName, sourceColumnName, targetIDColumnName, sourceIDColumnName); } + + /** + * {@inheritDoc} + */ + @Override + public String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName, + String sourceIDColumnName, String targetTable, + String targetColumnName, String targetIDColumnName, + String sourceConditionFieldName, String condition) { + return String.format("UPDATE %1$s a SET (a.%3$s) = (SELECT b.%4$s FROM %2$s b WHERE b.%6$s = a.%5$s AND b.%7$s = '%8$s' AND ROWNUM < 2)", + targetTable, sourceTable, targetColumnName, sourceColumnName, targetIDColumnName, sourceIDColumnName, sourceConditionFieldName, condition); + } + + /** + * {@inheritDoc} + */ + @Override + public String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName, + String sourceIDColumnName1, String sourceIDColumnName2, + String sourceIDColumnName3, + String targetTable, String targetColumnName, + String targetIDColumnName1, String targetIDColumnName2, + String targetIDColumnName3, + String sourceConditionFieldName, String condition) { + return String.format("UPDATE %1$s a SET (a.%3$s) = (SELECT b.%4$s FROM %2$s b WHERE b.%8$s = a.%5$s AND b.%9$s = a.%6$s AND b.%10$s = a.%7$s AND b.%11$s = '%12$s' AND ROWNUM < 2)", + targetTable, sourceTable, targetColumnName, sourceColumnName, targetIDColumnName1, targetIDColumnName2, targetIDColumnName3, + sourceIDColumnName1, sourceIDColumnName2, sourceIDColumnName3, sourceConditionFieldName, condition); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/2307c9d4/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/PostgresHelper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/PostgresHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/PostgresHelper.java index 46d7315..b0aac45 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/PostgresHelper.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/dbms/PostgresHelper.java @@ -55,6 +55,33 @@ public class PostgresHelper extends GenericDbmsHelper { targetTable, sourceTable, targetColumnName, sourceColumnName, targetIDColumnName, sourceIDColumnName); } + /** + * {@inheritDoc} + */ + @Override + public String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName, + String sourceIDColumnName, String targetTable, + String targetColumnName, String targetIDColumnName, + String sourceConditionFieldName, String condition) { + return String.format("UPDATE %1$s AS a SET %3$s = b.%4$s FROM %2$s AS b WHERE a.%5$s = b.%6$s AND b.%7$s = '%8$s'", + targetTable, sourceTable, targetColumnName, sourceColumnName, targetIDColumnName, sourceIDColumnName, sourceConditionFieldName, condition); + } + + /** + * {@inheritDoc} + */ + @Override + public String getCopyColumnToAnotherTableStatement(String sourceTable, String sourceColumnName, + String sourceIDColumnName1, String sourceIDColumnName2, + String sourceIDColumnName3, + String targetTable, String targetColumnName, + String targetIDColumnName1, String targetIDColumnName2, + String targetIDColumnName3, + String sourceConditionFieldName, String condition) { + return String.format("UPDATE %1$s AS a SET %3$s = b.%4$s FROM %2$s AS b WHERE a.%5$s = b.%8$s AND a.%6$s = b.%9$s AND a.%7$s = b.%10$s AND b.%11$s = '%12$s'", + targetTable, sourceTable, targetColumnName, sourceColumnName, targetIDColumnName1, targetIDColumnName2, targetIDColumnName3, + sourceIDColumnName1, sourceIDColumnName2, sourceIDColumnName3, sourceConditionFieldName, condition); + } @Override public StringBuilder writeSetNullableString(StringBuilder builder, http://git-wip-us.apache.org/repos/asf/ambari/blob/2307c9d4/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java index 35e5b10..961c4f8 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/SchemaUpgradeHelper.java @@ -180,6 +180,7 @@ public class SchemaUpgradeHelper { Multibinder.newSetBinder(binder(), UpgradeCatalog.class); catalogBinder.addBinding().to(UpgradeCatalog251.class); catalogBinder.addBinding().to(UpgradeCatalog252.class); + catalogBinder.addBinding().to(UpgradeCatalog260.class); catalogBinder.addBinding().to(UpdateAlertScriptPaths.class); catalogBinder.addBinding().to(FinalUpgradeCatalog.class); http://git-wip-us.apache.org/repos/asf/ambari/blob/2307c9d4/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog260.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog260.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog260.java new file mode 100644 index 0000000..d14021b --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog260.java @@ -0,0 +1,318 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ambari.server.upgrade; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.ambari.server.AmbariException; +import org.apache.ambari.server.orm.DBAccessor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.inject.Inject; +import com.google.inject.Injector; + +/** + * The {@link org.apache.ambari.server.upgrade.UpgradeCatalog260} upgrades Ambari from 2.5.2 to 2.6.0. + */ +public class UpgradeCatalog260 extends AbstractUpgradeCatalog { + + private static final String CLUSTER_CONFIG_MAPPING_TABLE = "clusterconfigmapping"; + private static final String CLUSTER_VERSION_TABLE = "cluster_version"; + private static final String CLUSTER_ID_COLUMN = "cluster_id"; + private static final String STATE_COLUMN = "state"; + private static final String CREATE_TIMESTAMP_COLUMN = "create_timestamp"; + private static final String VERSION_TAG_COLUMN = "version_tag"; + private static final String TYPE_NAME_COLUMN = "type_name"; + + private static final String CLUSTER_CONFIG_TABLE = "clusterconfig"; + private static final String SELECTED_COLUMN = "selected"; + private static final String SELECTED_TIMESTAMP_COLUMN = "selected_timestamp"; + + private static final String SERVICE_COMPONENT_DESIRED_STATE_TABLE = "servicecomponentdesiredstate"; + private static final String DESIRED_STACK_ID_COLUMN = "desired_stack_id"; + private static final String DESIRED_VERSION_COLUMN = "desired_version"; + private static final String DESIRED_REPO_VERSION_ID_COLUMN = "desired_repo_version_id"; + private static final String REPO_STATE_COLUMN = "repo_state"; + private static final String FK_SCDS_DESIRED_STACK_ID = "FK_scds_desired_stack_id"; + private static final String FK_SCDS_DESIRED_REPO_ID = "FK_scds_desired_repo_id"; + + private static final String REPO_VERSION_TABLE = "repo_version"; + private static final String REPO_VERSION_ID_COLUMN = "repo_version_id"; + + private static final String HOST_COMPONENT_DESIRED_STATE_TABLE = "hostcomponentdesiredstate"; + private static final String FK_HCDS_DESIRED_STACK_ID = "FK_hcds_desired_stack_id"; + + private static final String HOST_COMPONENT_STATE_TABLE = "hostcomponentstate"; + private static final String CURRENT_STACK_ID_COLUMN = "current_stack_id"; + private static final String FK_HCS_CURRENT_STACK_ID = "FK_hcs_current_stack_id"; + + private static final String HOST_VERSION_TABLE = "host_version"; + private static final String UQ_HOST_REPO = "UQ_host_repo"; + private static final String HOST_ID_COLUMN = "host_id"; + + private static final String SERVICE_DESIRED_STATE_TABLE = "servicedesiredstate"; + private static final String FK_SDS_DESIRED_STACK_ID = "FK_sds_desired_stack_id"; + private static final String FK_REPO_VERSION_ID = "FK_repo_version_id"; + + private static final String UPGRADE_TABLE = "upgrade"; + private static final String FROM_REPO_VERSION_ID_COLUMN = "from_repo_version_id"; + private static final String TO_REPO_VERSION_ID_COLUMN = "to_repo_version_id"; + private static final String ORCHESTRATION_COLUMN = "orchestration"; + private static final String FK_UPGRADE_FROM_REPO_ID = "FK_upgrade_from_repo_id"; + private static final String FK_UPGRADE_TO_REPO_ID = "FK_upgrade_to_repo_id"; + private static final String FK_UPGRADE_REPO_VERSION_ID = "FK_upgrade_repo_version_id"; + + private static final String SERVICE_COMPONENT_HISTORY_TABLE = "servicecomponent_history"; + private static final String UPGRADE_HISTORY_TABLE = "upgrade_history"; + private static final String ID_COLUMN = "id"; + private static final String UPGRADE_ID_COLUMN = "upgrade_id"; + private static final String SERVICE_NAME_COLUMN = "service_name"; + private static final String COMPONENT_NAME_COLUMN = "component_name"; + private static final String TARGET_REPO_VERSION_ID_COLUMN = "target_repo_version_id"; + private static final String PK_UPGRADE_HIST = "PK_upgrade_hist"; + private static final String FK_UPGRADE_HIST_UPGRADE_ID = "FK_upgrade_hist_upgrade_id"; + private static final String FK_UPGRADE_HIST_FROM_REPO = "FK_upgrade_hist_from_repo"; + private static final String FK_UPGRADE_HIST_TARGET_REPO = "FK_upgrade_hist_target_repo"; + private static final String UQ_UPGRADE_HIST = "UQ_upgrade_hist"; + + /** + * Logger. + */ + private static final Logger LOG = LoggerFactory.getLogger(UpgradeCatalog260.class); + public static final String STANDARD = "STANDARD"; + public static final String NOT_REQUIRED = "NOT_REQUIRED"; + public static final String CURRENT = "CURRENT"; + public static final String SELECTED = "1"; + + + /** + * Constructor. + * + * @param injector + */ + @Inject + public UpgradeCatalog260(Injector injector) { + super(injector); + } + + /** + * {@inheritDoc} + */ + @Override + public String getSourceVersion() { + return "2.5.2"; + } + + /** + * {@inheritDoc} + */ + @Override + public String getTargetVersion() { + return "2.6.0"; + } + + /** + * {@inheritDoc} + */ + @Override + protected void executeDDLUpdates() throws AmbariException, SQLException { + updateServiceComponentDesiredStateTable(); + updateServiceDesiredStateTable(); + addSelectedCollumsToClusterconfigTable(); + updateHostComponentDesiredStateTable(); + updateHostComponentStateTable(); + updateUpgradeTable(); + createUpgradeHistoryTable(); + dropStaleTables(); + + + } + + private void createUpgradeHistoryTable() throws SQLException { + List<DBAccessor.DBColumnInfo> columns = new ArrayList<DBAccessor.DBColumnInfo>(); + + columns.add(new DBAccessor.DBColumnInfo(ID_COLUMN, Long.class, null, null, false)); + columns.add(new DBAccessor.DBColumnInfo(UPGRADE_ID_COLUMN, Long.class, null, null, false)); + columns.add(new DBAccessor.DBColumnInfo(SERVICE_NAME_COLUMN, String.class, 255, null, false)); + columns.add(new DBAccessor.DBColumnInfo(COMPONENT_NAME_COLUMN, String.class, 255, null, false)); + columns.add(new DBAccessor.DBColumnInfo(FROM_REPO_VERSION_ID_COLUMN, Long.class, null, null, false)); + columns.add(new DBAccessor.DBColumnInfo(TARGET_REPO_VERSION_ID_COLUMN, Long.class, null, null, false)); + dbAccessor.createTable(UPGRADE_HISTORY_TABLE, columns); + + dbAccessor.addPKConstraint(UPGRADE_HISTORY_TABLE, PK_UPGRADE_HIST, ID_COLUMN); + + dbAccessor.addFKConstraint(UPGRADE_HISTORY_TABLE, FK_UPGRADE_HIST_UPGRADE_ID, UPGRADE_ID_COLUMN, UPGRADE_TABLE, UPGRADE_ID_COLUMN, false); + dbAccessor.addFKConstraint(UPGRADE_HISTORY_TABLE, FK_UPGRADE_HIST_FROM_REPO, FROM_REPO_VERSION_ID_COLUMN, REPO_VERSION_TABLE, REPO_VERSION_ID_COLUMN, false); + dbAccessor.addFKConstraint(UPGRADE_HISTORY_TABLE, FK_UPGRADE_HIST_TARGET_REPO, TARGET_REPO_VERSION_ID_COLUMN, REPO_VERSION_TABLE, REPO_VERSION_ID_COLUMN, false); + dbAccessor.addUniqueConstraint(UPGRADE_HISTORY_TABLE, UQ_UPGRADE_HIST, UPGRADE_ID_COLUMN, COMPONENT_NAME_COLUMN, SERVICE_NAME_COLUMN); + + addSequence("upgrade_history_id_seq", 0L, false); + } + + /** + * Updates {@value #UPGRADE_TABLE} table. + * clear {@value #UPGRADE_TABLE} table + * Removes {@value #FROM_REPO_VERSION_ID_COLUMN} column. + * Removes {@value #TO_REPO_VERSION_ID_COLUMN} column. + * Adds the {@value #ORCHESTRATION_COLUMN} column. + * Adds the {@value #REPO_VERSION_ID_COLUMN} column. + * Removes {@value #FK_UPGRADE_FROM_REPO_ID} foreign key. + * Removes {@value #FK_UPGRADE_TO_REPO_ID} foreign key. + * adds {@value #FK_REPO_VERSION_ID} foreign key. + * + * @throws java.sql.SQLException + */ + private void updateUpgradeTable() throws SQLException { + dbAccessor.clearTable(UPGRADE_TABLE); + dbAccessor.dropFKConstraint(UPGRADE_TABLE, FK_UPGRADE_FROM_REPO_ID); + dbAccessor.dropFKConstraint(UPGRADE_TABLE, FK_UPGRADE_TO_REPO_ID); + dbAccessor.dropColumn(UPGRADE_TABLE, FROM_REPO_VERSION_ID_COLUMN); + dbAccessor.dropColumn(UPGRADE_TABLE, TO_REPO_VERSION_ID_COLUMN); + + dbAccessor.addColumn(UPGRADE_TABLE, + new DBAccessor.DBColumnInfo(REPO_VERSION_ID_COLUMN, Long.class, null, null, false)); + dbAccessor.addColumn(UPGRADE_TABLE, + new DBAccessor.DBColumnInfo(ORCHESTRATION_COLUMN, String.class, 255, STANDARD, false)); + + dbAccessor.addFKConstraint(UPGRADE_TABLE, FK_UPGRADE_REPO_VERSION_ID, REPO_VERSION_ID_COLUMN, REPO_VERSION_TABLE, REPO_VERSION_ID_COLUMN, false); + } + + /** + * Updates {@value #SERVICE_DESIRED_STATE_TABLE} table. + * Removes {@value #DESIRED_STACK_ID_COLUMN} column. + * Adds the {@value #DESIRED_REPO_VERSION_ID_COLUMN} column. + * Removes {@value #FK_SDS_DESIRED_STACK_ID} foreign key. + * adds {@value #FK_REPO_VERSION_ID} foreign key. + * + * @throws java.sql.SQLException + */ + private void updateServiceDesiredStateTable() throws SQLException { + DBAccessor.DBColumnInfo desiredRepoVersionIDColumnInfo = new DBAccessor.DBColumnInfo(DESIRED_REPO_VERSION_ID_COLUMN, Long.class, null, null, false); + DBAccessor.DBColumnInfo RepoVersionIDColumnInfo = new DBAccessor.DBColumnInfo(REPO_VERSION_ID_COLUMN, Long.class, null, null, false); + + + dbAccessor.copyColumnToAnotherTable(CLUSTER_VERSION_TABLE, RepoVersionIDColumnInfo, CLUSTER_ID_COLUMN, SERVICE_DESIRED_STATE_TABLE, desiredRepoVersionIDColumnInfo, CLUSTER_ID_COLUMN, STATE_COLUMN, CURRENT, null); + + dbAccessor.addFKConstraint(SERVICE_DESIRED_STATE_TABLE, FK_REPO_VERSION_ID, DESIRED_REPO_VERSION_ID_COLUMN, REPO_VERSION_TABLE, REPO_VERSION_ID_COLUMN, false); + dbAccessor.dropFKConstraint(SERVICE_DESIRED_STATE_TABLE, FK_SDS_DESIRED_STACK_ID); + dbAccessor.dropColumn(SERVICE_DESIRED_STATE_TABLE, DESIRED_STACK_ID_COLUMN); + } + + /** + * drop {@value #CLUSTER_CONFIG_MAPPING_TABLE} and {@value #CLUSTER_VERSION_TABLE} tables. + * + * @throws java.sql.SQLException + */ + private void dropStaleTables() throws SQLException { + dbAccessor.dropTable(CLUSTER_CONFIG_MAPPING_TABLE); + dbAccessor.dropTable(CLUSTER_VERSION_TABLE); + dbAccessor.dropTable(SERVICE_COMPONENT_HISTORY_TABLE); + } + + /** + * Adds the {@value #SELECTED_COLUMN} and {@value #SELECTED_TIMESTAMP_COLUMN} columns to the + * {@value #CLUSTER_CONFIG_TABLE} table. + * + * @throws java.sql.SQLException + */ + private void addSelectedCollumsToClusterconfigTable() throws SQLException { + DBAccessor.DBColumnInfo selectedColumnInfo = new DBAccessor.DBColumnInfo(SELECTED_COLUMN, Short.class, null, 0, false); + DBAccessor.DBColumnInfo selectedmappingColumnInfo = new DBAccessor.DBColumnInfo(SELECTED_COLUMN, Integer.class, null, 0, false); + DBAccessor.DBColumnInfo selectedTimestampColumnInfo = new DBAccessor.DBColumnInfo(SELECTED_TIMESTAMP_COLUMN, Long.class, null, 0, false); + DBAccessor.DBColumnInfo CreateTimestampColumnInfo = new DBAccessor.DBColumnInfo(CREATE_TIMESTAMP_COLUMN, Long.class, null, null, false); + dbAccessor.copyColumnToAnotherTable(CLUSTER_CONFIG_MAPPING_TABLE, selectedmappingColumnInfo, + CLUSTER_ID_COLUMN, TYPE_NAME_COLUMN, VERSION_TAG_COLUMN, CLUSTER_CONFIG_TABLE, selectedColumnInfo, + CLUSTER_ID_COLUMN, TYPE_NAME_COLUMN, VERSION_TAG_COLUMN, SELECTED_COLUMN, SELECTED, 0); + + dbAccessor.copyColumnToAnotherTable(CLUSTER_CONFIG_MAPPING_TABLE, CreateTimestampColumnInfo, + CLUSTER_ID_COLUMN, TYPE_NAME_COLUMN, VERSION_TAG_COLUMN, CLUSTER_CONFIG_TABLE, selectedTimestampColumnInfo, + CLUSTER_ID_COLUMN, TYPE_NAME_COLUMN, VERSION_TAG_COLUMN, SELECTED_COLUMN, SELECTED, 0); + } + + + /** + * Updates {@value #SERVICE_COMPONENT_DESIRED_STATE_TABLE} table. + * Removes {@value #DESIRED_VERSION_COLUMN},{@value #DESIRED_STACK_ID_COLUMN} columns. + * Adds the {@value #DESIRED_REPO_VERSION_ID_COLUMN},{@value #REPO_STATE_COLUMN} columns. + * Removes {@value #FK_SCDS_DESIRED_STACK_ID} foreign key. + * adds {@value #FK_SCDS_DESIRED_REPO_ID} foreign key. + * + * @throws java.sql.SQLException + */ + private void updateServiceComponentDesiredStateTable() throws SQLException { + DBAccessor.DBColumnInfo desiredRepoVersionIDColumnInfo = new DBAccessor.DBColumnInfo(DESIRED_REPO_VERSION_ID_COLUMN, Long.class, null, null, false); + DBAccessor.DBColumnInfo RepoVersionIDColumnInfo = new DBAccessor.DBColumnInfo(REPO_VERSION_ID_COLUMN, Long.class, null, null, false); + + + dbAccessor.copyColumnToAnotherTable(CLUSTER_VERSION_TABLE, RepoVersionIDColumnInfo, CLUSTER_ID_COLUMN, SERVICE_COMPONENT_DESIRED_STATE_TABLE, desiredRepoVersionIDColumnInfo, CLUSTER_ID_COLUMN, STATE_COLUMN, CURRENT, null); + + dbAccessor.addColumn(SERVICE_COMPONENT_DESIRED_STATE_TABLE, + new DBAccessor.DBColumnInfo(REPO_STATE_COLUMN, String.class, 255, CURRENT, false)); + dbAccessor.alterColumn(SERVICE_COMPONENT_DESIRED_STATE_TABLE, + new DBAccessor.DBColumnInfo(REPO_STATE_COLUMN, String.class, 255, NOT_REQUIRED, false)); + + dbAccessor.addFKConstraint(SERVICE_COMPONENT_DESIRED_STATE_TABLE, FK_SCDS_DESIRED_REPO_ID, DESIRED_REPO_VERSION_ID_COLUMN, REPO_VERSION_TABLE, REPO_VERSION_ID_COLUMN, false); + + dbAccessor.dropFKConstraint(SERVICE_COMPONENT_DESIRED_STATE_TABLE, FK_SCDS_DESIRED_STACK_ID); + dbAccessor.dropColumn(SERVICE_COMPONENT_DESIRED_STATE_TABLE, DESIRED_STACK_ID_COLUMN); + dbAccessor.dropColumn(SERVICE_COMPONENT_DESIRED_STATE_TABLE, DESIRED_VERSION_COLUMN); + } + + /** + * Updates {@value #HOST_COMPONENT_DESIRED_STATE_TABLE} table. + * Removes {@value #DESIRED_STACK_ID_COLUMN} column. + * Removes {@value #FK_HCDS_DESIRED_STACK_ID} foreign key. + * + * @throws java.sql.SQLException + */ + private void updateHostComponentDesiredStateTable() throws SQLException { + dbAccessor.dropFKConstraint(HOST_COMPONENT_DESIRED_STATE_TABLE, FK_HCDS_DESIRED_STACK_ID); + dbAccessor.dropColumn(HOST_COMPONENT_DESIRED_STATE_TABLE, DESIRED_STACK_ID_COLUMN); + } + + /** + * Updates {@value #HOST_COMPONENT_STATE_TABLE} table. + * Removes {@value #CURRENT_STACK_ID_COLUMN} column. + * Removes {@value #FK_HCS_CURRENT_STACK_ID} foreign key. + * + * @throws java.sql.SQLException + */ + private void updateHostComponentStateTable() throws SQLException { + dbAccessor.dropFKConstraint(HOST_COMPONENT_STATE_TABLE, FK_HCS_CURRENT_STACK_ID); + dbAccessor.dropColumn(HOST_COMPONENT_STATE_TABLE, CURRENT_STACK_ID_COLUMN); + } + + /** + * {@inheritDoc} + */ + @Override + protected void executePreDMLUpdates() throws AmbariException, SQLException { + + } + + /** + * {@inheritDoc} + */ + @Override + protected void executeDMLUpdates() throws AmbariException, SQLException { + addNewConfigurationsFromXml(); + } +}