Class MigrationExecutor
java.lang.Object
net.blockhost.commons.config.migration.MigrationExecutor
Executes configuration migrations sequentially by version.
The executor is responsible for applying migrations from a registry in the correct order, tracking progress, handling errors, and optionally creating backups or performing rollbacks.
Basic Usage
MigrationRegistry registry = MigrationRegistry.create()
.register(new MigrateV1ToV2())
.register(new MigrateV2ToV3());
MigrationExecutor executor = MigrationExecutor.create(registry);
Map<String, Object> data = RawYamlLoader.load(configPath);
int currentVersion = RawYamlLoader.extractVersion(data);
MigrationResult result = executor.execute(configPath, data, currentVersion, 3);
if (result.isSuccess()) {
RawYamlLoader.save(configPath, result.data());
}
Execution Modes
The executor supports different execution modes:
- Strict mode (default): Fails if any migration in the chain is missing
- Lenient mode: Skips missing migrations and applies available ones
- Dry run: Simulates migrations without modifying the data
Callbacks
Register callbacks to monitor migration progress:
executor.beforeMigration(migration -> {
logger.info("Applying migration: " + migration.description());
});
executor.afterMigration((migration, duration) -> {
logger.info("Completed in " + duration.toMillis() + "ms");
});
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceCallback interface for post-migration notifications. -
Method Summary
Modifier and TypeMethodDescriptionafterMigration(@Nullable MigrationExecutor.MigrationCallback callback) Sets a callback to be invoked after each successful migration.beforeMigration(@Nullable Consumer<Migration> callback) Sets a callback to be invoked before each migration.booleancanMigrate(int fromVersion, int toVersion) Checks if migrations can be executed for the given version range.static MigrationExecutorcreate(MigrationRegistry registry) Creates a new migration executor with the specified registry.dryRun(boolean dryRun) Enables or disables dry run mode.Executes migrations on the provided data.Executes migrations on in-memory data without a file path.onError(@Nullable Consumer<MigrationException> callback) Sets a callback to be invoked when a migration fails.registry()Returns the registry used by this executor.strictMode(boolean strict) Enables or disables strict mode.
-
Method Details
-
create
Creates a new migration executor with the specified registry.- Parameters:
registry- the migration registry to use- Returns:
- a new executor
- Throws:
NullPointerException- if registry is null
-
strictMode
Enables or disables strict mode.
In strict mode (default), the executor fails if any migration in the chain is missing. In lenient mode, missing migrations are skipped.
- Parameters:
strict- true for strict mode, false for lenient mode- Returns:
- this executor for method chaining
-
dryRun
Enables or disables dry run mode.
In dry run mode, migrations are executed but results are discarded. The original data is preserved. Useful for testing migrations.
- Parameters:
dryRun- true to enable dry run mode- Returns:
- this executor for method chaining
-
beforeMigration
Sets a callback to be invoked before each migration.- Parameters:
callback- the callback to invoke- Returns:
- this executor for method chaining
-
afterMigration
Sets a callback to be invoked after each successful migration.- Parameters:
callback- the callback to invoke- Returns:
- this executor for method chaining
-
onError
Sets a callback to be invoked when a migration fails.- Parameters:
callback- the callback to invoke- Returns:
- this executor for method chaining
-
execute
public MigrationResult execute(Path filePath, Map<String, Object> data, int fromVersion, int toVersion) Executes migrations on the provided data.- Parameters:
filePath- the path to the config file (for context/logging)data- the raw YAML data to migratefromVersion- the current version of the datatoVersion- the target version to migrate to- Returns:
- the migration result
- Throws:
NullPointerException- if filePath or data is null
-
execute
Executes migrations on in-memory data without a file path.- Parameters:
data- the raw YAML data to migratefromVersion- the current version of the datatoVersion- the target version to migrate to- Returns:
- the migration result
- Throws:
NullPointerException- if data is null
-
canMigrate
public boolean canMigrate(int fromVersion, int toVersion) Checks if migrations can be executed for the given version range.- Parameters:
fromVersion- the current versiontoVersion- the target version- Returns:
- true if all required migrations are available
-
registry
Returns the registry used by this executor.- Returns:
- the migration registry
-