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>
This commit is contained in:
Shawn O. Pearce
2010-02-16 16:29:09 -08:00
parent feebfbe636
commit 29103b77f9
2 changed files with 16 additions and 5 deletions

View File

@@ -19,6 +19,7 @@ import com.google.gerrit.reviewdb.ReviewDb;
import com.google.gerrit.reviewdb.SystemConfig; import com.google.gerrit.reviewdb.SystemConfig;
import com.google.gwtorm.client.OrmException; import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.jdbc.JdbcSchema; import com.google.gwtorm.jdbc.JdbcSchema;
import com.google.gwtorm.schema.sql.DialectH2;
import com.google.gwtorm.schema.sql.DialectMySQL; import com.google.gwtorm.schema.sql.DialectMySQL;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Provider; import com.google.inject.Provider;
@@ -55,6 +56,10 @@ class Schema_21 extends SchemaVersion {
if (jdbc.getDialect() instanceof DialectMySQL) { if (jdbc.getDialect() instanceof DialectMySQL) {
s.execute("DROP FUNCTION nextval_project_id"); 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 { } finally {
s.close(); s.close();

View File

@@ -22,6 +22,7 @@ import com.google.gerrit.reviewdb.ReviewDb;
import com.google.gerrit.reviewdb.AccountExternalId.Key; import com.google.gerrit.reviewdb.AccountExternalId.Key;
import com.google.gwtorm.client.OrmException; import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.jdbc.JdbcSchema; import com.google.gwtorm.jdbc.JdbcSchema;
import com.google.gwtorm.schema.sql.DialectH2;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Provider; import com.google.inject.Provider;
@@ -39,15 +40,15 @@ class Schema_22 extends SchemaVersion {
@Override @Override
protected void migrateData(ReviewDb db) throws OrmException, SQLException { protected void migrateData(ReviewDb db) throws OrmException, SQLException {
Collection<AccountExternalId> ids = new ArrayList<AccountExternalId>(); Statement s = ((JdbcSchema) db).getConnection().createStatement();
Statement queryStmt = ((JdbcSchema) db).getConnection().createStatement();
try { try {
ResultSet results = ResultSet results =
queryStmt.executeQuery(// s.executeQuery(//
"SELECT account_id, ssh_user_name" "SELECT account_id, ssh_user_name"
+ " FROM accounts" // + " FROM accounts" //
+ " WHERE ssh_user_name IS NOT NULL" + " WHERE ssh_user_name IS NOT NULL"
+ " AND ssh_user_name <> ''"); + " AND ssh_user_name <> ''");
Collection<AccountExternalId> ids = new ArrayList<AccountExternalId>();
while (results.next()) { while (results.next()) {
final int accountId = results.getInt(1); final int accountId = results.getInt(1);
final String userName = results.getString(2); final String userName = results.getString(2);
@@ -56,10 +57,15 @@ class Schema_22 extends SchemaVersion {
final AccountExternalId.Key key = toKey(userName); final AccountExternalId.Key key = toKey(userName);
ids.add(new AccountExternalId(account, key)); 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 { } finally {
queryStmt.close(); s.close();
} }
db.accountExternalIds().insert(ids);
} }
private Key toKey(final String userName) { private Key toKey(final String userName) {