AMBARI-21728. Service and Patch Upgrade Catalog Changes for 2.6 - additional fixes (dlysnichenko)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/4cc8976c Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/4cc8976c Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/4cc8976c Branch: refs/heads/branch-feature-AMBARI-21450 Commit: 4cc8976ce500fae4d6d311b813fe0d4ec0367795 Parents: 6424b5f Author: Lisnichenko Dmitro <[email protected]> Authored: Thu Aug 17 19:37:18 2017 +0300 Committer: Lisnichenko Dmitro <[email protected]> Committed: Thu Aug 17 19:37:18 2017 +0300 ---------------------------------------------------------------------- .../apache/ambari/server/orm/DBAccessor.java | 36 +- .../ambari/server/orm/DBAccessorImpl.java | 90 ++-- .../server/orm/helpers/dbms/DbmsHelper.java | 20 - .../orm/helpers/dbms/GenericDbmsHelper.java | 11 - .../server/orm/helpers/dbms/H2Helper.java | 13 - .../server/orm/helpers/dbms/MySqlHelper.java | 13 - .../server/orm/helpers/dbms/OracleHelper.java | 12 - .../server/orm/helpers/dbms/PostgresHelper.java | 12 - .../server/upgrade/UpgradeCatalog260.java | 169 ++++--- .../ambari/server/orm/DBAccessorImplTest.java | 88 ++++ .../server/upgrade/UpgradeCatalog260Test.java | 468 +++++++++++++++++++ 11 files changed, 719 insertions(+), 213 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/4cc8976c/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 01bec59..8b22d5a 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 @@ -367,6 +367,21 @@ public interface DBAccessor { void executePreparedUpdate(String query, boolean ignoreFailure, Object...arguments) throws SQLException; /** + * Execute select {@code columnName} from {@code tableName} + * where {@code columnNames} values = {@code values} + * + * @param tableName + * @param columnName + * @param columnNames + * @param values + * @param ignoreFailure + * @return + * @throws SQLException + */ + List<Integer> getIntColumnValues(String tableName, String columnName, String[] columnNames, + String[] values, boolean ignoreFailure) throws SQLException; + + /** * Drop table from schema * @param tableName * @throws SQLException @@ -697,27 +712,6 @@ public interface DBAccessor { * * @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} http://git-wip-us.apache.org/repos/asf/ambari/blob/4cc8976c/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 bed21e8..9564934 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 @@ -1454,42 +1454,6 @@ 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 { @@ -1550,4 +1514,58 @@ public class DBAccessorImpl implements DBAccessor { String sqlQuery = String.format("UPDATE %s SET %s = ?", convertObjectName(tableName), convertObjectName(columnName)); executePreparedUpdate(sqlQuery, value); } + + /** + * {@inheritDoc} + */ + @Override + public List<Integer> getIntColumnValues(String tableName, String columnName, String[] conditionColumnNames, + String[] values, boolean ignoreFailure) throws SQLException { + + if (!tableHasColumn(tableName, columnName)) { + throw new IllegalArgumentException(String.format("%s table does not contain %s column", tableName, columnName)); + } + StringBuilder builder = new StringBuilder(); + builder.append("SELECT ").append(columnName).append(" FROM ").append(tableName); + if (conditionColumnNames != null && conditionColumnNames.length > 0) { + for (String name : conditionColumnNames) { + if (!tableHasColumn(tableName, name)) { + throw new IllegalArgumentException(String.format("%s table does not contain %s column", tableName, name)); + } + } + if (conditionColumnNames.length != values.length) { + throw new IllegalArgumentException("number of columns should be equal to number of values"); + } + builder.append(" WHERE ").append(conditionColumnNames[0]).append("='").append(values[0]).append("'"); + for (int i = 1; i < conditionColumnNames.length; i++) { + builder.append(" AND ").append(conditionColumnNames[i]).append("='").append(values[i]).append("'"); + } + } + + List<Integer> result = new ArrayList<>(); + Statement statement = getConnection().createStatement(); + ResultSet resultSet = null; + String query = builder.toString(); + try { + resultSet = statement.executeQuery(query); + if (resultSet != null) { + while (resultSet.next()) { + result.add(resultSet.getInt(1)); + } + } + } catch (SQLException e) { + LOG.warn("Unable to execute query: " + query, e); + if (!ignoreFailure) { + throw e; + } + } finally { + if (resultSet != null) { + resultSet.close(); + } + if (statement != null) { + statement.close(); + } + } + return result; + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/4cc8976c/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 603c33b..044ced4 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 @@ -165,26 +165,6 @@ public interface DbmsHelper { * * @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} http://git-wip-us.apache.org/repos/asf/ambari/blob/4cc8976c/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 34358a9..a1969af 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 @@ -91,17 +91,6 @@ public class GenericDbmsHelper implements DbmsHelper { */ @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, http://git-wip-us.apache.org/repos/asf/ambari/blob/4cc8976c/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 602117a..b2cbb44 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 @@ -97,17 +97,4 @@ public class H2Helper extends GenericDbmsHelper { 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/4cc8976c/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 2df4547..0c5e2ae 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 @@ -110,19 +110,6 @@ public class MySqlHelper extends GenericDbmsHelper { */ @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, http://git-wip-us.apache.org/repos/asf/ambari/blob/4cc8976c/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 fa8ba02..fd512c2 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 @@ -97,18 +97,6 @@ public class OracleHelper extends GenericDbmsHelper { */ @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, http://git-wip-us.apache.org/repos/asf/ambari/blob/4cc8976c/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 b0aac45..6788070 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 @@ -60,18 +60,6 @@ public class PostgresHelper extends GenericDbmsHelper { */ @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, http://git-wip-us.apache.org/repos/asf/ambari/blob/4cc8976c/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 index d14021b..7fc392d 100644 --- 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 @@ -23,6 +23,7 @@ import java.util.List; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.orm.DBAccessor; +import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,64 +35,64 @@ import com.google.inject.Injector; */ 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"; + public static final String CLUSTER_CONFIG_MAPPING_TABLE = "clusterconfigmapping"; + public static final String CLUSTER_VERSION_TABLE = "cluster_version"; + public static final String CLUSTER_ID_COLUMN = "cluster_id"; + public static final String STATE_COLUMN = "state"; + public static final String CREATE_TIMESTAMP_COLUMN = "create_timestamp"; + public static final String VERSION_TAG_COLUMN = "version_tag"; + public static final String TYPE_NAME_COLUMN = "type_name"; + + public static final String CLUSTER_CONFIG_TABLE = "clusterconfig"; + public static final String SELECTED_COLUMN = "selected"; + public static final String SELECTED_TIMESTAMP_COLUMN = "selected_timestamp"; + + public static final String SERVICE_COMPONENT_DESIRED_STATE_TABLE = "servicecomponentdesiredstate"; + public static final String DESIRED_STACK_ID_COLUMN = "desired_stack_id"; + public static final String DESIRED_VERSION_COLUMN = "desired_version"; + public static final String DESIRED_REPO_VERSION_ID_COLUMN = "desired_repo_version_id"; + public static final String REPO_STATE_COLUMN = "repo_state"; + public static final String FK_SCDS_DESIRED_STACK_ID = "FK_scds_desired_stack_id"; + public static final String FK_SCDS_DESIRED_REPO_ID = "FK_scds_desired_repo_id"; + + public static final String REPO_VERSION_TABLE = "repo_version"; + public static final String REPO_VERSION_ID_COLUMN = "repo_version_id"; + + public static final String HOST_COMPONENT_DESIRED_STATE_TABLE = "hostcomponentdesiredstate"; + public static final String FK_HCDS_DESIRED_STACK_ID = "FK_hcds_desired_stack_id"; + + public static final String HOST_COMPONENT_STATE_TABLE = "hostcomponentstate"; + public static final String CURRENT_STACK_ID_COLUMN = "current_stack_id"; + public static final String FK_HCS_CURRENT_STACK_ID = "FK_hcs_current_stack_id"; + + public static final String HOST_VERSION_TABLE = "host_version"; + public static final String UQ_HOST_REPO = "UQ_host_repo"; + public static final String HOST_ID_COLUMN = "host_id"; + + public static final String SERVICE_DESIRED_STATE_TABLE = "servicedesiredstate"; + public static final String FK_SDS_DESIRED_STACK_ID = "FK_sds_desired_stack_id"; + public static final String FK_REPO_VERSION_ID = "FK_repo_version_id"; + + public static final String UPGRADE_TABLE = "upgrade"; + public static final String FROM_REPO_VERSION_ID_COLUMN = "from_repo_version_id"; + public static final String TO_REPO_VERSION_ID_COLUMN = "to_repo_version_id"; + public static final String ORCHESTRATION_COLUMN = "orchestration"; + public static final String FK_UPGRADE_FROM_REPO_ID = "FK_upgrade_from_repo_id"; + public static final String FK_UPGRADE_TO_REPO_ID = "FK_upgrade_to_repo_id"; + public static final String FK_UPGRADE_REPO_VERSION_ID = "FK_upgrade_repo_version_id"; + + public static final String SERVICE_COMPONENT_HISTORY_TABLE = "servicecomponent_history"; + public static final String UPGRADE_HISTORY_TABLE = "upgrade_history"; + public static final String ID_COLUMN = "id"; + public static final String UPGRADE_ID_COLUMN = "upgrade_id"; + public static final String SERVICE_NAME_COLUMN = "service_name"; + public static final String COMPONENT_NAME_COLUMN = "component_name"; + public static final String TARGET_REPO_VERSION_ID_COLUMN = "target_repo_version_id"; + public static final String PK_UPGRADE_HIST = "PK_upgrade_hist"; + public static final String FK_UPGRADE_HIST_UPGRADE_ID = "FK_upgrade_hist_upgrade_id"; + public static final String FK_UPGRADE_HIST_FROM_REPO = "FK_upgrade_hist_from_repo"; + public static final String FK_UPGRADE_HIST_TARGET_REPO = "FK_upgrade_hist_target_repo"; + public static final String UQ_UPGRADE_HIST = "UQ_upgrade_hist"; /** * Logger. @@ -134,16 +135,15 @@ public class UpgradeCatalog260 extends AbstractUpgradeCatalog { */ @Override protected void executeDDLUpdates() throws AmbariException, SQLException { - updateServiceComponentDesiredStateTable(); - updateServiceDesiredStateTable(); + int currentVersionID = getCurrentVersionID(); + updateServiceComponentDesiredStateTable(currentVersionID); + updateServiceDesiredStateTable(currentVersionID); addSelectedCollumsToClusterconfigTable(); updateHostComponentDesiredStateTable(); updateHostComponentStateTable(); updateUpgradeTable(); createUpgradeHistoryTable(); dropStaleTables(); - - } private void createUpgradeHistoryTable() throws SQLException { @@ -204,12 +204,12 @@ public class UpgradeCatalog260 extends AbstractUpgradeCatalog { * * @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); - + private void updateServiceDesiredStateTable(int currentRepoID) throws SQLException { - dbAccessor.copyColumnToAnotherTable(CLUSTER_VERSION_TABLE, RepoVersionIDColumnInfo, CLUSTER_ID_COLUMN, SERVICE_DESIRED_STATE_TABLE, desiredRepoVersionIDColumnInfo, CLUSTER_ID_COLUMN, STATE_COLUMN, CURRENT, null); + dbAccessor.addColumn(SERVICE_DESIRED_STATE_TABLE, + new DBAccessor.DBColumnInfo(DESIRED_REPO_VERSION_ID_COLUMN, Long.class, null, currentRepoID, false)); + dbAccessor.alterColumn(SERVICE_DESIRED_STATE_TABLE, + new DBAccessor.DBColumnInfo(DESIRED_REPO_VERSION_ID_COLUMN, Long.class, null, null, false)); 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); @@ -237,12 +237,12 @@ public class UpgradeCatalog260 extends AbstractUpgradeCatalog { 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.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, + 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); } @@ -257,12 +257,11 @@ public class UpgradeCatalog260 extends AbstractUpgradeCatalog { * * @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); + private void updateServiceComponentDesiredStateTable(int currentRepoID) throws SQLException { + dbAccessor.addColumn(SERVICE_COMPONENT_DESIRED_STATE_TABLE, + new DBAccessor.DBColumnInfo(DESIRED_REPO_VERSION_ID_COLUMN, Long.class, null, currentRepoID, false)); + dbAccessor.alterColumn(SERVICE_COMPONENT_DESIRED_STATE_TABLE, + new DBAccessor.DBColumnInfo(DESIRED_REPO_VERSION_ID_COLUMN, Long.class, null, null, false)); dbAccessor.addColumn(SERVICE_COMPONENT_DESIRED_STATE_TABLE, new DBAccessor.DBColumnInfo(REPO_STATE_COLUMN, String.class, 255, CURRENT, false)); @@ -315,4 +314,24 @@ public class UpgradeCatalog260 extends AbstractUpgradeCatalog { protected void executeDMLUpdates() throws AmbariException, SQLException { addNewConfigurationsFromXml(); } + + /** + * get {@value #REPO_VERSION_ID_COLUMN} value from {@value #CLUSTER_VERSION_TABLE} + * where {@value #STATE_COLUMN} = {@value #CURRENT} + * and validate it + * + * @return current version ID + * @throws AmbariException + * @throws SQLException + */ + public int getCurrentVersionID() throws AmbariException, SQLException { + List<Integer> currentVersionList = dbAccessor.getIntColumnValues(CLUSTER_VERSION_TABLE, REPO_VERSION_ID_COLUMN, + new String[]{STATE_COLUMN}, new String[]{CURRENT}, false); + if (currentVersionList.isEmpty()) { + throw new AmbariException("Unable to find any CURRENT repositories."); + } else if (currentVersionList.size() != 1) { + throw new AmbariException("The following repositories were found to be CURRENT: ".concat(StringUtils.join(currentVersionList, ","))); + } + return currentVersionList.get(0); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/4cc8976c/ambari-server/src/test/java/org/apache/ambari/server/orm/DBAccessorImplTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/DBAccessorImplTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/DBAccessorImplTest.java index d576cc8..bbffa6f 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/orm/DBAccessorImplTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/DBAccessorImplTest.java @@ -641,6 +641,94 @@ public class DBAccessorImplTest { } @Test + public void testCopyColumnToAnotherTable() throws Exception { + DBAccessorImpl dbAccessor = injector.getInstance(DBAccessorImpl.class); + String sourceTableName = getFreeTableName(); + String targetTableName = getFreeTableName(); + int testRowAmount = 10; + + createMyTable(sourceTableName, "col1", "col2", "col3", "col4", "col5"); + createMyTable(targetTableName, "col1", "col2", "col3"); + + for (Integer i = 0; i < testRowAmount; i++) { + dbAccessor.insertRow(sourceTableName, + new String[]{"id", "col1", "col2", "col3", "col4", "col5"}, + new String[]{i.toString(), String.format("'1,%s'", i), String.format("'2,%s'", i * 2), String.format("'3,%s'", i * 3), String.format("'4,%s'", i * 4), String.format("'%s'", (i * 5) % 2)}, false); + + dbAccessor.insertRow(targetTableName, + new String[]{"id", "col1", "col2", "col3"}, + new String[]{i.toString(), String.format("'1,%s'", i), String.format("'2,%s'", i * 2), String.format("'3,%s'", i * 3)}, false); + } + + DBColumnInfo sourceColumn = new DBColumnInfo("col4", String.class, null, null, false); + DBColumnInfo targetColumn = new DBColumnInfo("col4", String.class, null, null, false); + + dbAccessor.copyColumnToAnotherTable(sourceTableName, sourceColumn, "id", "col1", "col2", + targetTableName, targetColumn, "id", "col1", "col2", "col5", "0", "initial"); + + Statement statement = dbAccessor.getConnection().createStatement(); + ResultSet resultSet = statement.executeQuery("SELECT col4 FROM " + targetTableName + " ORDER BY id"); + + assertNotNull(resultSet); + + List<String> response = new LinkedList<>(); + + while (resultSet.next()) { + response.add(resultSet.getString(1)); + } + + assertEquals(testRowAmount, response.toArray().length); + for (String row : response) { + System.out.println(row); + } + + + int i = 0; + for (String row : response) { + if (i % 2 == 0) { + assertEquals(String.format("4,%s", i * 4), row); + } else { + assertEquals("initial", row); + } + i++; + } + + } + + @Test + public void testGetIntColumnValues() throws Exception { + DBAccessorImpl dbAccessor = injector.getInstance(DBAccessorImpl.class); + String sourceTableName = getFreeTableName(); + int testRowAmount = 10; + + createMyTable(sourceTableName, "col1", "col2", "col3", "col4", "col5"); + + for (Integer i = 0; i < testRowAmount; i++) { + dbAccessor.insertRow(sourceTableName, + new String[]{"id", "col1", "col2", "col3", "col4", "col5"}, + new String[]{i.toString(), String.format("'1,%s'", i), String.format("'2,%s'", i * 2), String.format("'3,%s'", i * 3), String.format("'4,%s'", i * 4), String.format("'%s'", (i * 5) % 2)}, false); + } + + List<Integer> idList = dbAccessor.getIntColumnValues(sourceTableName, "id", + new String[]{"col1", "col5"}, new String[]{"1,0", "0"}, false); + + assertEquals(idList.size(), 1); + assertEquals(idList.get(0), Integer.valueOf(0)); + + idList = dbAccessor.getIntColumnValues(sourceTableName, "id", + new String[]{"col5"}, new String[]{"0"}, false); + + assertEquals(idList.size(), 5); + + int i = 0; + for (Integer id : idList) { + assertEquals(id, Integer.valueOf(i * 2)); + i++; + } + + } + + @Test public void testMoveNonexistentColumnIsNoop() throws Exception { DBAccessorImpl dbAccessor = injector.getInstance(DBAccessorImpl.class); String sourceTableName = getFreeTableName(); http://git-wip-us.apache.org/repos/asf/ambari/blob/4cc8976c/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog260Test.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog260Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog260Test.java new file mode 100644 index 0000000..44b5d91 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog260Test.java @@ -0,0 +1,468 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.capture; +import static org.easymock.EasyMock.eq; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.newCapture; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.reset; +import static org.easymock.EasyMock.verify; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.persistence.EntityManager; + +import org.apache.ambari.server.actionmanager.ActionManager; +import org.apache.ambari.server.configuration.Configuration; +import org.apache.ambari.server.controller.KerberosHelper; +import org.apache.ambari.server.controller.MaintenanceStateHelper; +import org.apache.ambari.server.orm.DBAccessor; +import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo; +import org.apache.ambari.server.state.Cluster; +import org.apache.ambari.server.state.Clusters; +import org.apache.ambari.server.state.Config; +import org.apache.ambari.server.state.Service; +import org.apache.ambari.server.state.stack.OsFamily; +import org.easymock.Capture; +import org.easymock.EasyMockRunner; +import org.easymock.Mock; +import org.easymock.MockType; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.google.gson.Gson; +import com.google.inject.Binder; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Module; +import com.google.inject.Provider; + +/** + * {@link UpgradeCatalog260} unit tests. + */ +@RunWith(EasyMockRunner.class) +public class UpgradeCatalog260Test { + + // private Injector injector; + @Mock(type = MockType.STRICT) + private Provider<EntityManager> entityManagerProvider; + + @Mock(type = MockType.NICE) + private EntityManager entityManager; + + @Mock(type = MockType.NICE) + private DBAccessor dbAccessor; + + @Mock(type = MockType.NICE) + private Configuration configuration; + + @Mock(type = MockType.NICE) + private Connection connection; + + @Mock(type = MockType.NICE) + private Statement statement; + + @Mock(type = MockType.NICE) + private ResultSet resultSet; + + @Mock(type = MockType.NICE) + private OsFamily osFamily; + + @Mock(type = MockType.NICE) + private KerberosHelper kerberosHelper; + + @Mock(type = MockType.NICE) + private ActionManager actionManager; + + @Mock(type = MockType.NICE) + private Config config; + + @Mock(type = MockType.STRICT) + private Service service; + + @Mock(type = MockType.NICE) + private Clusters clusters; + + @Mock(type = MockType.NICE) + private Cluster cluster; + + @Mock(type = MockType.NICE) + private Injector injector; + + @Before + public void init() { + reset(entityManagerProvider, injector); + + expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes(); + + expect(injector.getInstance(Gson.class)).andReturn(null).anyTimes(); + expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).anyTimes(); + expect(injector.getInstance(KerberosHelper.class)).andReturn(kerberosHelper).anyTimes(); + + replay(entityManagerProvider, injector); + } + + @After + public void tearDown() { + } + + @Test + public void testExecuteDDLUpdates() throws Exception { + + List<Integer> current = new ArrayList<Integer>(); + current.add(1); + + expect(dbAccessor.getConnection()).andReturn(connection).anyTimes(); + expect(connection.createStatement()).andReturn(statement).anyTimes(); + expect(statement.executeQuery(anyObject(String.class))).andReturn(resultSet).anyTimes(); + expect(configuration.getDatabaseType()).andReturn(Configuration.DatabaseType.POSTGRES).anyTimes(); + + + Capture<String[]> scdcaptureKey = newCapture(); + Capture<String[]> scdcaptureValue = newCapture(); + expectGetCurrentVersionID(current, scdcaptureKey, scdcaptureValue); + + Capture<DBColumnInfo> scdstadd1 = newCapture(); + Capture<DBColumnInfo> scdstalter1 = newCapture(); + Capture<DBColumnInfo> scdstadd2 = newCapture(); + Capture<DBColumnInfo> scdstalter2 = newCapture(); + expectUpdateServiceComponentDesiredStateTable(scdstadd1, scdstalter1, scdstadd2, scdstalter2); + + Capture<DBColumnInfo> sdstadd = newCapture(); + Capture<DBColumnInfo> sdstalter = newCapture(); + expectUpdateServiceDesiredStateTable(sdstadd, sdstalter); + + Capture<DBColumnInfo> selectedColumnInfo = newCapture(); + Capture<DBColumnInfo> selectedmappingColumnInfo = newCapture(); + Capture<DBColumnInfo> selectedTimestampColumnInfo = newCapture(); + Capture<DBColumnInfo> createTimestampColumnInfo = newCapture(); + expectAddSelectedCollumsToClusterconfigTable(selectedColumnInfo, selectedmappingColumnInfo, selectedTimestampColumnInfo, createTimestampColumnInfo); + + expectUpdateHostComponentDesiredStateTable(); + expectUpdateHostComponentStateTable(); + + Capture<DBColumnInfo> rvid = newCapture(); + Capture<DBColumnInfo> orchestration = newCapture(); + expectUpdateUpgradeTable(rvid, orchestration); + + Capture<List<DBAccessor.DBColumnInfo>> columns = newCapture(); + expectCreateUpgradeHistoryTable(columns); + + expectDropStaleTables(); + + replay(dbAccessor, configuration, connection, statement, resultSet); + + Module module = new Module() { + @Override + public void configure(Binder binder) { + binder.bind(DBAccessor.class).toInstance(dbAccessor); + binder.bind(OsFamily.class).toInstance(osFamily); + binder.bind(EntityManager.class).toInstance(entityManager); + binder.bind(Configuration.class).toInstance(configuration); + } + }; + + Injector injector = Guice.createInjector(module); + UpgradeCatalog260 upgradeCatalog260 = injector.getInstance(UpgradeCatalog260.class); + upgradeCatalog260.executeDDLUpdates(); + + verify(dbAccessor); + + verifyGetCurrentVersionID(scdcaptureKey, scdcaptureValue); + verifyUpdateServiceComponentDesiredStateTable(scdstadd1, scdstalter1, scdstadd2, scdstalter2); + verifyUpdateServiceDesiredStateTable(sdstadd, sdstalter); + verifyAddSelectedCollumsToClusterconfigTable(selectedColumnInfo, selectedmappingColumnInfo, selectedTimestampColumnInfo, createTimestampColumnInfo); + verifyUpdateUpgradeTable(rvid, orchestration); + verifyCreateUpgradeHistoryTable(columns); + + } + + public void expectDropStaleTables() throws SQLException { + dbAccessor.dropTable(eq(UpgradeCatalog260.CLUSTER_CONFIG_MAPPING_TABLE)); + expectLastCall().once(); + dbAccessor.dropTable(eq(UpgradeCatalog260.CLUSTER_VERSION_TABLE)); + expectLastCall().once(); + dbAccessor.dropTable(eq(UpgradeCatalog260.SERVICE_COMPONENT_HISTORY_TABLE)); + expectLastCall().once(); + } + + public void verifyCreateUpgradeHistoryTable(Capture<List<DBColumnInfo>> columns) { + List<DBColumnInfo> columnsValue = columns.getValue(); + Assert.assertEquals(columnsValue.size(), 6); + + DBColumnInfo id = columnsValue.get(0); + Assert.assertEquals(UpgradeCatalog260.ID_COLUMN, id.getName()); + Assert.assertEquals(Long.class, id.getType()); + Assert.assertEquals(null, id.getLength()); + Assert.assertEquals(null, id.getDefaultValue()); + Assert.assertEquals(false, id.isNullable()); + + DBColumnInfo upgradeId = columnsValue.get(1); + Assert.assertEquals(UpgradeCatalog260.UPGRADE_ID_COLUMN, upgradeId.getName()); + Assert.assertEquals(Long.class, upgradeId.getType()); + Assert.assertEquals(null, upgradeId.getLength()); + Assert.assertEquals(null, upgradeId.getDefaultValue()); + Assert.assertEquals(false, upgradeId.isNullable()); + + DBColumnInfo serviceName = columnsValue.get(2); + Assert.assertEquals(UpgradeCatalog260.SERVICE_NAME_COLUMN, serviceName.getName()); + Assert.assertEquals(String.class, serviceName.getType()); + Assert.assertEquals(Integer.valueOf(255), serviceName.getLength()); + Assert.assertEquals(null, serviceName.getDefaultValue()); + Assert.assertEquals(false, serviceName.isNullable()); + + DBColumnInfo componentName = columnsValue.get(3); + Assert.assertEquals(UpgradeCatalog260.COMPONENT_NAME_COLUMN, componentName.getName()); + Assert.assertEquals(String.class, componentName.getType()); + Assert.assertEquals(Integer.valueOf(255), componentName.getLength()); + Assert.assertEquals(null, componentName.getDefaultValue()); + Assert.assertEquals(false, componentName.isNullable()); + + DBColumnInfo fromRepoID = columnsValue.get(4); + Assert.assertEquals(UpgradeCatalog260.FROM_REPO_VERSION_ID_COLUMN, fromRepoID.getName()); + Assert.assertEquals(Long.class, fromRepoID.getType()); + Assert.assertEquals(null, fromRepoID.getLength()); + Assert.assertEquals(null, fromRepoID.getDefaultValue()); + Assert.assertEquals(false, fromRepoID.isNullable()); + + DBColumnInfo targetRepoID = columnsValue.get(5); + Assert.assertEquals(UpgradeCatalog260.TARGET_REPO_VERSION_ID_COLUMN, targetRepoID.getName()); + Assert.assertEquals(Long.class, targetRepoID.getType()); + Assert.assertEquals(null, targetRepoID.getLength()); + Assert.assertEquals(null, targetRepoID.getDefaultValue()); + Assert.assertEquals(false, targetRepoID.isNullable()); + } + + public void expectCreateUpgradeHistoryTable(Capture<List<DBColumnInfo>> columns) throws SQLException { + dbAccessor.createTable(eq(UpgradeCatalog260.UPGRADE_HISTORY_TABLE), capture(columns)); + expectLastCall().once(); + + dbAccessor.addPKConstraint(eq(UpgradeCatalog260.UPGRADE_HISTORY_TABLE), eq(UpgradeCatalog260.PK_UPGRADE_HIST), eq(UpgradeCatalog260.ID_COLUMN)); + expectLastCall().once(); + + dbAccessor.addFKConstraint(eq(UpgradeCatalog260.UPGRADE_HISTORY_TABLE), eq(UpgradeCatalog260.FK_UPGRADE_HIST_UPGRADE_ID), eq(UpgradeCatalog260.UPGRADE_ID_COLUMN), eq(UpgradeCatalog260.UPGRADE_TABLE), eq(UpgradeCatalog260.UPGRADE_ID_COLUMN), eq(false)); + expectLastCall().once(); + dbAccessor.addFKConstraint(eq(UpgradeCatalog260.UPGRADE_HISTORY_TABLE), eq(UpgradeCatalog260.FK_UPGRADE_HIST_FROM_REPO), eq(UpgradeCatalog260.FROM_REPO_VERSION_ID_COLUMN), eq(UpgradeCatalog260.REPO_VERSION_TABLE), eq(UpgradeCatalog260.REPO_VERSION_ID_COLUMN), eq(false)); + expectLastCall().once(); + dbAccessor.addFKConstraint(eq(UpgradeCatalog260.UPGRADE_HISTORY_TABLE), eq(UpgradeCatalog260.FK_UPGRADE_HIST_TARGET_REPO), eq(UpgradeCatalog260.TARGET_REPO_VERSION_ID_COLUMN), eq(UpgradeCatalog260.REPO_VERSION_TABLE), eq(UpgradeCatalog260.REPO_VERSION_ID_COLUMN), eq(false)); + expectLastCall().once(); + dbAccessor.addUniqueConstraint(eq(UpgradeCatalog260.UPGRADE_HISTORY_TABLE), eq(UpgradeCatalog260.UQ_UPGRADE_HIST), eq(UpgradeCatalog260.UPGRADE_ID_COLUMN), eq(UpgradeCatalog260.COMPONENT_NAME_COLUMN), eq(UpgradeCatalog260.SERVICE_NAME_COLUMN)); + expectLastCall().once(); + } + + public void verifyUpdateUpgradeTable(Capture<DBColumnInfo> rvid, Capture<DBColumnInfo> orchestration) { + DBColumnInfo rvidValue = rvid.getValue(); + Assert.assertEquals(UpgradeCatalog260.REPO_VERSION_ID_COLUMN, rvidValue.getName()); + Assert.assertEquals(Long.class, rvidValue.getType()); + Assert.assertEquals(null, rvidValue.getLength()); + Assert.assertEquals(null, rvidValue.getDefaultValue()); + Assert.assertEquals(false, rvidValue.isNullable()); + + DBColumnInfo orchestrationValue = orchestration.getValue(); + Assert.assertEquals(UpgradeCatalog260.ORCHESTRATION_COLUMN, orchestrationValue.getName()); + Assert.assertEquals(String.class, orchestrationValue.getType()); + Assert.assertEquals(Integer.valueOf(255), orchestrationValue.getLength()); + Assert.assertEquals(UpgradeCatalog260.STANDARD, orchestrationValue.getDefaultValue()); + Assert.assertEquals(false, orchestrationValue.isNullable()); + } + + public void expectUpdateUpgradeTable(Capture<DBColumnInfo> rvid, Capture<DBColumnInfo> orchestration) throws SQLException { + dbAccessor.clearTable(eq(UpgradeCatalog260.UPGRADE_TABLE)); + expectLastCall().once(); + dbAccessor.dropFKConstraint(eq(UpgradeCatalog260.UPGRADE_TABLE), eq(UpgradeCatalog260.FK_UPGRADE_FROM_REPO_ID)); + expectLastCall().once(); + dbAccessor.dropFKConstraint(eq(UpgradeCatalog260.UPGRADE_TABLE), eq(UpgradeCatalog260.FK_UPGRADE_TO_REPO_ID)); + expectLastCall().once(); + dbAccessor.dropColumn(eq(UpgradeCatalog260.UPGRADE_TABLE), eq(UpgradeCatalog260.FROM_REPO_VERSION_ID_COLUMN)); + expectLastCall().once(); + dbAccessor.dropColumn(eq(UpgradeCatalog260.UPGRADE_TABLE), eq(UpgradeCatalog260.TO_REPO_VERSION_ID_COLUMN)); + expectLastCall().once(); + + dbAccessor.addColumn(eq(UpgradeCatalog260.UPGRADE_TABLE), capture(rvid)); + expectLastCall().once(); + dbAccessor.addColumn(eq(UpgradeCatalog260.UPGRADE_TABLE), capture(orchestration)); + expectLastCall().once(); + + dbAccessor.addFKConstraint(eq(UpgradeCatalog260.UPGRADE_TABLE), eq(UpgradeCatalog260.FK_UPGRADE_REPO_VERSION_ID), eq(UpgradeCatalog260.REPO_VERSION_ID_COLUMN), eq(UpgradeCatalog260.REPO_VERSION_TABLE), eq(UpgradeCatalog260.REPO_VERSION_ID_COLUMN), eq(false)); + expectLastCall().once(); + } + + public void expectUpdateHostComponentStateTable() throws SQLException { + dbAccessor.dropFKConstraint(eq(UpgradeCatalog260.HOST_COMPONENT_STATE_TABLE), eq(UpgradeCatalog260.FK_HCS_CURRENT_STACK_ID)); + expectLastCall().once(); + dbAccessor.dropColumn(eq(UpgradeCatalog260.HOST_COMPONENT_STATE_TABLE), eq(UpgradeCatalog260.CURRENT_STACK_ID_COLUMN)); + expectLastCall().once(); + } + + public void expectUpdateHostComponentDesiredStateTable() throws SQLException { + dbAccessor.dropFKConstraint(eq(UpgradeCatalog260.HOST_COMPONENT_DESIRED_STATE_TABLE), eq(UpgradeCatalog260.FK_HCDS_DESIRED_STACK_ID)); + expectLastCall().once(); + dbAccessor.dropColumn(eq(UpgradeCatalog260.HOST_COMPONENT_DESIRED_STATE_TABLE), eq(UpgradeCatalog260.DESIRED_STACK_ID_COLUMN)); + expectLastCall().once(); + } + + public void verifyAddSelectedCollumsToClusterconfigTable(Capture<DBColumnInfo> selectedColumnInfo, Capture<DBColumnInfo> selectedmappingColumnInfo, Capture<DBColumnInfo> selectedTimestampColumnInfo, Capture<DBColumnInfo> createTimestampColumnInfo) { + DBColumnInfo selectedColumnInfoValue = selectedColumnInfo.getValue(); + Assert.assertEquals(UpgradeCatalog260.SELECTED_COLUMN, selectedColumnInfoValue.getName()); + Assert.assertEquals(Short.class, selectedColumnInfoValue.getType()); + Assert.assertEquals(null, selectedColumnInfoValue.getLength()); + Assert.assertEquals(0, selectedColumnInfoValue.getDefaultValue()); + Assert.assertEquals(false, selectedColumnInfoValue.isNullable()); + + DBColumnInfo selectedmappingColumnInfoValue = selectedmappingColumnInfo.getValue(); + Assert.assertEquals(UpgradeCatalog260.SELECTED_COLUMN, selectedmappingColumnInfoValue.getName()); + Assert.assertEquals(Integer.class, selectedmappingColumnInfoValue.getType()); + Assert.assertEquals(null, selectedmappingColumnInfoValue.getLength()); + Assert.assertEquals(0, selectedmappingColumnInfoValue.getDefaultValue()); + Assert.assertEquals(false, selectedmappingColumnInfoValue.isNullable()); + + DBColumnInfo selectedTimestampColumnInfoValue = selectedTimestampColumnInfo.getValue(); + Assert.assertEquals(UpgradeCatalog260.SELECTED_TIMESTAMP_COLUMN, selectedTimestampColumnInfoValue.getName()); + Assert.assertEquals(Long.class, selectedTimestampColumnInfoValue.getType()); + Assert.assertEquals(null, selectedTimestampColumnInfoValue.getLength()); + Assert.assertEquals(0, selectedTimestampColumnInfoValue.getDefaultValue()); + Assert.assertEquals(false, selectedTimestampColumnInfoValue.isNullable()); + + DBColumnInfo createTimestampColumnInfoValue = createTimestampColumnInfo.getValue(); + Assert.assertEquals(UpgradeCatalog260.CREATE_TIMESTAMP_COLUMN, createTimestampColumnInfoValue.getName()); + Assert.assertEquals(Long.class, createTimestampColumnInfoValue.getType()); + Assert.assertEquals(null, createTimestampColumnInfoValue.getLength()); + Assert.assertEquals(null, createTimestampColumnInfoValue.getDefaultValue()); + Assert.assertEquals(false, createTimestampColumnInfoValue.isNullable()); + } + + public void expectAddSelectedCollumsToClusterconfigTable(Capture<DBColumnInfo> selectedColumnInfo, Capture<DBColumnInfo> selectedmappingColumnInfo, Capture<DBColumnInfo> selectedTimestampColumnInfo, Capture<DBColumnInfo> createTimestampColumnInfo) throws SQLException { + dbAccessor.copyColumnToAnotherTable(eq(UpgradeCatalog260.CLUSTER_CONFIG_MAPPING_TABLE), capture(selectedmappingColumnInfo), + eq(UpgradeCatalog260.CLUSTER_ID_COLUMN), eq(UpgradeCatalog260.TYPE_NAME_COLUMN), eq(UpgradeCatalog260.VERSION_TAG_COLUMN), eq(UpgradeCatalog260.CLUSTER_CONFIG_TABLE), capture(selectedColumnInfo), + eq(UpgradeCatalog260.CLUSTER_ID_COLUMN), eq(UpgradeCatalog260.TYPE_NAME_COLUMN), eq(UpgradeCatalog260.VERSION_TAG_COLUMN), eq(UpgradeCatalog260.SELECTED_COLUMN), eq(UpgradeCatalog260.SELECTED), eq(0)); + expectLastCall().once(); + + dbAccessor.copyColumnToAnotherTable(eq(UpgradeCatalog260.CLUSTER_CONFIG_MAPPING_TABLE), capture(createTimestampColumnInfo), + eq(UpgradeCatalog260.CLUSTER_ID_COLUMN), eq(UpgradeCatalog260.TYPE_NAME_COLUMN), eq(UpgradeCatalog260.VERSION_TAG_COLUMN), eq(UpgradeCatalog260.CLUSTER_CONFIG_TABLE), capture(selectedTimestampColumnInfo), + eq(UpgradeCatalog260.CLUSTER_ID_COLUMN), eq(UpgradeCatalog260.TYPE_NAME_COLUMN), eq(UpgradeCatalog260.VERSION_TAG_COLUMN), eq(UpgradeCatalog260.SELECTED_COLUMN), eq(UpgradeCatalog260.SELECTED), eq(0)); + expectLastCall().once(); + } + + public void verifyUpdateServiceDesiredStateTable(Capture<DBColumnInfo> sdstadd, Capture<DBColumnInfo> sdstalter) { + DBColumnInfo sdstaddValue = sdstadd.getValue(); + Assert.assertEquals(UpgradeCatalog260.DESIRED_REPO_VERSION_ID_COLUMN, sdstaddValue.getName()); + Assert.assertEquals(1, sdstaddValue.getDefaultValue()); + Assert.assertEquals(Long.class, sdstaddValue.getType()); + Assert.assertEquals(false, sdstaddValue.isNullable()); + Assert.assertEquals(null, sdstaddValue.getLength()); + + DBColumnInfo sdstalterValue = sdstalter.getValue(); + Assert.assertEquals(UpgradeCatalog260.DESIRED_REPO_VERSION_ID_COLUMN, sdstalterValue.getName()); + Assert.assertEquals(null, sdstalterValue.getDefaultValue()); + Assert.assertEquals(Long.class, sdstalterValue.getType()); + Assert.assertEquals(false, sdstalterValue.isNullable()); + Assert.assertEquals(null, sdstalterValue.getLength()); + } + + public void expectUpdateServiceDesiredStateTable(Capture<DBColumnInfo> sdstadd, Capture<DBColumnInfo> sdstalter) throws SQLException { + dbAccessor.addColumn(eq(UpgradeCatalog260.SERVICE_DESIRED_STATE_TABLE), capture(sdstadd)); + expectLastCall().once(); + dbAccessor.alterColumn(eq(UpgradeCatalog260.SERVICE_DESIRED_STATE_TABLE), capture(sdstalter)); + expectLastCall().once(); + + dbAccessor.addFKConstraint(eq(UpgradeCatalog260.SERVICE_DESIRED_STATE_TABLE), eq(UpgradeCatalog260.FK_REPO_VERSION_ID), eq(UpgradeCatalog260.DESIRED_REPO_VERSION_ID_COLUMN), eq(UpgradeCatalog260.REPO_VERSION_TABLE), eq(UpgradeCatalog260.REPO_VERSION_ID_COLUMN), eq(false)); + expectLastCall().once(); + dbAccessor.dropFKConstraint(eq(UpgradeCatalog260.SERVICE_DESIRED_STATE_TABLE), eq(UpgradeCatalog260.FK_SDS_DESIRED_STACK_ID)); + expectLastCall().once(); + dbAccessor.dropColumn(eq(UpgradeCatalog260.SERVICE_DESIRED_STATE_TABLE), eq(UpgradeCatalog260.DESIRED_STACK_ID_COLUMN)); + expectLastCall().once(); + } + + public void verifyUpdateServiceComponentDesiredStateTable(Capture<DBColumnInfo> scdstadd1, Capture<DBColumnInfo> scdstalter1, Capture<DBColumnInfo> scdstadd2, Capture<DBColumnInfo> scdstalter2) { + DBColumnInfo scdstaddValue1 = scdstadd1.getValue(); + Assert.assertEquals(UpgradeCatalog260.DESIRED_REPO_VERSION_ID_COLUMN, scdstaddValue1.getName()); + Assert.assertEquals(1, scdstaddValue1.getDefaultValue()); + Assert.assertEquals(Long.class, scdstaddValue1.getType()); + Assert.assertEquals(false, scdstaddValue1.isNullable()); + + DBColumnInfo scdstalterValue1 = scdstalter1.getValue(); + Assert.assertEquals(UpgradeCatalog260.DESIRED_REPO_VERSION_ID_COLUMN, scdstalterValue1.getName()); + Assert.assertEquals(null, scdstalterValue1.getDefaultValue()); + Assert.assertEquals(Long.class, scdstalterValue1.getType()); + Assert.assertEquals(false, scdstalterValue1.isNullable()); + + DBColumnInfo scdstaddValue2 = scdstadd2.getValue(); + Assert.assertEquals(UpgradeCatalog260.REPO_STATE_COLUMN, scdstaddValue2.getName()); + Assert.assertEquals(UpgradeCatalog260.CURRENT, scdstaddValue2.getDefaultValue()); + Assert.assertEquals(String.class, scdstaddValue2.getType()); + Assert.assertEquals(false, scdstaddValue2.isNullable()); + Assert.assertEquals(Integer.valueOf(255), scdstaddValue2.getLength()); + + DBColumnInfo scdstalterValue2 = scdstalter2.getValue(); + Assert.assertEquals(UpgradeCatalog260.REPO_STATE_COLUMN, scdstalterValue2.getName()); + Assert.assertEquals(UpgradeCatalog260.NOT_REQUIRED, scdstalterValue2.getDefaultValue()); + Assert.assertEquals(String.class, scdstalterValue2.getType()); + Assert.assertEquals(false, scdstalterValue2.isNullable()); + Assert.assertEquals(Integer.valueOf(255), scdstaddValue2.getLength()); + } + + public void verifyGetCurrentVersionID(Capture<String[]> scdcaptureKey, Capture<String[]> scdcaptureValue) { + Assert.assertTrue(Arrays.equals(scdcaptureKey.getValue(), new String[]{UpgradeCatalog260.STATE_COLUMN})); + Assert.assertTrue(Arrays.equals(scdcaptureValue.getValue(), new String[]{UpgradeCatalog260.CURRENT})); + } + + public void expectUpdateServiceComponentDesiredStateTable(Capture<DBColumnInfo> scdstadd1, Capture<DBColumnInfo> scdstalter1, Capture<DBColumnInfo> scdstadd2, Capture<DBColumnInfo> scdstalter2) throws SQLException { + dbAccessor.addColumn(eq(UpgradeCatalog260.SERVICE_COMPONENT_DESIRED_STATE_TABLE), capture(scdstadd1)); + expectLastCall().once(); + + dbAccessor.alterColumn(eq(UpgradeCatalog260.SERVICE_COMPONENT_DESIRED_STATE_TABLE), capture(scdstalter1)); + expectLastCall().once(); + + dbAccessor.addColumn(eq(UpgradeCatalog260.SERVICE_COMPONENT_DESIRED_STATE_TABLE), capture(scdstadd2)); + expectLastCall().once(); + + dbAccessor.alterColumn(eq(UpgradeCatalog260.SERVICE_COMPONENT_DESIRED_STATE_TABLE), capture(scdstalter2)); + expectLastCall().once(); + + dbAccessor.addFKConstraint(eq(UpgradeCatalog260.SERVICE_COMPONENT_DESIRED_STATE_TABLE), eq(UpgradeCatalog260.FK_SCDS_DESIRED_REPO_ID), eq(UpgradeCatalog260.DESIRED_REPO_VERSION_ID_COLUMN), eq(UpgradeCatalog260.REPO_VERSION_TABLE), eq(UpgradeCatalog260.REPO_VERSION_ID_COLUMN), eq(false)); + expectLastCall().once(); + + dbAccessor.dropFKConstraint(eq(UpgradeCatalog260.SERVICE_COMPONENT_DESIRED_STATE_TABLE), eq(UpgradeCatalog260.FK_SCDS_DESIRED_STACK_ID)); + expectLastCall().once(); + dbAccessor.dropColumn(eq(UpgradeCatalog260.SERVICE_COMPONENT_DESIRED_STATE_TABLE), eq(UpgradeCatalog260.DESIRED_STACK_ID_COLUMN)); + expectLastCall().once(); + dbAccessor.dropColumn(eq(UpgradeCatalog260.SERVICE_COMPONENT_DESIRED_STATE_TABLE), eq(UpgradeCatalog260.DESIRED_VERSION_COLUMN)); + expectLastCall().once(); + } + + public void expectGetCurrentVersionID(List<Integer> current, Capture<String[]> scdcaptureKey, Capture<String[]> scdcaptureValue) throws SQLException { + expect(dbAccessor.getIntColumnValues(eq("cluster_version"), eq("repo_version_id"), + capture(scdcaptureKey), capture(scdcaptureValue), eq(false))).andReturn(current).once(); + } + + +}
