Fix database migration to schema 119

When upgrading directly from schema 116 (2.12 release) to schema 119
there is a problem with COPY_SELF_ON_EMAIL column, that was replaced by
EMAIL_STRATEGY in schema 116. That's because the new column
EMAIL_STRATEGY was removed in schema 119 and thus is not added to the
accounts relation during update schema step. This leads to two different
problems:

1. data migation from COPY_SELF_ON_EMAIL to EMAIL_STRATEGY in schema 116
is failing now because there is no such column EMAIL_STRATEGY
2. select the data in data migration step in schema 119 is failing as
well, because there is no such column EMAIL_STRATEGY

Rectify by moving the data migration step from schema 116 to schema 119
for this column and use conditionally old or new column name in the
select statement in migration step in schema 119.

Test Plan:

Scenario I: upgrade before schema 116

* install gerrit 2.12
* upgrade to master

Scenario II: upgrade after schema 116

* upgrade Gerrit to schema 116 (If40d0d34 or later)
* upgrade to master

Bug: Issue 3978
Change-Id: I72d98fe8782704f6da90e5a970243e16ccab735b
This commit is contained in:
David Ostrovsky
2016-03-19 16:01:33 +01:00
committed by Hugo Arès
parent 02f71327cf
commit 51759c1513
2 changed files with 30 additions and 23 deletions

View File

@@ -14,30 +14,12 @@
package com.google.gerrit.server.schema;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gwtorm.jdbc.JdbcSchema;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.sql.SQLException;
import java.sql.Statement;
public class Schema_116 extends SchemaVersion {
@Inject
Schema_116(Provider<Schema_115> prior) {
super(prior);
}
@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws SQLException {
ui.message("Migrate user preference copySelfOnEmail to emailStrategy");
try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement()) {
stmt.executeUpdate("UPDATE accounts SET "
+ "EMAIL_STRATEGY='ENABLED' "
+ "WHERE (COPY_SELF_ON_EMAIL='N')");
stmt.executeUpdate("UPDATE accounts SET "
+ "EMAIL_STRATEGY='CC_ON_OWN_COMMENTS' "
+ "WHERE (COPY_SELF_ON_EMAIL='Y')");
}
}
}

View File

@@ -53,11 +53,13 @@ import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevWalk;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Schema_119 extends SchemaVersion {
private static final Map<String, String> LEGACY_DISPLAYNAME_MAP =
@@ -86,6 +88,12 @@ public class Schema_119 extends SchemaVersion {
@Override
protected void migrateData(ReviewDb db, UpdateUI ui)
throws OrmException, SQLException {
JdbcSchema schema = (JdbcSchema) db;
Connection connection = schema.getConnection();
String tableName = "accounts";
String emailStrategy = "email_strategy";
Set<String> columns =
schema.getDialect().listColumns(connection, tableName);
Map<Account.Id, GeneralPreferencesInfo> imports = new HashMap<>();
try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
ResultSet rs = stmt.executeQuery(
@@ -96,7 +104,9 @@ public class Schema_119 extends SchemaVersion {
+ "use_flash_clipboard, "
+ "download_url, "
+ "download_command, "
+ "email_strategy, "
+ (columns.contains(emailStrategy)
? emailStrategy + ", "
: "copy_self_on_email, ")
+ "date_format, "
+ "time_format, "
+ "relative_date_in_change_table, "
@@ -105,7 +115,7 @@ public class Schema_119 extends SchemaVersion {
+ "legacycid_in_change_table, "
+ "review_category_strategy, "
+ "mute_common_path_prefixes "
+ "from accounts")) {
+ "from " + tableName)) {
while (rs.next()) {
GeneralPreferencesInfo p =
new GeneralPreferencesInfo();
@@ -115,7 +125,8 @@ public class Schema_119 extends SchemaVersion {
p.useFlashClipboard = toBoolean(rs.getString(4));
p.downloadScheme = convertToModernNames(rs.getString(5));
p.downloadCommand = toDownloadCommand(rs.getString(6));
p.emailStrategy = toEmailStrategy(rs.getString(7));
p.emailStrategy = toEmailStrategy(rs.getString(7),
columns.contains(emailStrategy));
p.dateFormat = toDateFormat(rs.getString(8));
p.timeFormat = toTimeFormat(rs.getString(9));
p.relativeDateInChangeTable = toBoolean(rs.getString(10));
@@ -191,11 +202,25 @@ public class Schema_119 extends SchemaVersion {
return DiffView.valueOf(v);
}
private static EmailStrategy toEmailStrategy(String v) {
private static EmailStrategy toEmailStrategy(String v,
boolean emailStrategyColumnExists) throws OrmException {
if (v == null) {
return EmailStrategy.ENABLED;
}
return EmailStrategy.valueOf(v);
if (emailStrategyColumnExists) {
return EmailStrategy.valueOf(v);
} else {
if (v.equals("N")) {
// EMAIL_STRATEGY='ENABLED' WHERE (COPY_SELF_ON_EMAIL='N')
return EmailStrategy.ENABLED;
} else if (v.equals("Y")) {
// EMAIL_STRATEGY='CC_ON_OWN_COMMENTS' WHERE (COPY_SELF_ON_EMAIL='Y')
return EmailStrategy.CC_ON_OWN_COMMENTS;
} else {
throw new OrmException(
"invalid value in accounts.copy_self_on_email: " + v);
}
}
}
private static ReviewCategoryStrategy toReviewCategoryStrategy(String v) {