Class MigrationRegistry
Registry for managing configuration migrations indexed by version.
The registry stores migrations keyed by their target version, allowing efficient lookup and sequential application of migrations from any source version to a target version.
Registration
Migrations are registered with their target version as the key:
MigrationRegistry registry = MigrationRegistry.create();
// Register individual migrations
registry.register(new MigrateV1ToV2());
registry.register(new MigrateV2ToV3());
// Or use the fluent API
registry
.register(Migration.of(2, "Add timeout field", ctx -> {
ctx.data().putIfAbsent("timeout", 30);
}))
.register(Migration.of(3, "Rename host to hostname", ctx -> {
ctx.rename("host", "hostname");
}));
Version Lookup
The registry provides methods to find migrations for a given version range:
// Get all migrations needed to go from version 1 to version 5
List<Migration> migrations = registry.getMigrationsInRange(1, 5);
// Check if a migration exists for a specific target version
Optional<Migration> migration = registry.get(3);
Thread Safety
The registry is thread-safe for concurrent registration and lookup operations.
Use create() for a thread-safe implementation, or createNonConcurrent()
for a non-thread-safe but slightly faster implementation.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all registered migrations.booleancontains(int targetVersion) Checks if a migration exists for the specified target version.static MigrationRegistrycreate()Creates a new thread-safe migration registry.static MigrationRegistryCreates a new non-thread-safe migration registry.findMissingMigrations(int fromVersion, int toVersion) Validates that all migrations needed to go from one version to another exist.get(int targetVersion) Gets the migration for a specific target version.Returns all registered migrations in version order.Returns all registered target versions in ascending order.Gets the highest registered target version.Gets the lowest registered target version.getMigrationsInRange(int fromVersion, int toVersion) Gets all migrations needed to migrate from one version to another.booleanhasCompleteMigrationChain(int fromVersion, int toVersion) Checks if the registry has a complete migration chain between two versions.Creates an immutable copy of this registry.booleanisEmpty()Checks if the registry is empty.static MigrationRegistryof(Collection<? extends Migration> migrations) Creates a registry pre-populated with the given migrations.static MigrationRegistryCreates a registry pre-populated with the given migrations.Registers a migration.registerAll(Collection<? extends Migration> migrations) Registers multiple migrations.registerAll(Migration... migrations) Registers multiple migrations.@Nullable MigrationregisterOrReplace(Migration migration) Registers a migration, replacing any existing migration for the same target version.@Nullable Migrationremove(int targetVersion) Removes the migration for the specified target version.intsize()Returns the number of registered migrations.toString()
-
Method Details
-
create
Creates a new thread-safe migration registry.- Returns:
- a new empty registry
-
createNonConcurrent
Creates a new non-thread-safe migration registry.
Use this when migrations are only registered during initialization and the registry won't be modified after that.
- Returns:
- a new empty registry
-
of
Creates a registry pre-populated with the given migrations.- Parameters:
migrations- the migrations to register- Returns:
- a new registry containing the migrations
- Throws:
NullPointerException- if migrations is nullIllegalArgumentException- if multiple migrations have the same target version
-
of
Creates a registry pre-populated with the given migrations.- Parameters:
migrations- the migrations to register- Returns:
- a new registry containing the migrations
- Throws:
NullPointerException- if migrations is nullIllegalArgumentException- if multiple migrations have the same target version
-
register
Registers a migration.
The migration is indexed by its
Migration.targetVersion().- Parameters:
migration- the migration to register- Returns:
- this registry for method chaining
- Throws:
NullPointerException- if migration is nullIllegalArgumentException- if a migration is already registered for the target version
-
registerOrReplace
Registers a migration, replacing any existing migration for the same target version.- Parameters:
migration- the migration to register- Returns:
- the previously registered migration, or null if none
- Throws:
NullPointerException- if migration is null
-
registerAll
Registers multiple migrations.- Parameters:
migrations- the migrations to register- Returns:
- this registry for method chaining
- Throws:
NullPointerException- if migrations is nullIllegalArgumentException- if a migration is already registered for any target version
-
registerAll
Registers multiple migrations.- Parameters:
migrations- the migrations to register- Returns:
- this registry for method chaining
- Throws:
NullPointerException- if migrations is nullIllegalArgumentException- if a migration is already registered for any target version
-
get
-
contains
public boolean contains(int targetVersion) Checks if a migration exists for the specified target version.- Parameters:
targetVersion- the target version to check- Returns:
- true if a migration is registered for that version
-
remove
Removes the migration for the specified target version.- Parameters:
targetVersion- the target version- Returns:
- the removed migration, or null if none was registered
-
clear
public void clear()Clears all registered migrations. -
size
public int size()Returns the number of registered migrations.- Returns:
- the migration count
-
isEmpty
public boolean isEmpty()Checks if the registry is empty.- Returns:
- true if no migrations are registered
-
getAllMigrations
-
getAllVersions
-
getHighestVersion
-
getLowestVersion
-
getMigrationsInRange
Gets all migrations needed to migrate from one version to another.
Returns migrations where
sourceVersion < targetVersion <= toVersion, ensuring migrations are applicable for configs at the given source version.- Parameters:
fromVersion- the current config version (exclusive - migrations after this)toVersion- the target version (inclusive)- Returns:
- a list of migrations in version order
- Throws:
MigrationException- if fromVersion >= toVersion
-
findMissingMigrations
Validates that all migrations needed to go from one version to another exist.
This checks that there are no gaps in the migration chain.
- Parameters:
fromVersion- the starting versiontoVersion- the target version- Returns:
- a list of missing version numbers, empty if chain is complete
-
hasCompleteMigrationChain
public boolean hasCompleteMigrationChain(int fromVersion, int toVersion) Checks if the registry has a complete migration chain between two versions.- Parameters:
fromVersion- the starting versiontoVersion- the target version- Returns:
- true if migrations exist for all versions in the range
-
immutableCopy
Creates an immutable copy of this registry.- Returns:
- a new immutable registry with the same migrations
-
toString
-