hyrax.config_migrations.migration_utils#
Migration engine, helpers, and registry for versioned config migrations.
Attributes#
Classes#
A single schema migration with optional key-rename metadata. |
Functions#
|
Decorator that registers a migration function into |
|
Rename a top-level table from |
|
Move a nested key from |
|
|
|
Upgrade a user config document to the current schema version. |
Module Contents#
- MIGRATIONS: dict[int, MigrationStep][source]#
- class MigrationStep[source]#
A single schema migration with optional key-rename metadata.
- Parameters:
func (Callable[[TOMLDocument], TOMLDocument]) – The function that upgrades a config document by one version.
key_renames (dict[str, str]) – Old dotted path -> new dotted path for every key renamed by this migration. Used by
ConfigManager.set_configto warn callers who still reference the old names at runtime.
- migration_step(from_version: int, key_renames: dict[str, str] | None = None)[source]#
Decorator that registers a migration function into
MIGRATIONS.- Parameters:
from_version (int) – The config schema version this function upgrades FROM. The target version is implicitly
from_version + 1.key_renames (dict[str, str], optional) – Old dotted path -> new dotted path for every key renamed by this migration.
- rename_table(cfg: tomlkit.toml_document.TOMLDocument | dict, old: str, new: str) bool[source]#
Rename a top-level table from
oldtonew.If only
oldexists it is moved. If both exist,cfg[new]wins on leaf collisions; the two are deep-merged via ConfigManager.merge_configs andoldis removed. If neither exists this is a no-op.- Parameters:
cfg (TOMLDocument or dict) – The user config document to mutate in place.
old (str) – The table name that is being retired.
new (str) – The new table name to adopt.
- Returns:
Trueiffoldwas present and something was renamed (so the caller can emit a deprecation warning conditionally).- Return type:
bool
- move_key(cfg: tomlkit.toml_document.TOMLDocument | dict, old_path: str, new_path: str) bool[source]#
Move a nested key from
old_pathtonew_path.Paths are dotted strings parsed by
hyrax.config_utils.parse_dotted_key(), so quoted segments like"'torch.optim.Adam'.lr"are supported.- Parameters:
cfg (TOMLDocument or dict) – The user config document to mutate in place.
old_path (str) – The dotted path to the existing key.
new_path (str) – The dotted path to write the value to. Intermediate tables are created as needed.
- Returns:
Trueiffold_pathwas present and the value was moved.- Return type:
bool
- migrate_config(user_config: tomlkit.toml_document.TOMLDocument, *, _migrations: dict[int, MigrationStep] | None = None, _target_version: int | None = None) tomlkit.toml_document.TOMLDocument[source]#
Upgrade a user config document to the current schema version.
The document is mutated in place (and also returned for convenience). If
config_versionis absent it is assumed to be1— older configs predate the versioning field. If it is greater than the current schema version, aRuntimeErroris raised because the installed Hyrax does not know how to read the schema.- Parameters:
user_config (TOMLDocument) – The parsed user config. An empty document (the “no user config” case) is returned unchanged.
_migrations (dict[int, MigrationStep], optional) – Override the global
MIGRATIONSregistry (testing only)._target_version (int, optional) – Override the auto-derived target version (testing only).
- Returns:
The upgraded document with
config_versionstamped to the current value.- Return type:
TOMLDocument
- Raises:
RuntimeError – If
user_configdeclares a version newer than this Hyrax can understand, or if a migration step is missing fromMIGRATIONS.