Class MigrationContext
Holds the state and data for a configuration migration.
The migration context provides access to the raw YAML data being migrated, along with metadata about the migration process. It also provides utility methods for common migration operations like getting nested values, renaming fields, and type-safe value extraction.
Data Access
The data() method returns a mutable map that migrations can modify directly:
@Override
public void migrate(MigrationContext context) {
Map<String, Object> data = context.data();
data.put("newField", "value");
data.remove("oldField");
}
Nested Data Access
Use getNestedMap(String) and getNestedValue(String, String, Class) to safely access nested structures:
@Override
public void migrate(MigrationContext context) {
// Safely get nested map, creating it if it doesn't exist
Map<String, Object> database = context.getOrCreateNestedMap("database");
database.put("poolSize", 10);
// Safely get a nested value with type casting
Optional<String> host = context.getNestedValue("database", "host", String.class);
}
Immutability
The context itself is immutable (file path and current version cannot change), but the data map is mutable to allow migrations to transform it.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptioncopyData()Creates a copy of the current data.intReturns the version the data is currently at.data()Returns the mutable map containing the YAML data.filePath()Returns the path to the configuration file being migrated.<T> Optional<T> Gets a value from the data map with type casting.getBoolean(String key) Gets a boolean value from the data map.Gets an integer value from the data map.Gets a list value from the data map.getNestedMap(String key) Gets a nested map from the data.<T> Optional<T> getNestedValue(String mapKey, String valueKey, Class<T> type) Gets a value from a nested map.Gets or creates a nested map in the data.Gets a string value from the data map.booleanmoveFromNested(String sourceMapKey, String sourceKey, String targetKey) Moves a value from a nested map to the root map.booleanmoveToNested(String sourceKey, String targetMapKey, String targetKey) Moves a value from one location to a nested map.static MigrationContextCreates a new migration context.static MigrationContextCreates a migration context without a file path (for in-memory migrations).booleanRenames a key in the data map.booleanrenameNested(String mapKey, String oldKey, String newKey) Renames a key within a nested map.booleansetDefault(String key, @Nullable Object value) Sets a value in the data map if the key is not already present.intReturns the target version to migrate to.toString()
-
Method Details
-
of
public static MigrationContext of(Path filePath, Map<String, Object> data, int currentVersion, int targetVersion) Creates a new migration context.- Parameters:
filePath- the path to the configuration filedata- the raw YAML datacurrentVersion- the current versiontargetVersion- the target version- Returns:
- a new migration context
- Throws:
NullPointerException- if filePath or data is null
-
ofData
public static MigrationContext ofData(Map<String, Object> data, int currentVersion, int targetVersion) Creates a migration context without a file path (for in-memory migrations).- Parameters:
data- the raw YAML datacurrentVersion- the current versiontargetVersion- the target version- Returns:
- a new migration context
- Throws:
NullPointerException- if data is null
-
filePath
Returns the path to the configuration file being migrated.- Returns:
- the file path, or an empty path for in-memory migrations
-
data
-
currentVersion
public int currentVersion()Returns the version the data is currently at.
This is the version before any migrations in the current batch are applied.
- Returns:
- the current version
-
targetVersion
public int targetVersion()Returns the target version to migrate to.- Returns:
- the target version
-
get
-
getString
-
getInt
-
getBoolean
-
getList
-
getNestedMap
-
getOrCreateNestedMap
Gets or creates a nested map in the data.
If the key doesn't exist, a new
LinkedHashMapis created and added.- Parameters:
key- the key for the nested map- Returns:
- the existing or newly created nested map
- Throws:
MigrationException- if the key exists but is not a map
-
getNestedValue
Gets a value from a nested map.- Type Parameters:
T- the value type- Parameters:
mapKey- the key for the nested mapvalueKey- the key within the nested maptype- the expected type- Returns:
- an Optional containing the value if present
-
rename
Renames a key in the data map.
If the old key doesn't exist, this method does nothing. If the new key already exists, it will be overwritten.
- Parameters:
oldKey- the current key namenewKey- the new key name- Returns:
- true if the key was renamed, false if the old key didn't exist
-
renameNested
Renames a key within a nested map.- Parameters:
mapKey- the key for the nested mapoldKey- the current key name within the nested mapnewKey- the new key name within the nested map- Returns:
- true if the key was renamed, false otherwise
-
setDefault
-
moveToNested
Moves a value from one location to a nested map.- Parameters:
sourceKey- the source key in the root maptargetMapKey- the key for the target nested maptargetKey- the key within the target nested map- Returns:
- true if the value was moved, false if source didn't exist
-
moveFromNested
Moves a value from a nested map to the root map.- Parameters:
sourceMapKey- the key for the source nested mapsourceKey- the key within the source nested maptargetKey- the target key in the root map- Returns:
- true if the value was moved, false otherwise
-
copyData
-
toString
-