gemstone_utils.db¶
Process-global SQLAlchemy engine, session factory, and declarative base.
- class gemstone_utils.db.GemstoneDB(**kwargs)[source]¶
Bases:
DeclarativeBaseShared declarative base for
gemstone_utilsORM models.Subclass this base in plugin modules, import them before
init_db, then callinit_dbto create any missing tables onGemstoneDB.metadata.- Parameters:
kwargs (Any)
- metadata: ClassVar[MetaData] = MetaData()¶
Refers to the
_schema.MetaDatacollection that will be used for new_schema.Tableobjects.See also
- registry: ClassVar[_RegistryType] = <sqlalchemy.orm.decl_api.registry object>¶
Refers to the
_orm.registryin use where new_orm.Mapperobjects will be associated.
- gemstone_utils.db.get_session()[source]¶
Return a new session bound to the engine from
init_db().- Returns:
New
Session. Close when done, or use as a context manager:with get_session() as session:.- Raises:
RuntimeError – If
init_db()was not called.- Return type:
- gemstone_utils.db.init_db(db_url, *, echo=False, **engine_kw)[source]¶
Configure the process-global engine and create missing tables.
Import every module that defines
GemstoneDBsubclasses before calling this function so their tables are registered onGemstoneDB.metadata.Applies dialect-specific defaults (SQLite WAL; MySQL/MariaDB utf8mb4 and pool tuning; PostgreSQL UTC session timezone). Pass
**engine_kwto override or extendsqlalchemy.create_engine()arguments.Schema creation uses an advisory lock on PostgreSQL and MySQL/MariaDB so multiple workers can call
init_dbat startup without racing onCREATE TABLE.