Fix upgrading H2 from schema 20 to current

We can't drop the unused objects out because H2 requires us to
first drop the constraints that apply to these columns.

Change-Id: I02313f1c4a4180bcd0cce24a15bdffe817606987
Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_21.java b/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_21.java
index c43818c..7ecf66c 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_21.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_21.java
@@ -19,6 +19,7 @@
 import com.google.gerrit.reviewdb.SystemConfig;
 import com.google.gwtorm.client.OrmException;
 import com.google.gwtorm.jdbc.JdbcSchema;
+import com.google.gwtorm.schema.sql.DialectH2;
 import com.google.gwtorm.schema.sql.DialectMySQL;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
@@ -55,6 +56,10 @@
 
       if (jdbc.getDialect() instanceof DialectMySQL) {
         s.execute("DROP FUNCTION nextval_project_id");
+
+      } else if (jdbc.getDialect() instanceof DialectH2) {
+        s.execute("ALTER TABLE projects DROP CONSTRAINT"
+            + " IF EXISTS CONSTRAINT_F3");
       }
     } finally {
       s.close();
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_22.java b/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_22.java
index a1269a1..2de38d1 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_22.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_22.java
@@ -22,6 +22,7 @@
 import com.google.gerrit.reviewdb.AccountExternalId.Key;
 import com.google.gwtorm.client.OrmException;
 import com.google.gwtorm.jdbc.JdbcSchema;
+import com.google.gwtorm.schema.sql.DialectH2;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 
@@ -39,15 +40,15 @@
 
   @Override
   protected void migrateData(ReviewDb db) throws OrmException, SQLException {
-    Collection<AccountExternalId> ids = new ArrayList<AccountExternalId>();
-    Statement queryStmt = ((JdbcSchema) db).getConnection().createStatement();
+    Statement s = ((JdbcSchema) db).getConnection().createStatement();
     try {
       ResultSet results =
-          queryStmt.executeQuery(//
+          s.executeQuery(//
               "SELECT account_id, ssh_user_name"
                   + " FROM accounts" //
                   + " WHERE ssh_user_name IS NOT NULL"
                   + " AND ssh_user_name <> ''");
+      Collection<AccountExternalId> ids = new ArrayList<AccountExternalId>();
       while (results.next()) {
         final int accountId = results.getInt(1);
         final String userName = results.getString(2);
@@ -56,10 +57,15 @@
         final AccountExternalId.Key key = toKey(userName);
         ids.add(new AccountExternalId(account, key));
       }
+      db.accountExternalIds().insert(ids);
+
+      if (((JdbcSchema) db).getDialect() instanceof DialectH2) {
+        s.execute("ALTER TABLE accounts DROP CONSTRAINT"
+            + " IF EXISTS CONSTRAINT_AF");
+      }
     } finally {
-      queryStmt.close();
+      s.close();
     }
-    db.accountExternalIds().insert(ids);
   }
 
   private Key toKey(final String userName) {