ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nc...@apache.org
Subject [06/50] ambari git commit: AMBARI-20387 - Upgrades on MySQL Should Also Drop Indexes With Foreign Key Names (jonathanhurley)
Date Tue, 14 Mar 2017 18:41:50 GMT
AMBARI-20387 - Upgrades on MySQL Should Also Drop Indexes With Foreign Key Names (jonathanhurley)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/d4839a9b
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/d4839a9b
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/d4839a9b

Branch: refs/heads/branch-feature-AMBARI-12556
Commit: d4839a9bc2942cb5aca7659b95f59d6bcb9ff95d
Parents: eb41a37
Author: Jonathan Hurley <jhurley@hortonworks.com>
Authored: Thu Mar 9 16:29:52 2017 -0500
Committer: Jonathan Hurley <jhurley@hortonworks.com>
Committed: Fri Mar 10 08:44:44 2017 -0500

----------------------------------------------------------------------
 .../apache/ambari/server/orm/DBAccessor.java    | 11 ++++++--
 .../ambari/server/orm/DBAccessorImpl.java       | 29 +++++++++++---------
 2 files changed, 24 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/d4839a9b/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 5d9eb6c..c132a3d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/DBAccessor.java
@@ -352,9 +352,14 @@ public interface DBAccessor {
   void dropSequence(String sequenceName) throws SQLException;
 
   /**
-   * Drop a FK constraint from table
-   * @param tableName name of the table
-   * @param constraintName name of the constraint
+   * Drops a FK constraint from a table. In the case of
+   * {@link DatabaseType#MYSQL}, this will also ensure that any associated
+   * indexes are also dropped.
+   *
+   * @param tableName
+   *          name of the table
+   * @param constraintName
+   *          name of the constraint
    * @throws SQLException
    */
   void dropFKConstraint(String tableName, String constraintName) throws SQLException;

http://git-wip-us.apache.org/repos/asf/ambari/blob/d4839a9b/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 1c0ab30..1dd3b54 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
@@ -285,7 +285,7 @@ public class DBAccessorImpl implements DBAccessor {
 
   @Override
   public boolean tableHasColumn(String tableName, String... columnName) throws SQLException
{
-    List<String> columnsList = new ArrayList<String>(Arrays.asList(columnName));
+    List<String> columnsList = new ArrayList<>(Arrays.asList(columnName));
     DatabaseMetaData metaData = getDatabaseMetaData();
 
     CustomStringUtils.toUpperCase(columnsList);
@@ -376,11 +376,11 @@ public class DBAccessorImpl implements DBAccessor {
     ResultSet rs = metaData.getCrossReference(null, dbSchema, convertObjectName(referenceTableName),
             null, dbSchema, convertObjectName(tableName));
 
-    List<String> pkColumns = new ArrayList<String>(referenceColumns.length);
+    List<String> pkColumns = new ArrayList<>(referenceColumns.length);
     for (String referenceColumn : referenceColumns) {
       pkColumns.add(convertObjectName(referenceColumn));
     }
-    List<String> fkColumns = new ArrayList<String>(keyColumns.length);
+    List<String> fkColumns = new ArrayList<>(keyColumns.length);
     for (String keyColumn : keyColumns) {
       fkColumns.add(convertObjectName(keyColumn));
     }
@@ -888,23 +888,26 @@ public class DBAccessorImpl implements DBAccessor {
     dropFKConstraint(tableName, constraintName, false);
   }
 
+  /**
+   * {@inheritDoc}
+   */
   @Override
   public void dropFKConstraint(String tableName, String constraintName, boolean ignoreFailure)
throws SQLException {
-    // ToDo: figure out if name of index and constraint differs
     String checkedConstraintName = getCheckedForeignKey(convertObjectName(tableName), constraintName);
     if (checkedConstraintName != null) {
       String query = dbmsHelper.getDropFKConstraintStatement(tableName, checkedConstraintName);
       executeQuery(query, ignoreFailure);
+    } else {
+      LOG.warn("Foreign key {} from {} table does not exist and will not be dropped",
+          constraintName, tableName);
+    }
 
-      // MySQL also adds indexes in addition to the FK which should be dropped
-      Configuration.DatabaseType databaseType = configuration.getDatabaseType();
-      if (databaseType == DatabaseType.MYSQL) {
-        query = dbmsHelper.getDropIndexStatement(constraintName, tableName);
+    // even if the FK didn't exist, the index constraint might, so check it
+    // indepedently of the FK (but only on MySQL)
+    Configuration.DatabaseType databaseType = configuration.getDatabaseType();
+    if (databaseType == DatabaseType.MYSQL && tableHasIndex(tableName, false, constraintName))
{
+        String query = dbmsHelper.getDropIndexStatement(constraintName, tableName);
         executeQuery(query, true);
-      }
-
-    } else {
-      LOG.warn("Constraint {} from {} table not found, nothing to drop", constraintName,
tableName);
     }
   }
 
@@ -1125,7 +1128,7 @@ public class DBAccessorImpl implements DBAccessor {
   public List<String> getIndexesList(String tableName, boolean unique)
     throws SQLException{
     ResultSet rs = getDatabaseMetaData().getIndexInfo(null, dbSchema, convertObjectName(tableName),
unique, false);
-    List<String> indexList = new ArrayList<String>();
+    List<String> indexList = new ArrayList<>();
     if (rs != null){
       try{
         while (rs.next()) {


Mime
View raw message