codemation / pydbantic

A single model for shaping, creating, accessing, storing data within a Database
https://pydbantic.readthedocs.io/en/latest/
Apache License 2.0
228 stars 15 forks source link

Improved pickle loads error handling #29

Closed codemation closed 2 years ago

codemation commented 2 years ago

De-Serialization Enhancements

This PR improves a DataBaseModel's ability to handle model definition moves within a project. Because pickle expects the same module/attribute(BaseModel or other class) to exist when performing loads from a bytestring, movements such as complete DataBaseModel or attribute definitions could result in AttributeError during migration.

New Solution

If an AttributeError or ModuleNotFound error is detected during loads (deserialization), pydbantic will add a dummy module matching the missing module(if relevant), or add a generic BaseModel matching the missing attribute name to expected Module defition, which allows deserialization to complete, and thus migrations. It is expected that attribute location definition changes will trigger a migration.

Updated Tests

Previous tests covered partial handling of only AttributeError correction, but are now updated to include ModuleNotFoundError

New / Adjust in, not_in attribute methods

Added DataBaseModelAttribute method .inside() and .not_inside() for a more natural way of filtering via .filter() with a in or not in list of items condition, eventually replacing .matches & not_matches

Example

mid_salary_employees = await Employees.filter(
    Employees.salary.inside([30000, 40000])
)

# combining conditionals with keyword args
mid_salary_employees = await Employees.filter(
    Employees.OR(
        Employees.salary >= 30000,
        Employees.salary.inside([20000, 40000])
    ),
    is_employed = True
)