pyrmute 0.1.0__py3-none-any.whl → 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyrmute/__init__.py +35 -8
- pyrmute/_migration_manager.py +112 -37
- pyrmute/_registry.py +24 -11
- pyrmute/_schema_manager.py +31 -34
- pyrmute/_version.py +2 -2
- pyrmute/exceptions.py +55 -0
- pyrmute/migration_testing.py +161 -0
- pyrmute/model_diff.py +272 -0
- pyrmute/model_manager.py +529 -68
- pyrmute/model_version.py +16 -8
- pyrmute/types.py +17 -5
- pyrmute-0.3.0.dist-info/METADATA +352 -0
- pyrmute-0.3.0.dist-info/RECORD +17 -0
- pyrmute-0.1.0.dist-info/METADATA +0 -130
- pyrmute-0.1.0.dist-info/RECORD +0 -14
- {pyrmute-0.1.0.dist-info → pyrmute-0.3.0.dist-info}/WHEEL +0 -0
- {pyrmute-0.1.0.dist-info → pyrmute-0.3.0.dist-info}/licenses/LICENSE +0 -0
- {pyrmute-0.1.0.dist-info → pyrmute-0.3.0.dist-info}/top_level.txt +0 -0
pyrmute/model_version.py
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
from dataclasses import dataclass
|
4
4
|
from typing import Self
|
5
5
|
|
6
|
+
from .exceptions import InvalidVersionError
|
7
|
+
|
6
8
|
|
7
9
|
@dataclass(frozen=True, order=True)
|
8
10
|
class ModelVersion:
|
@@ -29,19 +31,25 @@ class ModelVersion:
|
|
29
31
|
Parsed Version instance.
|
30
32
|
|
31
33
|
Raises:
|
32
|
-
|
34
|
+
InvalidVersionError: If version string format is invalid.
|
33
35
|
"""
|
34
|
-
parts = version_str.split(".")
|
35
|
-
if len(parts) != 3: # noqa: PLR2004
|
36
|
-
raise ValueError(f"Invalid version format: {version_str}")
|
37
|
-
|
38
36
|
try:
|
39
|
-
|
37
|
+
parts = version_str.split(".")
|
38
|
+
if len(parts) != 3: # noqa: PLR2004
|
39
|
+
raise InvalidVersionError(
|
40
|
+
version_str, "Version must have exactly 3 parts (major.minor.patch)"
|
41
|
+
)
|
42
|
+
|
43
|
+
major, minor, patch = map(int, parts)
|
40
44
|
if major < 0 or minor < 0 or patch < 0:
|
41
|
-
raise
|
45
|
+
raise InvalidVersionError(
|
46
|
+
version_str, "Version parts must be positive integers"
|
47
|
+
)
|
42
48
|
return cls(major, minor, patch)
|
43
49
|
except ValueError as e:
|
44
|
-
raise
|
50
|
+
raise InvalidVersionError(
|
51
|
+
version_str, f"Version parts must be integers: {e}"
|
52
|
+
) from e
|
45
53
|
|
46
54
|
def __str__(self: Self) -> str:
|
47
55
|
"""Return string representation of version.
|
pyrmute/types.py
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
"""Type aliases needed in the package."""
|
2
2
|
|
3
|
+
from __future__ import annotations
|
4
|
+
|
3
5
|
from collections.abc import Callable
|
6
|
+
from dataclasses import dataclass
|
4
7
|
from typing import Any, TypeAlias, TypeVar
|
5
8
|
|
6
9
|
from pydantic import BaseModel
|
@@ -9,18 +12,27 @@ from .model_version import ModelVersion
|
|
9
12
|
|
10
13
|
DecoratedBaseModel = TypeVar("DecoratedBaseModel", bound=BaseModel)
|
11
14
|
|
12
|
-
JsonValue: TypeAlias =
|
15
|
+
JsonValue: TypeAlias = (
|
16
|
+
int | float | str | bool | None | list["JsonValue"] | dict[str, "JsonValue"]
|
17
|
+
)
|
13
18
|
JsonSchema: TypeAlias = dict[str, JsonValue]
|
14
|
-
JsonSchemaDefinitions: TypeAlias = dict[str,
|
19
|
+
JsonSchemaDefinitions: TypeAlias = dict[str, JsonValue]
|
15
20
|
JsonSchemaGenerator: TypeAlias = Callable[[type[BaseModel]], JsonSchema]
|
16
21
|
SchemaGenerators: TypeAlias = dict[ModelVersion, JsonSchemaGenerator]
|
17
22
|
|
18
|
-
|
19
|
-
MigrationFunc: TypeAlias = Callable[[
|
20
|
-
|
23
|
+
ModelData: TypeAlias = dict[str, Any]
|
24
|
+
MigrationFunc: TypeAlias = Callable[[ModelData], ModelData]
|
21
25
|
MigrationKey: TypeAlias = tuple[ModelVersion, ModelVersion]
|
22
26
|
MigrationMap: TypeAlias = dict[MigrationKey, MigrationFunc]
|
23
27
|
|
24
28
|
ModelName: TypeAlias = str
|
25
29
|
ModelMetadata: TypeAlias = tuple[ModelName, ModelVersion]
|
26
30
|
VersionedModels: TypeAlias = dict[ModelVersion, type[BaseModel]]
|
31
|
+
|
32
|
+
|
33
|
+
@dataclass
|
34
|
+
class NestedModelInfo:
|
35
|
+
"""Contains information about nested models."""
|
36
|
+
|
37
|
+
name: str
|
38
|
+
version: ModelVersion
|
@@ -0,0 +1,352 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: pyrmute
|
3
|
+
Version: 0.3.0
|
4
|
+
Summary: Pydantic model migrations and schemas
|
5
|
+
Author-email: Matt Ferrera <mattferrera@gmail.com>
|
6
|
+
License: MIT
|
7
|
+
Project-URL: Homepage, https://github.com/mferrera/pyrmute
|
8
|
+
Project-URL: Repository, https://github.com/mferrera/pyrmute
|
9
|
+
Project-URL: Documentation, https://github.com/mferrera/pyrmute
|
10
|
+
Keywords: pydantic,migration,versioning,schema
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
12
|
+
Classifier: Topic :: Utilities
|
13
|
+
Classifier: Operating System :: POSIX :: Linux
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
18
|
+
Classifier: Natural Language :: English
|
19
|
+
Requires-Python: >=3.11
|
20
|
+
Description-Content-Type: text/markdown
|
21
|
+
License-File: LICENSE
|
22
|
+
Requires-Dist: pydantic
|
23
|
+
Provides-Extra: dev
|
24
|
+
Requires-Dist: hypothesis; extra == "dev"
|
25
|
+
Requires-Dist: mypy; extra == "dev"
|
26
|
+
Requires-Dist: pytest; extra == "dev"
|
27
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
28
|
+
Requires-Dist: pytest-mock; extra == "dev"
|
29
|
+
Requires-Dist: pytest-xdist; extra == "dev"
|
30
|
+
Requires-Dist: ruff; extra == "dev"
|
31
|
+
Dynamic: license-file
|
32
|
+
|
33
|
+
# pyrmute
|
34
|
+
[](https://github.com/mferrera/pyrmute/actions?query=event%3Apush+branch%3Amain+workflow%3Aci)
|
35
|
+
[](https://pypi.python.org/pypi/pyrmute)
|
36
|
+
[](https://github.com/mferrera/pyrmute)
|
37
|
+
[](https://github.com/mferrera/pyrmute/blob/main/LICENSE)
|
38
|
+
|
39
|
+
Pydantic model migrations and schema management with semantic versioning.
|
40
|
+
|
41
|
+
pyrmute handles the complexity of data model evolution so you can confidently
|
42
|
+
make changes without breaking your production systems. Version your models,
|
43
|
+
define transformations, and let pyrmute automatically migrate legacy data
|
44
|
+
through multiple versions.
|
45
|
+
|
46
|
+
**Key Features**
|
47
|
+
|
48
|
+
- **Version your models** - Track schema evolution with semantic versioning
|
49
|
+
- **Automatic migration chains** - Transform data across multiple versions
|
50
|
+
(1.0.0 → 2.0.0 → 3.0.0) in a single call
|
51
|
+
- **Type-safe transformations** - Migrations return validated Pydantic models,
|
52
|
+
catching errors before they reach production
|
53
|
+
- **Flexible schema export** - Generate JSON schemas for all versions with
|
54
|
+
support for `$ref`, custom generators, and nested models
|
55
|
+
- **Production-ready** - Batch processing, parallel execution, and streaming
|
56
|
+
support for large datasets
|
57
|
+
- **Only one dependency** - Pydantic
|
58
|
+
|
59
|
+
## Help
|
60
|
+
|
61
|
+
See [documentation](https://mferrera.github.io/pyrmute/) for complete guides
|
62
|
+
and API reference.
|
63
|
+
|
64
|
+
## Installation
|
65
|
+
|
66
|
+
```bash
|
67
|
+
pip install pyrmute
|
68
|
+
```
|
69
|
+
|
70
|
+
## Quick Start
|
71
|
+
|
72
|
+
```python
|
73
|
+
from pydantic import BaseModel
|
74
|
+
from pyrmute import ModelManager, ModelData
|
75
|
+
|
76
|
+
manager = ModelManager()
|
77
|
+
|
78
|
+
|
79
|
+
# Version 1: Simple user model
|
80
|
+
@manager.model("User", "1.0.0")
|
81
|
+
class UserV1(BaseModel):
|
82
|
+
name: str
|
83
|
+
age: int
|
84
|
+
|
85
|
+
|
86
|
+
# Version 2: Split name into components
|
87
|
+
@manager.model("User", "2.0.0")
|
88
|
+
class UserV2(BaseModel):
|
89
|
+
first_name: str
|
90
|
+
last_name: str
|
91
|
+
age: int
|
92
|
+
|
93
|
+
|
94
|
+
# Version 3: Add email and make age optional
|
95
|
+
@manager.model("User", "3.0.0")
|
96
|
+
class UserV3(BaseModel):
|
97
|
+
first_name: str
|
98
|
+
last_name: str
|
99
|
+
email: str
|
100
|
+
age: int | None = None
|
101
|
+
|
102
|
+
|
103
|
+
# Define how to migrate between versions
|
104
|
+
@manager.migration("User", "1.0.0", "2.0.0")
|
105
|
+
def split_name(data: ModelData) -> ModelData:
|
106
|
+
parts = data["name"].split(" ", 1)
|
107
|
+
return {
|
108
|
+
"first_name": parts[0],
|
109
|
+
"last_name": parts[1] if len(parts) > 1 else "",
|
110
|
+
"age": data["age"],
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
@manager.migration("User", "2.0.0", "3.0.0")
|
115
|
+
def add_email(data: ModelData) -> ModelData:
|
116
|
+
return {
|
117
|
+
**data,
|
118
|
+
"email": f"{data['first_name'].lower()}@example.com"
|
119
|
+
}
|
120
|
+
|
121
|
+
|
122
|
+
# Migrate legacy data to the latest version
|
123
|
+
legacy_data = {"name": "John Doe", "age": 30} # or, legacy.model_dump()
|
124
|
+
current_user = manager.migrate(legacy_data, "User", "1.0.0", "3.0.0")
|
125
|
+
|
126
|
+
print(current_user)
|
127
|
+
# UserV3(first_name='John', last_name='Doe', email='john@example.com', age=30)
|
128
|
+
```
|
129
|
+
|
130
|
+
## Advanced Usage
|
131
|
+
|
132
|
+
### Compare Model Versions
|
133
|
+
|
134
|
+
```python
|
135
|
+
# See exactly what changed between versions
|
136
|
+
diff = manager.diff("User", "1.0.0", "3.0.0")
|
137
|
+
print(f"Added: {diff.added_fields}")
|
138
|
+
print(f"Removed: {diff.removed_fields}")
|
139
|
+
# Render a changelog to Markdown
|
140
|
+
print(diff.to_markdown(header_depth=4))
|
141
|
+
```
|
142
|
+
|
143
|
+
With `header_depth=4` the output can be embedded nicely into this document.
|
144
|
+
|
145
|
+
#### User: 1.0.0 → 3.0.0
|
146
|
+
|
147
|
+
##### Added Fields
|
148
|
+
|
149
|
+
- `email: str` (required)
|
150
|
+
- `first_name: str` (required)
|
151
|
+
- `last_name: str` (required)
|
152
|
+
|
153
|
+
##### Removed Fields
|
154
|
+
|
155
|
+
- `name`
|
156
|
+
|
157
|
+
##### Modified Fields
|
158
|
+
|
159
|
+
- `age` - type: `int` → `int | None` - now optional - default added: `None`
|
160
|
+
|
161
|
+
##### Breaking Changes
|
162
|
+
|
163
|
+
- ⚠️ New required field 'last_name' will fail for existing data without defaults
|
164
|
+
- ⚠️ New required field 'first_name' will fail for existing data without defaults
|
165
|
+
- ⚠️ New required field 'email' will fail for existing data without defaults
|
166
|
+
- ⚠️ Removed fields 'name' will be lost during migration
|
167
|
+
- ⚠️ Field 'age' type changed - may cause validation errors
|
168
|
+
|
169
|
+
|
170
|
+
### Batch Processing
|
171
|
+
|
172
|
+
```python
|
173
|
+
# Migrate thousands of records efficiently
|
174
|
+
legacy_users = [
|
175
|
+
{"name": "Alice Smith", "age": 28},
|
176
|
+
{"name": "Bob Johnson", "age": 35},
|
177
|
+
# ... thousands more
|
178
|
+
]
|
179
|
+
|
180
|
+
# Parallel processing for CPU-intensive migrations
|
181
|
+
users = manager.migrate_batch(
|
182
|
+
legacy_users,
|
183
|
+
"User",
|
184
|
+
from_version="1.0.0",
|
185
|
+
to_version="3.0.0",
|
186
|
+
parallel=True,
|
187
|
+
max_workers=4,
|
188
|
+
)
|
189
|
+
```
|
190
|
+
|
191
|
+
### Streaming Large Datasets
|
192
|
+
|
193
|
+
```python
|
194
|
+
# Process huge datasets without loading everything into memory
|
195
|
+
def load_users_from_database() -> Iterator[dict[str, Any]]:
|
196
|
+
yield from database.stream_users()
|
197
|
+
|
198
|
+
|
199
|
+
# Migrate and save incrementally
|
200
|
+
for user in manager.migrate_batch_streaming(
|
201
|
+
load_users_from_database(),
|
202
|
+
"User",
|
203
|
+
from_version="1.0.0",
|
204
|
+
to_version="3.0.0",
|
205
|
+
chunk_size=1000
|
206
|
+
):
|
207
|
+
database.save(user)
|
208
|
+
```
|
209
|
+
|
210
|
+
### Test Your Migrations
|
211
|
+
|
212
|
+
```python
|
213
|
+
# Validate migration logic with test cases
|
214
|
+
results = manager.test_migration(
|
215
|
+
"User",
|
216
|
+
from_version="1.0.0",
|
217
|
+
to_version="2.0.0",
|
218
|
+
test_cases=[
|
219
|
+
# (input, expected_output)
|
220
|
+
(
|
221
|
+
{"name": "Alice Smith", "age": 28},
|
222
|
+
{"first_name": "Alice", "last_name": "Smith", "age": 28}
|
223
|
+
),
|
224
|
+
(
|
225
|
+
{"name": "Bob", "age": 35},
|
226
|
+
{"first_name": "Bob", "last_name": "", "age": 35}
|
227
|
+
),
|
228
|
+
]
|
229
|
+
)
|
230
|
+
|
231
|
+
# Use in your test suite
|
232
|
+
assert results.all_passed, f"Migration failed: {results.failures}"
|
233
|
+
```
|
234
|
+
|
235
|
+
### Export JSON Schemas
|
236
|
+
|
237
|
+
```python
|
238
|
+
# Generate schemas for all versions
|
239
|
+
manager.dump_schemas("schemas/")
|
240
|
+
# Creates: User_v1.0.0.json, User_v2.0.0.json, User_v3.0.0.json
|
241
|
+
|
242
|
+
# Use separate files with $ref for nested models with 'enable_ref=True'.
|
243
|
+
manager.dump_schemas(
|
244
|
+
"schemas/",
|
245
|
+
separate_definitions=True,
|
246
|
+
ref_template="https://api.example.com/schemas/{model}_v{version}.json"
|
247
|
+
)
|
248
|
+
```
|
249
|
+
|
250
|
+
### Auto-Migration
|
251
|
+
|
252
|
+
```python
|
253
|
+
# Skip writing migration functions for simple changes
|
254
|
+
@manager.model("Config", "1.0.0")
|
255
|
+
class ConfigV1(BaseModel):
|
256
|
+
timeout: int = 30
|
257
|
+
|
258
|
+
|
259
|
+
@manager.model("Config", "2.0.0", backward_compatible=True)
|
260
|
+
class ConfigV2(BaseModel):
|
261
|
+
timeout: int = 30
|
262
|
+
retries: int = 3 # New field with default
|
263
|
+
|
264
|
+
|
265
|
+
# No migration function needed - defaults are applied automatically
|
266
|
+
config = manager.migrate({"timeout": 60}, "Config", "1.0.0", "2.0.0")
|
267
|
+
# ConfigV2(timeout=60, retries=3)
|
268
|
+
```
|
269
|
+
|
270
|
+
## Real-World Example
|
271
|
+
|
272
|
+
```python
|
273
|
+
from datetime import datetime
|
274
|
+
from pydantic import BaseModel, EmailStr
|
275
|
+
from pyrmute import ModelManager, ModelData
|
276
|
+
|
277
|
+
manager = ModelManager()
|
278
|
+
|
279
|
+
|
280
|
+
# API v1: Basic order
|
281
|
+
@manager.model("Order", "1.0.0")
|
282
|
+
class OrderV1(BaseModel):
|
283
|
+
id: str
|
284
|
+
items: list[str]
|
285
|
+
total: float
|
286
|
+
|
287
|
+
|
288
|
+
# API v2: Add customer info
|
289
|
+
@manager.model("Order", "2.0.0")
|
290
|
+
class OrderV2(BaseModel):
|
291
|
+
id: str
|
292
|
+
items: list[str]
|
293
|
+
total: float
|
294
|
+
customer_email: EmailStr
|
295
|
+
|
296
|
+
|
297
|
+
# API v3: Structured items and timestamps
|
298
|
+
@manager.model("Order", "3.0.0")
|
299
|
+
class OrderItemV3(BaseModel):
|
300
|
+
product_id: str
|
301
|
+
quantity: int
|
302
|
+
price: float
|
303
|
+
|
304
|
+
|
305
|
+
@manager.model("Order", "3.0.0")
|
306
|
+
class OrderV3(BaseModel):
|
307
|
+
id: str
|
308
|
+
items: list[OrderItemV3]
|
309
|
+
total: float
|
310
|
+
customer_email: EmailStr
|
311
|
+
created_at: datetime
|
312
|
+
|
313
|
+
|
314
|
+
# Define migrations
|
315
|
+
@manager.migration("Order", "1.0.0", "2.0.0")
|
316
|
+
def add_customer_email(data: ModelData) -> ModelData:
|
317
|
+
return {**data, "customer_email": "customer@example.com"}
|
318
|
+
|
319
|
+
|
320
|
+
@manager.migration("Order", "2.0.0", "3.0.0")
|
321
|
+
def structure_items(data: ModelData) -> ModelData:
|
322
|
+
# Convert simple strings to structured items
|
323
|
+
structured_items = [
|
324
|
+
{
|
325
|
+
"product_id": item,
|
326
|
+
"quantity": 1,
|
327
|
+
"price": data["total"] / len(data["items"])
|
328
|
+
}
|
329
|
+
for item in data["items"]
|
330
|
+
]
|
331
|
+
return {
|
332
|
+
**data,
|
333
|
+
"items": structured_items,
|
334
|
+
"created_at": datetime.now().isoformat()
|
335
|
+
}
|
336
|
+
|
337
|
+
# Migrate old orders from your database
|
338
|
+
old_order = {"id": "123", "items": ["widget", "gadget"], "total": 29.99}
|
339
|
+
new_order = manager.migrate(old_order, "Order", "1.0.0", "3.0.0")
|
340
|
+
database.save(new_order)
|
341
|
+
```
|
342
|
+
|
343
|
+
## Contributing
|
344
|
+
|
345
|
+
For guidance on setting up a development environment and how to make a
|
346
|
+
contribution to pyrmute, see [Contributing to
|
347
|
+
pyrmute](https://mferrera.github.io/pyrmute/contributing/).
|
348
|
+
|
349
|
+
## Reporting a Security Vulnerability
|
350
|
+
|
351
|
+
See our [security
|
352
|
+
policy](https://github.com/mferrera/pyrmute/security/policy).
|
@@ -0,0 +1,17 @@
|
|
1
|
+
pyrmute/__init__.py,sha256=vB0WBe3CukMBDnK0XP4qqehbMM4z_TUQMcTVPCUyt6Q,1082
|
2
|
+
pyrmute/_migration_manager.py,sha256=KregnRUKqF1TC9XIpGAHpQvFlnRTEnp2X6Q2sAay8D4,12489
|
3
|
+
pyrmute/_registry.py,sha256=iUjMPd6CYgyvWT8PxZqHWBZnsHrX25fOPDi_-k_QDJs,6124
|
4
|
+
pyrmute/_schema_manager.py,sha256=eun8PTL9Gv1XAMVKmE3tYmjdrcf701-IapUXjb6WDL0,12122
|
5
|
+
pyrmute/_version.py,sha256=5zTqm8rgXsWYBpB2M3Zw_K1D-aV8wP7NsBLrmMKkrAQ,704
|
6
|
+
pyrmute/exceptions.py,sha256=Q57cUuzzMdkIl5Q0_VyLobpdB0WcrE0ggfC-LBoX2Uo,1681
|
7
|
+
pyrmute/migration_testing.py,sha256=dOR8BDzmz4mFAI4hFtDUCEMS8Qc8qqD_iOV0qRai-qM,4967
|
8
|
+
pyrmute/model_diff.py,sha256=vMa2NTYFqt9E7UYDZH4PQmLcoxQw5Sj-nPpUHB_53Ig,9594
|
9
|
+
pyrmute/model_manager.py,sha256=e3UKFo79pkseCUFXIzW2_onu3GYjAnY1FR4JR_QF-Gc,24596
|
10
|
+
pyrmute/model_version.py,sha256=ftNDuJlN3S5ZKQK8DKqqwfBDRiz4rGCYn-aJ3n6Zmqk,2025
|
11
|
+
pyrmute/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
12
|
+
pyrmute/types.py,sha256=56IH8Rl9AmVh_w3V6PbSSEwaPrBSfc4pYrtcxodvlT0,1187
|
13
|
+
pyrmute-0.3.0.dist-info/licenses/LICENSE,sha256=otWInySiZeGwhHqQQ7n7nxM5QBSBe2CzeGEmQDZEz8Q,1119
|
14
|
+
pyrmute-0.3.0.dist-info/METADATA,sha256=jnRgO76ovFaktYKqq5BMf4tpUc_DYO_drET7c1hPVk0,9580
|
15
|
+
pyrmute-0.3.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
16
|
+
pyrmute-0.3.0.dist-info/top_level.txt,sha256=C8QtzqE6yBHkeewSp1QewvsyeHj_VQLYjSa5HLtMiow,8
|
17
|
+
pyrmute-0.3.0.dist-info/RECORD,,
|
pyrmute-0.1.0.dist-info/METADATA
DELETED
@@ -1,130 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.4
|
2
|
-
Name: pyrmute
|
3
|
-
Version: 0.1.0
|
4
|
-
Summary: Pydantic model migrations and schemas
|
5
|
-
Author-email: Matt Ferrera <mattferrera@gmail.com>
|
6
|
-
License: MIT
|
7
|
-
Project-URL: Homepage, https://github.com/mferrera/pyrmute
|
8
|
-
Project-URL: Repository, https://github.com/mferrera/pyrmute
|
9
|
-
Project-URL: Documentation, https://github.com/mferrera/pyrmute
|
10
|
-
Keywords: pydantic,migration,versioning,schema
|
11
|
-
Classifier: Development Status :: 3 - Alpha
|
12
|
-
Classifier: Topic :: Utilities
|
13
|
-
Classifier: Operating System :: POSIX :: Linux
|
14
|
-
Classifier: Programming Language :: Python :: 3.11
|
15
|
-
Classifier: Programming Language :: Python :: 3.12
|
16
|
-
Classifier: Programming Language :: Python :: 3.13
|
17
|
-
Classifier: Natural Language :: English
|
18
|
-
Requires-Python: >=3.11
|
19
|
-
Description-Content-Type: text/markdown
|
20
|
-
License-File: LICENSE
|
21
|
-
Requires-Dist: pydantic
|
22
|
-
Provides-Extra: dev
|
23
|
-
Requires-Dist: mypy; extra == "dev"
|
24
|
-
Requires-Dist: pytest; extra == "dev"
|
25
|
-
Requires-Dist: pytest-cov; extra == "dev"
|
26
|
-
Requires-Dist: pytest-mock; extra == "dev"
|
27
|
-
Requires-Dist: pytest-xdist; extra == "dev"
|
28
|
-
Requires-Dist: ruff; extra == "dev"
|
29
|
-
Dynamic: license-file
|
30
|
-
|
31
|
-
# pyrmute
|
32
|
-
|
33
|
-
[](https://github.com/mferrera/pyrmute/actions?query=event%3Apush+branch%3Amain+workflow%3Aci)
|
34
|
-
[](https://pypi.python.org/pypi/pyrmute)
|
35
|
-
[](https://github.com/mferrera/pyrmute)
|
36
|
-
[](https://github.com/mferrera/pyrmute/blob/main/LICENSE)
|
37
|
-
|
38
|
-
Pydantic model migrations and schema management with semantic versioning.
|
39
|
-
|
40
|
-
Pyrmute helps you evolve your data models over time without breaking changes.
|
41
|
-
Version your Pydantic models, define migrations between versions, and
|
42
|
-
automatically transform legacy data to current schemas. Export JSON schemas
|
43
|
-
for all versions to maintain API contracts.
|
44
|
-
|
45
|
-
**Key features:**
|
46
|
-
- **Version your models** - Use semantic versioning to track model evolution
|
47
|
-
- **Automatic migrations** - Chain migrations across multiple versions (1.0.0 → 2.0.0 → 3.0.0)
|
48
|
-
- **Validated transformations** - Migrations return validated Pydantic models by default
|
49
|
-
- **Schema export** - Generate JSON schemas for all versions, with support for `$ref` to external schemas and custom schema generators
|
50
|
-
- **Nested model support** - Automatically migrates nested Pydantic models
|
51
|
-
|
52
|
-
## Help
|
53
|
-
|
54
|
-
See [documentation](https://mferrera.github.io/pyrmute/) for more details.
|
55
|
-
|
56
|
-
## Installation
|
57
|
-
|
58
|
-
Install using `pip install -U pyrmute`.
|
59
|
-
|
60
|
-
## Simple Example
|
61
|
-
|
62
|
-
```python
|
63
|
-
from pydantic import BaseModel
|
64
|
-
from pyrmute import ModelManager, MigrationData
|
65
|
-
|
66
|
-
manager = ModelManager()
|
67
|
-
|
68
|
-
|
69
|
-
@manager.model("User", "1.0.0")
|
70
|
-
class UserV1(BaseModel):
|
71
|
-
"""Version 1.0.0: Initial user model."""
|
72
|
-
name: str
|
73
|
-
age: int
|
74
|
-
|
75
|
-
|
76
|
-
@manager.model("User", "2.0.0")
|
77
|
-
class UserV2(BaseModel):
|
78
|
-
"""Version 2.0.0: Split name into first/last."""
|
79
|
-
first_name: str
|
80
|
-
last_name: str
|
81
|
-
age: int
|
82
|
-
|
83
|
-
|
84
|
-
@manager.model("User", "3.0.0")
|
85
|
-
class UserV3(BaseModel):
|
86
|
-
"""Version 3.0.0: Add email, make age optional."""
|
87
|
-
first_name: str
|
88
|
-
last_name: str
|
89
|
-
email: str
|
90
|
-
age: int | None = None
|
91
|
-
|
92
|
-
|
93
|
-
# Define migrations
|
94
|
-
@manager.migration("User", "1.0.0", "2.0.0")
|
95
|
-
def split_name(data: MigrationData) -> MigrationData:
|
96
|
-
parts = data["name"].split(" ", 1)
|
97
|
-
return {
|
98
|
-
"first_name": parts[0],
|
99
|
-
"last_name": parts[1] if len(parts) > 1 else "",
|
100
|
-
"age": data["age"],
|
101
|
-
}
|
102
|
-
|
103
|
-
|
104
|
-
@manager.migration("User", "2.0.0", "3.0.0")
|
105
|
-
def add_email(data: MigrationData) -> MigrationData:
|
106
|
-
return {**data, "email": f"{data['first_name'].lower()}@example.com"}
|
107
|
-
|
108
|
-
|
109
|
-
# Migrate old data forward, from raw data or dumped from the Pydantic model
|
110
|
-
legacy_data = {"name": "John Doe", "age": 30}
|
111
|
-
# Returns a validated Pydantic model
|
112
|
-
current_user = manager.migrate(legacy_data, "User", "1.0.0", "3.0.0")
|
113
|
-
|
114
|
-
print(current_user)
|
115
|
-
# first_name='John' last_name='Doe' email='john@example.com' age=30
|
116
|
-
|
117
|
-
# Export schemas for all versions
|
118
|
-
manager.dump_schemas("schemas/")
|
119
|
-
# Creates: schemas/User_v1.0.0.json, schemas/User_v2.0.0.json, schemas/User_v3.0.0.json
|
120
|
-
```
|
121
|
-
|
122
|
-
## Contributing
|
123
|
-
|
124
|
-
For guidance on setting up a development environment and how to make a
|
125
|
-
contribution to pyrmute, see
|
126
|
-
[Contributing to pyrmute](https://mferrera.github.io/pyrmute/contributing/).
|
127
|
-
|
128
|
-
## Reporting a Security Vulnerability
|
129
|
-
|
130
|
-
See our [security policy](https://github.com/mferrera/pyrmute/security/policy).
|
pyrmute-0.1.0.dist-info/RECORD
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
pyrmute/__init__.py,sha256=wGuYwo2F3zPXnB_HLJsmjt7jbZVfCcyVwf0J78KvwzA,516
|
2
|
-
pyrmute/_migration_manager.py,sha256=Z9Ie-fxJmOsmBw7oGWGHDrcNvr0aANlO6AqnBmJyYSA,9521
|
3
|
-
pyrmute/_registry.py,sha256=_fZGRQtdkfCsrCrF6GLUuMmHmyq8up9hF6aS9FboQZA,5572
|
4
|
-
pyrmute/_schema_manager.py,sha256=UdDC9j5nI-ljy9zKnfjhukzM4zfF-Edwu6P7CsdI7Xw,12581
|
5
|
-
pyrmute/_version.py,sha256=5jwwVncvCiTnhOedfkzzxmxsggwmTBORdFL_4wq0ZeY,704
|
6
|
-
pyrmute/model_manager.py,sha256=6rh0hXzZAaGv245RpVaAgjIRJ9s5DLmaRbsJAgwGQuQ,8289
|
7
|
-
pyrmute/model_version.py,sha256=_vyLcIfOzHXrumPqzDSuNZMAZGqFh1ZYv6g933O_aH0,1801
|
8
|
-
pyrmute/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
9
|
-
pyrmute/types.py,sha256=NmlBA0ohFjpAIyZ6EvhbEigBV645LCZ-k8vaHjtB7Z4,978
|
10
|
-
pyrmute-0.1.0.dist-info/licenses/LICENSE,sha256=otWInySiZeGwhHqQQ7n7nxM5QBSBe2CzeGEmQDZEz8Q,1119
|
11
|
-
pyrmute-0.1.0.dist-info/METADATA,sha256=0t3BYtlBAYNORci6prJdVpgzA9Q8A0kjHvTrw8cJCNs,4515
|
12
|
-
pyrmute-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
13
|
-
pyrmute-0.1.0.dist-info/top_level.txt,sha256=C8QtzqE6yBHkeewSp1QewvsyeHj_VQLYjSa5HLtMiow,8
|
14
|
-
pyrmute-0.1.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|