Database mixin
BaseModelType = TypeVar('BaseModelType', bound=BaseModel)
module-attribute
DatabaseSchemaType = TypeVar('DatabaseSchemaType', bound=DatabaseSchemaMixin | SchemaBase)
module-attribute
logger = logging.getLogger(__name__)
module-attribute
DatabaseSchemaMixin
Bases: BaseModel
Adds methods and values for storing in database (including id, version, deleted).
deleted = Field(default=False, exclude=False, description='Whether the schema has marked as deleted')
class-attribute
instance-attribute
id = Field(default=None, exclude=False, description='Database ID')
class-attribute
instance-attribute
iterate_nested_schemas = False
class-attribute
Should nested schemas (subclasses of DatabaseSchemaMixin) be iterated over for various operations. Defaults to False for efficiency. Also should be left False if the subclass handles behaviour explicitly.
new_instance_on_change = True
class-attribute
Should a whole new instance be created in the database for any change (version increment) vs changing in place.
private_incremented = False
class-attribute
instance-attribute
version = Field(default=None, exclude=False, description='Version of the schema')
class-attribute
instance-attribute
determine_version_increment(other)
Increment version for any updated schemas or sub-schemas.
I.e., 'self' is the updated schema to store in the database, and 'other' is what currently exists there.
equal_to(other, keys_to_recursively_remove=None, exclude_id=True, include_version=False)
For more complex comparisons where schema1 == schema2 is not sufficient.
E.g. Trying to compare all values without metadatas or ids.
increment_version()
Increment the version number.
Mark that model has been updated since last load. If self.id is set to None (after this) a new entry will be saved in the database.
Only makes sense to do this if the schema has an ID already.
mark_deleted(apply_nested=False)
reset_ids()
Set all ids to None.
Remove all persistence ids.
reset_incremented_for_testing()
Reset the incremented flag.
Only for testing purposes (where several updates to a schema are made within a single UOW).
For normal use, there should only ever be a single update to a schema within a UOW. Allowing multiple updates in a single UOW would allow multiple simultaneous UOWs to update the same record without clashing on version (e.g. if one UOW updates once and one twice before committing, there will be no apparent clash in version).
reset_version(nested)
Set all versions to None.
set_ids_from_model(model)
Set the ID from a model instance.
Used when creating a new schema from a model instance. Only used in repository/uow methods.
Note: Modified in place because it's safer for any schemas that ARE now associated with a saved model to have their ids set to avoid future accidental duplication.
set_initial_version()
NotFullyLoadedError
Bases: BackendError
Raised when a schema is not fully loaded but needs to be.
walk_all_schema_instances(schema_to_walk, class_type_to_yield, class_type_to_recurse=None, top_down=True)
Yield all sub-schemas in the schema (including the schema itself) recursively.
| PARAMETER | DESCRIPTION |
|---|---|
schema_to_walk
|
The schema to walk
TYPE:
|
class_type_to_yield
|
The class type to yield
TYPE:
|
class_type_to_recurse
|
The class type to recurse into (defaults to class_type_to_yield)
TYPE:
|
top_down
|
Whether to yield the schema itself first (True) or last (False)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Iterator[BaseModelType]
|
An iterator of all sub-schemas that are instances of class_type_to_yield |
walk_direct_sub_schema_instances(schema_to_walk, class_type_to_yield)
Yield all directly related sub-schemas that inherit from class_type_to_yield.
Directly related means schemas that are attributes of this schema, or contained within a list/dict of attributes of this schema, but does not recurse into sub-schemas.