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:
Edwin Kempin
2019-02-01 09:51:19 +01:00
parent e3eaf9e352
commit c95a285ec3
6 changed files with 12 additions and 31 deletions

View File

@@ -91,7 +91,7 @@ public class NoteDbSchemaUpdater {
for (int nextVersion : requiredUpgrades(currentVersion, schemaVersions.keySet())) { for (int nextVersion : requiredUpgrades(currentVersion, schemaVersions.keySet())) {
try { try {
ui.message(String.format("Migrating data to schema %d ...", nextVersion)); 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); versionManager.increment(nextVersion - 1);
} catch (Exception e) { } catch (Exception e) {
throw new OrmException( throw new OrmException(

View File

@@ -22,9 +22,8 @@ import com.google.inject.Singleton;
/** /**
* Schema upgrade implementation. * Schema upgrade implementation.
* *
* <p>Implementations must define a single public constructor that takes an {@link Arguments}. The * <p>Implementations must have a single non-private constructor with no arguments (e.g. the default
* recommended idiom is to pull out whichever individual fields from the {@code Arguments} are * constructor).
* required by this implementation.
*/ */
interface NoteDbSchemaVersion { interface NoteDbSchemaVersion {
@Singleton @Singleton
@@ -39,5 +38,5 @@ interface NoteDbSchemaVersion {
} }
} }
void upgrade(UpdateUI ui) throws Exception; void upgrade(Arguments args, UpdateUI ui) throws Exception;
} }

View File

@@ -45,13 +45,11 @@ public class NoteDbSchemaVersions {
} }
public static NoteDbSchemaVersion get( public static NoteDbSchemaVersion get(
ImmutableSortedMap<Integer, Class<? extends NoteDbSchemaVersion>> schemaVersions, ImmutableSortedMap<Integer, Class<? extends NoteDbSchemaVersion>> schemaVersions, int i) {
int i,
NoteDbSchemaVersion.Arguments args) {
Class<? extends NoteDbSchemaVersion> clazz = schemaVersions.get(i); Class<? extends NoteDbSchemaVersion> clazz = schemaVersions.get(i);
checkArgument(clazz != null, "Schema version not found: %s", i); checkArgument(clazz != null, "Schema version not found: %s", i);
try { try {
return clazz.getDeclaredConstructor(NoteDbSchemaVersion.Arguments.class).newInstance(args); return clazz.getDeclaredConstructor().newInstance();
} catch (InstantiationException } catch (InstantiationException
| IllegalAccessException | IllegalAccessException
| NoSuchMethodException | NoSuchMethodException

View File

@@ -15,13 +15,8 @@
package com.google.gerrit.server.schema; package com.google.gerrit.server.schema;
public class Schema_180 implements NoteDbSchemaVersion { public class Schema_180 implements NoteDbSchemaVersion {
@SuppressWarnings("unused")
Schema_180(Arguments args) {
// Do nothing.
}
@Override @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. // Do nothing; only used to populate the version ref, which is done by the caller.
} }
} }

View File

@@ -191,26 +191,16 @@ public class NoteDbSchemaUpdaterTest extends GerritBaseTests {
} }
} }
private static class TestSchema_10 implements NoteDbSchemaVersion { static class TestSchema_10 implements NoteDbSchemaVersion {
@SuppressWarnings("unused")
TestSchema_10(Arguments args) {
// Do nothing.
}
@Override @Override
public void upgrade(UpdateUI ui) { public void upgrade(Arguments args, UpdateUI ui) {
ui.message("body of 10"); ui.message("body of 10");
} }
} }
private static class TestSchema_11 implements NoteDbSchemaVersion { static class TestSchema_11 implements NoteDbSchemaVersion {
@SuppressWarnings("unused")
TestSchema_11(Arguments args) {
// Do nothing.
}
@Override @Override
public void upgrade(UpdateUI ui) { public void upgrade(Arguments args, UpdateUI ui) {
ui.message("BODY OF 11"); ui.message("BODY OF 11");
} }
} }

View File

@@ -68,9 +68,8 @@ public class NoteDbSchemaVersionsTest extends GerritBaseTests {
@Test @Test
public void schemaConstructors() throws Exception { public void schemaConstructors() throws Exception {
NoteDbSchemaVersion.Arguments args = new NoteDbSchemaVersion.Arguments(null, null);
for (int version : NoteDbSchemaVersions.ALL.keySet()) { for (int version : NoteDbSchemaVersions.ALL.keySet()) {
NoteDbSchemaVersions.get(NoteDbSchemaVersions.ALL, version, args); NoteDbSchemaVersions.get(NoteDbSchemaVersions.ALL, version);
} }
} }
} }