Use new SqlDialect.dropIndex method in schema upgrades
Change-Id: I5145812d08077326e3924a5e99ac6c9c774c7769
This commit is contained in:
@@ -19,11 +19,11 @@ import com.google.gwtorm.jdbc.JdbcSchema;
|
||||
import com.google.gwtorm.schema.sql.DialectPostgreSQL;
|
||||
import com.google.gwtorm.schema.sql.SqlDialect;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.gwtorm.server.StatementExecutor;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -38,29 +38,30 @@ public class Schema_102 extends SchemaVersion {
|
||||
throws OrmException, SQLException {
|
||||
JdbcSchema schema = (JdbcSchema) db;
|
||||
SqlDialect dialect = schema.getDialect();
|
||||
try (Statement stmt = newStatement(db)) {
|
||||
try (StatementExecutor e = newExecutor(db)) {
|
||||
// Drop left over indexes that were missed to be removed in schema 84.
|
||||
// See "Delete SQL index support" commit for more details:
|
||||
// d4ae3a16d5e1464574bd04f429a63eb9c02b3b43
|
||||
Pattern pattern =
|
||||
Pattern.compile("^changes_(allOpen|allClosed|byBranchClosed)$",
|
||||
Pattern.CASE_INSENSITIVE);
|
||||
String table = "changes";
|
||||
Set<String> listIndexes = dialect.listIndexes(
|
||||
schema.getConnection(), "changes");
|
||||
schema.getConnection(), table);
|
||||
for (String index : listIndexes) {
|
||||
if (pattern.matcher(index).matches()) {
|
||||
stmt.executeUpdate("DROP INDEX " + index);
|
||||
dialect.dropIndex(e, table, index);
|
||||
}
|
||||
}
|
||||
|
||||
stmt.executeUpdate("DROP INDEX changes_byProjectOpen");
|
||||
dialect.dropIndex(e, table, "changes_byProjectOpen");
|
||||
if (dialect instanceof DialectPostgreSQL) {
|
||||
stmt.executeUpdate("CREATE INDEX changes_byProjectOpen"
|
||||
+ " ON changes (dest_project_name, last_updated_on)"
|
||||
e.execute("CREATE INDEX changes_byProjectOpen"
|
||||
+ " ON " + table + " (dest_project_name, last_updated_on)"
|
||||
+ " WHERE open = 'Y'");
|
||||
} else {
|
||||
stmt.executeUpdate("CREATE INDEX changes_byProjectOpen"
|
||||
+ " ON changes (open, dest_project_name, last_updated_on)");
|
||||
e.execute("CREATE INDEX changes_byProjectOpen"
|
||||
+ " ON " + table + " (open, dest_project_name, last_updated_on)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.google.gwtorm.jdbc.JdbcSchema;
|
||||
import com.google.gwtorm.schema.sql.DialectMySQL;
|
||||
import com.google.gwtorm.schema.sql.SqlDialect;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.gwtorm.server.StatementExecutor;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
@@ -101,30 +102,30 @@ public class Schema_82 extends SchemaVersion {
|
||||
|
||||
private void renameIndexes(ReviewDb db) {
|
||||
SqlDialect dialect = ((JdbcSchema) db).getDialect();
|
||||
try (Statement stmt = newStatement(db)) {
|
||||
// Use a new executor so we can ignore errors.
|
||||
try (StatementExecutor e = newExecutor(db)) {
|
||||
// MySQL doesn't have alter index stmt, drop & create
|
||||
if (dialect instanceof DialectMySQL) {
|
||||
for (Map.Entry<String, Index> entry : indexes.entrySet()) {
|
||||
stmt.executeUpdate("DROP INDEX " + entry.getKey() + " ON "
|
||||
+ entry.getValue().table);
|
||||
dialect.dropIndex(e, entry.getValue().table, entry.getKey());
|
||||
}
|
||||
stmt.executeUpdate("CREATE INDEX account_project_watches_byP ON " +
|
||||
e.execute("CREATE INDEX account_project_watches_byP ON " +
|
||||
"account_project_watches (project_name)");
|
||||
stmt.executeUpdate("CREATE INDEX patch_set_approvals_closedByU ON " +
|
||||
e.execute("CREATE INDEX patch_set_approvals_closedByU ON " +
|
||||
"patch_set_approvals (change_open, account_id, change_sort_key)");
|
||||
stmt.executeUpdate("CREATE INDEX submodule_subscr_acc_bys ON " +
|
||||
e.execute("CREATE INDEX submodule_subscr_acc_bys ON " +
|
||||
"submodule_subscriptions (submodule_project_name, " +
|
||||
"submodule_branch_name)");
|
||||
} else {
|
||||
for (Map.Entry<String, Index> entry : indexes.entrySet()) {
|
||||
stmt.executeUpdate("ALTER INDEX " + entry.getKey() + " RENAME TO "
|
||||
e.execute("ALTER INDEX " + entry.getKey() + " RENAME TO "
|
||||
+ entry.getValue().index);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
// we don't care
|
||||
// better we would check if index was already renamed
|
||||
// gwtorm doesn't expose this functionality
|
||||
} catch (OrmException e) {
|
||||
// We don't care; better, we could check if index was already renamed, but
|
||||
// gwtorm didn't expose this functionality at the time this schema upgrade
|
||||
// was written.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,17 +14,15 @@
|
||||
|
||||
package com.google.gerrit.server.schema;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.gerrit.reviewdb.server.ReviewDb;
|
||||
import com.google.gwtorm.jdbc.JdbcSchema;
|
||||
import com.google.gwtorm.schema.sql.DialectMySQL;
|
||||
import com.google.gwtorm.schema.sql.SqlDialect;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.gwtorm.server.StatementExecutor;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class Schema_89 extends SchemaVersion {
|
||||
@Inject
|
||||
@@ -36,16 +34,11 @@ public class Schema_89 extends SchemaVersion {
|
||||
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException,
|
||||
SQLException {
|
||||
SqlDialect dialect = ((JdbcSchema) db).getDialect();
|
||||
try (Statement stmt = newStatement(db)) {
|
||||
for (String name : ImmutableList.of(
|
||||
"patch_set_approvals_openByUser",
|
||||
"patch_set_approvals_closedByU")) {
|
||||
if (dialect instanceof DialectMySQL) {
|
||||
stmt.executeUpdate("DROP INDEX " + name + " ON patch_set_approvals");
|
||||
} else {
|
||||
stmt.executeUpdate("DROP INDEX " + name);
|
||||
}
|
||||
}
|
||||
try (StatementExecutor e = newExecutor(db)) {
|
||||
dialect.dropIndex(e, "patch_set_approvals",
|
||||
"patch_set_approvals_openByUser");
|
||||
dialect.dropIndex(e, "patch_set_approvals",
|
||||
"patch_set_approvals_closedByU");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user