Add error handling if rename was not successful

DatabaseRenameHandler:
- Fix error handling if rename procedure was not successful.
- Add more information regarding exceptions in logs.

Manual testing shows that in reviewDb case rollback was not being
performed due to autoCommit was set to true before performing rollback
making it ineffective.

Bug: Issue 11734
Change-Id: I252b7071b5575241a7aba1c3d3537b4775c024a2
diff --git a/src/main/java/com/googlesource/gerrit/plugins/renameproject/RenameRevertException.java b/src/main/java/com/googlesource/gerrit/plugins/renameproject/RenameRevertException.java
new file mode 100644
index 0000000..fcb6021
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/renameproject/RenameRevertException.java
@@ -0,0 +1,25 @@
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed 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 com.googlesource.gerrit.plugins.renameproject;
+
+import com.google.gwtorm.server.OrmException;
+
+/** Add cause for exception during revert operation */
+public class RenameRevertException extends OrmException {
+  public RenameRevertException(Throwable revertException, Throwable cause) {
+    super(
+        "Failed to revert after failed rename. Revert cause: " + cause.getMessage(),
+        revertException.initCause(cause));
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/renameproject/database/DatabaseRenameHandler.java b/src/main/java/com/googlesource/gerrit/plugins/renameproject/database/DatabaseRenameHandler.java
index 2d123a5..cac3338 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/renameproject/database/DatabaseRenameHandler.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/renameproject/database/DatabaseRenameHandler.java
@@ -34,6 +34,7 @@
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 import com.google.inject.Singleton;
+import com.googlesource.gerrit.plugins.renameproject.RenameRevertException;
 import com.googlesource.gerrit.plugins.renameproject.monitor.ProgressMonitor;
 import java.io.IOException;
 import java.sql.Connection;
@@ -99,7 +100,7 @@
         changeIds.add(changeId);
       }
       log.debug(
-          "Number of changes in reviewDb related to the project {} are {}",
+          "Number of changes in reviewDb related to project {} are {}",
           oldProjectKey.get(),
           changeIds.size());
       return changeIds;
@@ -119,7 +120,7 @@
       changeIds.add(change.id());
     }
     log.debug(
-        "Number of changes in noteDb related to the project {} are {}",
+        "Number of changes in noteDb related to project {} are {}",
         oldProjectKey.get(),
         changeIds.size());
     return changeIds;
@@ -134,7 +135,7 @@
       Project.NameKey oldProjectKey,
       Project.NameKey newProjectKey,
       ProgressMonitor pm)
-      throws OrmException, IOException {
+      throws OrmException, RenameRevertException {
     pm.beginTask("Updating changes in the database");
     ReviewDb db = schemaFactory.open();
     return (isNoteDb())
@@ -152,34 +153,56 @@
     try (Statement stmt = conn.createStatement()) {
       conn.setAutoCommit(false);
       try {
-        log.debug("Updating the changes in reviewDb related to project {}", oldProjectKey.get());
-        for (Change.Id cd : changes) {
-          stmt.addBatch(
-              "update changes set dest_project_name='"
-                  + newProjectKey.get()
-                  + "' where change_id ="
-                  + cd.id
-                  + ";");
+        try {
+          log.debug("Updating the changes in reviewDb related to project {}", oldProjectKey.get());
+          for (Change.Id cd : changes) {
+            stmt.addBatch(
+                "update changes set dest_project_name='"
+                    + newProjectKey.get()
+                    + "' where change_id ="
+                    + cd.id
+                    + ";");
+          }
+          stmt.executeBatch();
+          conn.commit();
+        } catch (SQLException e) {
+          throw new OrmException(e);
         }
-        stmt.executeBatch();
         updateWatchEntries(oldProjectKey, newProjectKey);
-        conn.commit();
         log.debug(
             "Successfully updated the changes in reviewDb related to project {}",
             oldProjectKey.get());
         return changes;
+      } catch (OrmException e) {
+        try {
+          log.error(
+              "Failed to update changes in reviewDb for project {}, exception caught: {}. Rolling back the operation.",
+              oldProjectKey.get(),
+              e.toString());
+          conn.rollback();
+        } catch (SQLException revertEx) {
+          log.error(
+              "Failed to rollback changes in reviewDb from project {} to project {}, exception caught: {}",
+              newProjectKey.get(),
+              oldProjectKey.get(),
+              revertEx.toString());
+          throw new RenameRevertException(revertEx, e);
+        }
+        try {
+          updateWatchEntries(newProjectKey, oldProjectKey);
+        } catch (OrmException revertEx) {
+          log.error(
+              "Failed to update watched changes in reviewDb from project {} to project {}, exception caught: {}",
+              newProjectKey.get(),
+              oldProjectKey.get(),
+              revertEx.toString());
+          throw new RenameRevertException(revertEx, e);
+        }
+        throw e;
       } finally {
         conn.setAutoCommit(true);
       }
     } catch (SQLException e) {
-      try {
-        log.error(
-            "Failed to update changes in reviewDb for the project {}, rolling back the operation.",
-            oldProjectKey.get());
-        conn.rollback();
-      } catch (SQLException ex) {
-        throw new OrmException(ex);
-      }
       throw new OrmException(e);
     }
   }
@@ -232,12 +255,14 @@
                 a.getUserName(),
                 newProjectKey.get(),
                 e);
+            throw new OrmException(e);
           } catch (IOException e) {
             log.error(
                 "Updating watch entry for user {} in project {} failed.",
                 a.getUserName(),
                 newProjectKey.get(),
                 e);
+            throw new OrmException(e);
           }
         }
       }