Package: ul-db-utils
Generic library db-utilsProvides 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 functionalityThis section describes some classes or methods that are awailable for use in all services that use db-utils.CustomQueryAs a default this class inherit fromflask_sqlalchemy BaseQueryand adds additional filters.Filtering by only non-deleted (marked asis_alive=True) records.Joining with/without deleted (marked asis_alive=False) records.If you want to add some additional by-default behavior to all services than you have to add it here.transaction_commitThis context manager allows us to perform a database transaction at ORM level. You should use it like this:withtransaction_commit():query.perform(something)Abstract modelsThis section describes all available abstract models from which we can inherit in our services.BaseModelclassBaseModel(DbModel,SerializerMixin):__abstract__=Truequery_class=CustomQueryid=db.Column(UUID(as_uuid=True),primary_key=True,default=uuid.uuid4)date_created=db.Column(db.DateTime(),default=datetime.utcnow(),nullable=False)date_modified=db.Column(db.DateTime(),default=datetime.utcnow(),nullable=False)is_alive=db.Column(db.Boolean(),default=True,nullable=False)Provides UUID, record creation/modification datetime and is_alive field used for soft-deletion.BaseUndeletableModelclassBaseUndeletableModel(DbModel,SerializerMixin):__abstract__=Truequery_class=BaseQueryid=db.Column(PG_UUID(as_uuid=True),primary_key=True,default=uuid.uuid4)date_created=db.Column(db.DateTime(),default=datetime.utcnow(),nullable=False)date_modified=db.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.BaseMaterializedPGViewModelclassBaseMaterializedPGView(DbModel,SerializerMixin):id=db.Column(UUID(as_uuid=True),primary_key=True,default=uuid.uuid4)last_refresh_date=db.Column(db.DateTime(),nullable=False)sql=''refresh_by_tables:List[str]=[]query_class=BaseQuery__table_args__={'info':{'skip_autogenerate':True}}_index_format='{table}_{field}_index'_pkey_format='{table}_pkey'__abstract__=TrueProvides a way to create materialized views in PostgreSQL, add indexes, triggers, etc.BaseImmutableModelclassBaseImmutableModel(DbModel,SerializerMixin):__abstract__=Truequery_class=BaseQueryid=db.Column(PG_UUID(as_uuid=True),primary_key=True,default=uuid.uuid4)date_created=db.Column(db.DateTime(),default=datetime.utcnow(),nullable=False)user_created_id=db.Column(PG_UUID(as_uuid=True),nullable=False)Models that are going to inherit from this one won't have update record functionality.ApiUserclassApiUser(BaseModel):__tablename__='api_user'date_expiration=db.Column(db.DateTime(),nullable=False)name=db.Column(db.String(255),unique=True,nullable=False)note=db.Column(db.Text(),nullable=False)permissions=db.Column(ARRAY(db.Integer()),nullable=False)Every APIUser Model record has an array of permissions.ExceptionsExceptionDesriptionComparisonToNullErrorRaised when a client attempts to use a filter object that compares a resource's attribute to NULL using == operator instead of using is_null.DBFiltersErrorRaised when a client attempts to filter with invalid query params.DBSortErrorRaised when a client attempts to sort with invalid query params.MultipleObjectsReturnedErrorWhen only one object expected to be returned, but DB returned multiple objects.UnknownFieldErrorWhen user tries to reference the non-existent model field.DeletionNotAllowedErrorRaised when db obj deletion not allowed.UpdateColumnNotAllowedErrorRaised when db table column update not allowed.UpdateNotAllowedErrorRaised when db table update not allowed.DbErrorGeneric DB error.Adding new database-related packageFirst, 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 toPipfileunder[packages]section. Example:alembic = "==1.8.1".Runpipenv install.Add the package name and version tosetup.pytoinstall-requiressection.Commit changes.git commit -m "Add dependency *library-name*".Run version patch:pipenv run version_patch.Push changes directly to devgit push origin dev --tagsor raise MR for your changes to be reviewed.