Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /trisongz_lazyops /src /lazyops /libs /dbinit /controller.py
| from __future__ import annotations | |
| from lazyops.libs.dbinit.base import Engine | |
| from lazyops.libs.dbinit.entities.entity import Entity | |
| from lazyops.libs.dbinit.entities.role import Role | |
| class Controller: | |
| """ | |
| Entrypoint for creating/dropping database entities and granting/revoking privileges once they are | |
| declared in code. This is a wrapper of sorts around :class:`lazyops.libs.dbinit.entities.entity.Entity`. | |
| """ | |
| def create_all(cls, engine: Engine | None = None) -> None: | |
| """ | |
| Attempts to create all declared entities. Typically run via `run_all`. | |
| :param engine: A :class:`sqlalchemy.Engine` that defines the connection to a Postgres instance/cluster. | |
| """ | |
| cls._handle_engine(engine) | |
| for entity in Entity.entities: | |
| entity._safe_create() | |
| def grant_all(cls, engine: Engine | None = None) -> None: | |
| """ | |
| Attempts to grant all declared privileges. Requires entities to exist, typically run via `run_all` or after | |
| `create_all`. | |
| :param engine: A :class:`sqlalchemy.Engine` that defines the connection to a Postgres instance/cluster. | |
| """ | |
| cls._handle_engine(engine) | |
| for entity in Entity.entities: | |
| if isinstance(entity, Role): | |
| entity._safe_grant() | |
| def run_all(cls, engine: Engine | None = None) -> None: | |
| """ | |
| Attempts to create all declared entities then grant all declared privileges. Main way to do so. | |
| :param engine: A :class:`sqlalchemy.Engine` that defines the connection to a Postgres instance/cluster. | |
| """ | |
| cls._handle_engine(engine) | |
| cls.create_all() | |
| cls.grant_all() | |
| def drop_all(cls, engine: Engine | None = None) -> None: | |
| """ | |
| Attempts to drop all declared entities. Typically run via `remove_all`. | |
| :param engine: A :class:`sqlalchemy.Engine` that defines the connection to a Postgres instance/cluster. | |
| """ | |
| cls._handle_engine(engine) | |
| for entity in reversed(Entity.entities): | |
| entity._safe_drop() | |
| def revoke_all(cls, engine: Engine | None = None) -> None: | |
| """ | |
| Attempts to revoke all declared privileges. Requires entities to exist, typically run via `remove_all` or before | |
| `drop_all`. | |
| :param engine: A :class:`sqlalchemy.Engine` that defines the connection to a Postgres instance/cluster. | |
| """ | |
| cls._handle_engine(engine) | |
| for entity in reversed(Entity.entities): | |
| if isinstance(entity, Role): | |
| entity._safe_revoke() | |
| def remove_all(cls, engine: Engine | None = None) -> None: | |
| """ | |
| Attempts to revoke all declared privileges then drop all declared entities. Main way to do so. | |
| :param engine: A :class:`sqlalchemy.Engine` that defines the connection to a Postgres instance/cluster. | |
| """ | |
| cls._handle_engine(engine) | |
| cls.revoke_all() | |
| cls.drop_all() | |
| def _all_entities_exist(cls, engine: Engine | None = None) -> bool: | |
| """ | |
| Checks if all declared entities exist in the cluster. | |
| :param engine: A :class:`sqlalchemy.Engine` that defines the connection to a Postgres instance/cluster. | |
| """ | |
| cls._handle_engine(engine) | |
| return all(entity._exists() for entity in Entity.entities) | |
| def _all_grants_exist(cls, engine: Engine | None = None) -> bool: | |
| """ | |
| Checks if all declared grants exist in the cluster. | |
| :param engine: A :class:`sqlalchemy.Engine` that defines the connection to a Postgres instance/cluster. | |
| """ | |
| cls._handle_engine(engine) | |
| return all( | |
| role._grants_exist() | |
| for role in Entity.entities | |
| if isinstance(role, Role) | |
| ) | |
| def _all_exist(cls, engine: Engine | None = None) -> bool: | |
| """ | |
| Checks if all declared entities and grants exist in the cluster. | |
| :param engine: A :class:`sqlalchemy.Engine` that defines the connection to a Postgres instance/cluster. | |
| """ | |
| cls._handle_engine(engine) | |
| return cls._all_entities_exist() and cls._all_grants_exist() | |
| def _handle_engine(engine: Engine | None = None) -> None: | |
| """ | |
| Utility to assign the engine if it is provided. | |
| :param engine: A :class:`sqlalchemy.Engine` that defines the connection to a Postgres instance/cluster. | |
| """ | |
| if engine: | |
| Entity._engine = engine | |
Xet Storage Details
- Size:
- 4.66 kB
- Xet hash:
- bed31ffd03c01bdbada2c6534b3d4d2e79d8fae63c8017f55887fcbbb9ea9bbc
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.