1- """Forward-only migration support for versioned resource data."""
1+ """Forward-only migration support for versioned data."""
22
33from __future__ import annotations
44
@@ -20,7 +20,7 @@ class MigrationError(ValueError):
2020
2121
2222class MigrationManager (Generic [MigratableResource ]):
23- """Migrate versioned resource data to its current schema."""
23+ """Migrate stored data to its current schema."""
2424
2525 def __init__ (
2626 self ,
@@ -31,7 +31,7 @@ def __init__(
3131
3232 Args:
3333 model_class: Current Pydantic model for the resource.
34- migrations: Migration functions keyed by their source version.
34+ migrations: Migration functions keyed by their source schema version.
3535
3636 Raises:
3737 TypeError: If the model does not declare one positive integer
@@ -42,30 +42,30 @@ def __init__(
4242 self .current_version = self ._get_current_version ()
4343
4444 def migrate_resource (self , data : Any ) -> MigratableResource :
45- """Migrate decoded JSON data with predefined migration function and validate it.
45+ """Migrate decoded data with registered migration functions and validate it.
4646
4747 Each migration function must increment the schema version by exactly one.
4848
4949 Args:
5050 data: Decoded JSON data to migrate and validate.
5151
5252 Returns:
53- The resource data as a validated current model.
53+ The migrated data as a validated current model.
5454
5555 Raises:
5656 MigrationError: If the data is not a JSON object, a schema version is
57- invalid or newer than supported, a required migration is missing or
58- fails, a migration function does not increment the schema version by
59- one, or the final data does not match the current model.
57+ invalid or newer than supported, a required migration function is
58+ missing or fails, a migration function does not increment the schema
59+ version by one, or the final data does not match the current model.
6060 """
6161 if not isinstance (data , dict ):
6262 raise MigrationError (
6363 f"{ self .model_class .__name__ } resource must be a JSON object"
6464 )
65- source_version = self ._get_source_version (data )
66- migration_steps = self ._get_migration_steps (source_version )
65+ source_schema_version = self ._get_source_schema_version (data )
66+ migration_steps = self ._get_migration_steps (source_schema_version )
6767 migrated_data = copy .deepcopy (data ) if migration_steps else data .copy ()
68- migrated_data .setdefault ("schema_version" , source_version )
68+ migrated_data .setdefault ("schema_version" , source_schema_version )
6969 for version , migration_function in migration_steps :
7070 try :
7171 migrated_data = migration_function (migrated_data )
@@ -95,43 +95,44 @@ def migrate_resource(self, data: Any) -> MigratableResource:
9595 return validated_model
9696
9797 def requires_migration (self , data : dict [str , Any ]) -> bool :
98- """Return whether the data requires a migration before a write.
98+ """Return whether stored data needs migration before a write.
9999
100- This method does not run migrations. It confirms that all required forward
101- migration steps exist, then checks whether the source schema is older than
102- the current schema.
100+ This check verifies that all required forward migration functions exist and
101+ that the stored schema version is older than the current schema version.
103102
104103 Args:
105- data: Existing resource data that a write would replace.
104+ data: Existing stored data that a write would replace.
106105
107106 Returns:
108- Whether the resource data requires migration.
107+ Whether the stored data requires migration.
109108
110109 Raises:
111110 MigrationError: If the schema version is invalid or newer than supported,
112- or a required migration step is missing.
111+ or a required migration function is missing.
113112 """
114- source_version = self ._get_source_version (data )
115- self ._get_migration_steps (source_version )
116- return source_version < self .current_version
113+ source_schema_version = self ._get_source_schema_version (data )
114+ self ._get_migration_steps (source_schema_version )
115+ return source_schema_version < self .current_version
117116
118117 def _get_migration_steps (
119- self , source_version : int
118+ self , source_schema_version : int
120119 ) -> list [tuple [int , MigrationFunction ]]:
121- """Return and validate all migration steps needed by a source version."""
122- if source_version > self .current_version :
120+ """Return and validate migration steps needed by a source schema version."""
121+ if source_schema_version > self .current_version :
123122 raise MigrationError (
124- f"{ self .model_class .__name__ } schema version { source_version } is newer "
125- f"than supported version { self .current_version } ; downgrade migration "
123+ f"{ self .model_class .__name__ } schema version { source_schema_version } "
124+ "is newer than supported version "
125+ f"{ self .current_version } ; downgrade migration "
126126 "is not supported"
127127 )
128128
129129 steps : list [tuple [int , MigrationFunction ]] = []
130- for version in range (source_version , self .current_version ):
130+ for version in range (source_schema_version , self .current_version ):
131131 migration_function = self .migrations .get (version )
132132 if migration_function is None :
133133 raise MigrationError (
134- f"Missing { self .model_class .__name__ } migration from schema "
134+ f"Missing { self .model_class .__name__ } migration function from "
135+ "schema "
135136 f"version { version } to { version + 1 } "
136137 )
137138 steps .append ((version , migration_function ))
@@ -179,8 +180,8 @@ def _get_current_version(self) -> int:
179180 )
180181 return current_version
181182
182- def _get_source_version (self , data : dict [str , Any ]) -> int :
183- """Get the source version, using the legacy version when it is absent."""
183+ def _get_source_schema_version (self , data : dict [str , Any ]) -> int :
184+ """Return the stored schema version, using the legacy version when absent."""
184185 if "schema_version" not in data :
185186 return LEGACY_SCHEMA_VERSION
186187 return self ._validate_version (data .get ("schema_version" ))
0 commit comments