AnyBlok Multi Engines framework

Registry plugin

Session

class anyblok_multi_engines.registry.MixinSession

Bases: object

Mixin for the SQLAlchemy session the goal is to allow the connection with more than one engine: masters / slaves engines

get_bind(mapper=None, clause=None)

Overload the Session.get_bind method of SQLAlchemy

the rule are:

  • if unittest_transaction: durring unittest, they are no slaves / masters
  • if flushing: write on the database then we use the master
  • read the database then use a slave

Mixin of the registry

class anyblok_multi_engines.registry.MultiEngines

Bases: object

Mixin class which overload the AnyBlok Registry class

the goal is to implement in the the Registry the masters / slaves behaviour

bind

Return the bind in function of engine

close()

Overwrite close to cloe all the engines

create_session_factory()

Overwrite the creation of Session factory to Use the multi binding

engine

Return the engine

get_engine_for(ro=True)

Return one engine among the engines

Parameters:ro – if True the engine will be read only else write only
Return type:engine
Exception:RegistryException
init_bind()

Initialise the bind for unittest

init_engine(db_name=None)

Overload the initiation of engine to create more than one engine

use the Configuration option to create engines:

  • db_url: read and write engine
  • db_ro_urls: read only engines (list)
  • db_wo_url: write only engines

Warning

All the engines use the same database name not at the same location

Parameters:db_name – name of the database for the engines

Plugin class

class anyblok_multi_engines.registry.RegistryMultiEngines(db_name, loadwithoutmigration=False, unittest=False, **kwargs)

Bases: anyblok_multi_engines.registry.MultiEngines, anyblok.registry.Registry

Pluging class

get_url plugin

anyblok_multi_engines.config.get_url(db_name=None, url=None)

Return an sqlalchemy URL for database

Parameters:
  • db_name – Name of the database
  • url – base of the url
Return type:

SqlAlchemy URL

Exception:

ConfigurationException