Schema versions: Pass arguments into upgrade method, not to the constructor
Schema migrations do all their work in the upgrade method. This is the place where they need to access the arguments. If the arguments are passed into the constructor, each schema migration must save the arguments in a local member veriable to make them accessible from the upgrade method. To save this boilerplate code we now directly pass the arguments into the upgrade method. This means schema versions don't need to define any constructor since the default constructor is all that is needed. Signed-off-by: Edwin Kempin <ekempin@google.com> Change-Id: Ie35d088bde76c0d11440b073034ecdae8df59a2c
This commit is contained in:
@@ -91,7 +91,7 @@ public class NoteDbSchemaUpdater {
|
||||
for (int nextVersion : requiredUpgrades(currentVersion, schemaVersions.keySet())) {
|
||||
try {
|
||||
ui.message(String.format("Migrating data to schema %d ...", nextVersion));
|
||||
NoteDbSchemaVersions.get(schemaVersions, nextVersion, args).upgrade(ui);
|
||||
NoteDbSchemaVersions.get(schemaVersions, nextVersion).upgrade(args, ui);
|
||||
versionManager.increment(nextVersion - 1);
|
||||
} catch (Exception e) {
|
||||
throw new OrmException(
|
||||
|
||||
@@ -22,9 +22,8 @@ import com.google.inject.Singleton;
|
||||
/**
|
||||
* Schema upgrade implementation.
|
||||
*
|
||||
* <p>Implementations must define a single public constructor that takes an {@link Arguments}. The
|
||||
* recommended idiom is to pull out whichever individual fields from the {@code Arguments} are
|
||||
* required by this implementation.
|
||||
* <p>Implementations must have a single non-private constructor with no arguments (e.g. the default
|
||||
* constructor).
|
||||
*/
|
||||
interface NoteDbSchemaVersion {
|
||||
@Singleton
|
||||
@@ -39,5 +38,5 @@ interface NoteDbSchemaVersion {
|
||||
}
|
||||
}
|
||||
|
||||
void upgrade(UpdateUI ui) throws Exception;
|
||||
void upgrade(Arguments args, UpdateUI ui) throws Exception;
|
||||
}
|
||||
|
||||
@@ -45,13 +45,11 @@ public class NoteDbSchemaVersions {
|
||||
}
|
||||
|
||||
public static NoteDbSchemaVersion get(
|
||||
ImmutableSortedMap<Integer, Class<? extends NoteDbSchemaVersion>> schemaVersions,
|
||||
int i,
|
||||
NoteDbSchemaVersion.Arguments args) {
|
||||
ImmutableSortedMap<Integer, Class<? extends NoteDbSchemaVersion>> schemaVersions, int i) {
|
||||
Class<? extends NoteDbSchemaVersion> clazz = schemaVersions.get(i);
|
||||
checkArgument(clazz != null, "Schema version not found: %s", i);
|
||||
try {
|
||||
return clazz.getDeclaredConstructor(NoteDbSchemaVersion.Arguments.class).newInstance(args);
|
||||
return clazz.getDeclaredConstructor().newInstance();
|
||||
} catch (InstantiationException
|
||||
| IllegalAccessException
|
||||
| NoSuchMethodException
|
||||
|
||||
@@ -15,13 +15,8 @@
|
||||
package com.google.gerrit.server.schema;
|
||||
|
||||
public class Schema_180 implements NoteDbSchemaVersion {
|
||||
@SuppressWarnings("unused")
|
||||
Schema_180(Arguments args) {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void upgrade(UpdateUI ui) {
|
||||
public void upgrade(Arguments args, UpdateUI ui) {
|
||||
// Do nothing; only used to populate the version ref, which is done by the caller.
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user