apicrud.database

database.py

Database session management and schema update

created 31-mar-2019 by richb@instantlinux.net

Functions

alembic_migrate(models, version, script_location)

run schema migrations

get_session([scopefunc, scoped, db_url, engine])

open a db session scoped to flask context or celery thread

initialize_db(models[, db_url, engine, …])

initialize database connectivity

apicrud.database.alembic_migrate(models, version, script_location, migrate=False, db_session=None, schema_maxtime=0, seed_func=None)

run schema migrations

Parameters
  • models (obj) – the models file object

  • version (str) – schema version expected after migration

  • script_location (str) – relative path name of alembic’s env.py

  • db_session (obj) – existing db session

  • migrate (bool) – whether to run alembic migrations

  • schema_maxtime (int) – how long to wait for migration

  • seed_func (function) – function to seed initial records in blank db

apicrud.database.get_session(scopefunc=None, scoped=True, db_url=None, engine=None)

open a db session scoped to flask context or celery thread

Parameters
  • scopefunc (function) – function which returns a unique thread ID

  • scoped (bool) – whether to use scoped session management

  • db_url (str) – URL of database

  • engine (obj) – override engine object (for unit tests)

Returns

session

Return type

obj

apicrud.database.initialize_db(models, db_url=None, engine=None, redis_conn=None, redis_host=None, migrate=False, geo_support=False, connection_timeout=0, schema_update=None, schema_maxtime=60)

initialize database connectivity

Parameters
  • models (obj) – the models file object

  • db_url (str) – URL of database

  • engine (obj) – override engine object (for unit tests)

  • redis_conn (obj) – redis connection object override (for unit tests)

  • redis_host (str) – IP or hostname of redis service

  • migrate (bool) – whether to run alembic migrations

  • geo_support (bool) – whether to use GIS data types in db

  • connection_timeout (int) – how long to wait for DB connection

  • schema_update (function) – name of function to run for schema update

  • schema_maxtime (int) – how long to wait for migration