Menu
Introduction
Getting Started
Developer guide
Release
Architecture
CLI
Technical documentations
Version
Publication date

Mar 9, 2022

Confidentiality
Public
Reactions
1
Share

LazyAbstractDbManager

Abstract Database Manager that automatically handles Docker container startup logic.

Subclasses only need to define:

  • get_name: Name of the DB for this brick
  • get_brick_name: Name of the brick

Everything else (host, database name, port, credentials, network configuration) is handled automatically.

Example: class MyBrickDbManager(LazyAbstractDbManager): db = DatabaseProxy()

    _instance: Optional['MyBrickDbManager'] = None

    @classmethod
    def get_instance(cls) -> 'MyBrickDbManager':
        if cls._instance is None:
            cls._instance = cls()
        return cls._instance

    def get_name(self) -> str:
        return 'db'

    def get_brick_name(self) -> str:
        return 'my_brick'
Attributes
db: DatabaseProxymode: Optional
Functions
check_connection

Check if the database connection is working

Return type : bool
close_db

Close the db connection

connect_db

Open the db connection

create_tables

Create the tables for the provided models

models : list
drop_tables

Drop the tables for the provided models

models : list
execute_sql

Execute the provided sql command

sql : str
get_brick_name

Return the brick name for this database manager. Must be implemented by subclasses.

Return type : str
get_config

Get database configuration with credentials from Docker service

mode : Literal
Return type : DbConfig
get_db

Get the db object

Return type : DatabaseProxy
get_engine

Get the db object

Return type : Literal
get_name

Return the name of the DbManager. The combination of brick name and unique name must be unique accross all DbManager inheritors.

Return type : str
get_unique_name
Return type : str
has_transaction

Check if there is an active transaction

Return type : bool
ignore_error_on_init

If True, errors during initialization from DbManagerService will be ignored If False, errors during initialization will raise exceptions

Useful if the database need to be initialized manually later (eg: downloaded first)

Return type : bool
init

Initialize the database by starting the Docker container first

mode : Literal
is_initialized

Return if the db manager was initialized

Return type : bool
is_lazy_init

This DbManager is always lazy initialized.

Return type : bool
is_mysql_engine

Test if the mysql engine is active

register_after_commit_hook

Register a hook to be called after a transaction is committed

hook : Callable
Hook to register
close_dbs @classmethod

Close all db managers

get_db_managers @classmethod

Get all the classes that inherit this class

Return type : set
get_instance @classmethod

Get the singleton instance of this DbManager subclass

Must be called implement in sub concrete classes.

Return type : AbstractDbManager
reconnect_dbs @classmethod

Reconnect all db managers

transaction @classmethod

Decorator to create a new transaction around a method If an exception is raised, the transaction is rolled back

Can also be used as a context manager.

nested_transaction : bool - False
if False only 1 transaction is created at a time and it waits for the transaction to finish before creating a new one. If True, a transaction is created each time this method is called
Return type : Callable
Shine Logo
Technical bricks to reuse or customize

Have you developed a brick?

Share it to accelerate projects for the entire community.