Generic library db-utils
Provides common database-related functionality that can be used across different services.
Contains all database-related packages as dependencies. If you need to use some package that is not available in your service, you should add it here.
Common functionality
This section describes some classes or methods that are awailable for use in all services that use db-utils.
CustomQuery
As a default this class inherit from flask_sqlalchemy Query and adds additional filters.
- Filtering by only non-deleted (marked as is_alive=True) records.
- Joining with/without deleted (marked as is_alive=False) records.
If you want to add some additional by-default behavior to all services than you have to add it here.
transaction_commit
This context manager allows us to perform a database transaction at ORM level. You should use it like this:
with transaction_commit(): query.perform(something)
Abstract models
This section describes all available abstract models from which we can inherit in our services.
BaseModel
class BaseModel(DbModel, SerializerMixin):
__abstract__ = True
query_class = CustomQuery
id = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
date_created = mapped_column(db.DateTime(), default=datetime.utcnow(), nullable=False)
date_modified = mapped_column(db.DateTime(), default=datetime.utcnow(), nullable=False)
is_alive = mapped_column(db.Boolean(), default=True, nullable=False)
Provides UUID, record creation/modification datetime and is_alive field used for soft-deletion.
BaseUndeletableModel
class BaseUndeletableModel(DbModel, SerializerMixin):
__abstract__ = True
query_class = Query
id = mapped_column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
date_created = mapped_column(db.DateTime(), default=datetime.utcnow(), nullable=False)
date_modified = mapped_column(db.DateTime(), default=datetime.utcnow(), nullable=False)
The same thing as BaseModel but models that will inherit from this model won't be able to soft-delete records.
BaseMaterializedPGViewModel
class BaseMaterializedPGView(DbModel, SerializerMixin):
id = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
last_refresh_date = mapped_column(db.DateTime(), nullable=False)
sql = ''
refresh_by_tables: List[str] = []
query_class = Query
__table_args__ = {'info': {'skip_autogenerate': True}}
_index_format = '{table}_{field}_index'
_pkey_format = '{table}_pkey'
__abstract__ = True
Provides a way to create materialized views in PostgreSQL, add indexes, triggers, etc.
BaseImmutableModel
class BaseImmutableModel(DbModel, SerializerMixin):
__abstract__ = True
query_class = Query
id = mapped_column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
date_created = mapped_column(db.DateTime(), default=datetime.utcnow(), nullable=False)
user_created_id = mapped_column(PG_UUID(as_uuid=True), nullable=False)
Models that are going to inherit from this one won't have update record functionality.
ApiUser
class ApiUser(BaseModel):
__tablename__ = 'api_user'
date_expiration = mapped_column(db.DateTime(), nullable=False)
name = mapped_column(db.String(255), unique=True, nullable=False)
note = mapped_column(db.Text(), nullable=False)
permissions = mapped_column(ARRAY(db.Integer()), nullable=False)
Every APIUser Model record has an array of permissions.
Exceptions
| Exception | Desription |
|---|---|
| ComparisonToNullError | Raised when a client attempts to use a filter object that compares a resource's attribute to NULL using == operator instead of using is_null. |
| DBFiltersError | Raised when a client attempts to filter with invalid query params. |
| DBSortError | Raised when a client attempts to sort with invalid query params. |
| MultipleObjectsReturnedError | When only one object expected to be returned, but DB returned multiple objects. |
| UnknownFieldError | When user tries to reference the non-existent model field. |
| DeletionNotAllowedError | Raised when db obj deletion not allowed. |
| UpdateColumnNotAllowedError | Raised when db table column update not allowed. |
| UpdateNotAllowedError | Raised when db table update not allowed. |
| DbError | Generic DB error. |
Adding new database-related package
First, try to understand why do you need this library and what exactly can you do with it. Look at the list of already existing libraries and think if they can fulfill your needs.
Check this library for deprecation, does it have enough maintenance, library dependencies. If all above satisfies you, perform next steps:
- Add the package name and version to Pipfile under
[packages]section. Example:alembic = "==1.8.1".- Run
pipenv install.- Add the package name and version to setup.py to
install-requiressection.- Commit changes.
git commit -m "Add dependency *library-name*".- Run version patch:
pipenv run version_patch.- Push changes directly to dev
git push origin dev --tagsor raise MR for your changes to be reviewed.
Metadata
Release files for ul-db-utils 6.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ul_db_utils-6.0.3.tar.gz | 34.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ul_db_utils-6.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 94.7 kB
Release files / ul_db_utils-6.0.3.tar.gz
| Download URL | ul_db_utils-6.0.3.tar.gz |
|---|---|
| Size | 34.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ed11e2742c58f4692a15b52cf4ba47e02bfa41774ec8917d2e2067653274758e
|
|
BLAKE2b-256 checksum How to use checksums |
d1dd602af4ac60c331450d403e84d6d50e5b8998519f1ea6a4d1685e97bdb47c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Release files / ul_db_utils-6.0.3-py3-none-any.whl
| Download URL | ul_db_utils-6.0.3-py3-none-any.whl |
|---|---|
| Size | 60.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8e9f1189df8ab122eb11cf4692868c58bc47ef5b2639d4df67439a025f5ef55e
|
|
BLAKE2b-256 checksum How to use checksums |
c3268af4bb564ea2c4e58b2c97622b1480a8196c4bdf12c10c916a9cbe05b7ed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|