Extend UpdateUI to support interactive input

This change extends the existing UpdateUI to make it possible
to read string from users during the schema upgrade process.

One usage of this change is that we can allow administrators to
decide the target change status (work-in-progress or private)
during the migration of draft changes.

Change-Id: I8f7f09618e2bc76129e7fbb9613aa5b90a5e1558
This commit is contained in:
Changcheng Xiao 2017-08-09 11:30:07 +02:00
parent 1121ca1834
commit 62dd1b394c
4 changed files with 51 additions and 35 deletions

View File

@ -73,6 +73,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -387,14 +388,25 @@ public class BaseInit extends SiteProgram {
schemaUpdater.update(
new UpdateUI() {
@Override
public void message(String msg) {
System.err.println(msg);
public void message(String message) {
System.err.println(message);
System.err.flush();
}
@Override
public boolean yesno(boolean def, String msg) {
return ui.yesno(def, msg);
public boolean yesno(boolean defaultValue, String message) {
return ui.yesno(defaultValue, message);
}
@Override
public void waitForUser() {
ui.waitForUser();
}
@Override
public String readString(
String defaultValue, Set<String> allowedValues, String message) {
return ui.readString(defaultValue, allowedValues, message);
}
@Override

View File

@ -17,11 +17,24 @@ package com.google.gerrit.server.schema;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.StatementExecutor;
import java.util.List;
import java.util.Set;
public interface UpdateUI {
void message(String msg);
boolean yesno(boolean def, String msg);
void message(String message);
/** Requests the user to answer a yes/no question. */
boolean yesno(boolean defaultValue, String message);
/** Prints a message asking the user to let us know when it's safe to continue. */
void waitForUser();
/**
* Prompts the user for a string, suggesting a default.
*
* @return the chosen string from the list of allowed values.
*/
String readString(String defaultValue, Set<String> allowedValues, String message);
boolean isBatch();

View File

@ -34,9 +34,9 @@ import com.google.gerrit.server.notedb.NotesMigration;
import com.google.gerrit.testutil.InMemoryDatabase;
import com.google.gerrit.testutil.InMemoryH2Type;
import com.google.gerrit.testutil.InMemoryRepositoryManager;
import com.google.gerrit.testutil.TestUpdateUI;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.SchemaFactory;
import com.google.gwtorm.server.StatementExecutor;
import com.google.inject.Guice;
import com.google.inject.Key;
import com.google.inject.ProvisionException;
@ -45,7 +45,6 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.PersonIdent;
@ -133,28 +132,7 @@ public class SchemaUpdaterTest {
}
}
u.update(
new UpdateUI() {
@Override
public void message(String msg) {}
@Override
public boolean yesno(boolean def, String msg) {
return def;
}
@Override
public boolean isBatch() {
return true;
}
@Override
public void pruneSchema(StatementExecutor e, List<String> pruneList) throws OrmException {
for (String sql : pruneList) {
e.execute(sql);
}
}
});
u.update(new TestUpdateUI());
db.assertSchemaVersion();
final SystemConfig sc = db.getSystemConfig();

View File

@ -18,21 +18,34 @@ import com.google.gerrit.server.schema.UpdateUI;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.StatementExecutor;
import java.util.List;
import java.util.Set;
public class TestUpdateUI implements UpdateUI {
@Override
public void message(String msg) {}
public void message(String message) {}
@Override
public boolean yesno(boolean def, String msg) {
return false;
public boolean yesno(boolean defaultValue, String message) {
return defaultValue;
}
@Override
public void waitForUser() {}
@Override
public String readString(String defaultValue, Set<String> allowedValues, String message) {
return defaultValue;
}
@Override
public boolean isBatch() {
return false;
return true;
}
@Override
public void pruneSchema(StatementExecutor e, List<String> pruneList) throws OrmException {}
public void pruneSchema(StatementExecutor e, List<String> pruneList) throws OrmException {
for (String sql : pruneList) {
e.execute(sql);
}
}
}