autowisp.database.interface module

Class Inheritance Diagram

Inheritance diagram of DataModelBase, DatabaseError, FrozenRow, NullPool, sessionmaker

Connect to the database and provide a session scope for queries.

autowisp.database.interface.DB_URL_FNAME = 'autowisp_db.url'

Filename (relative to project home) where a non-SQLite connection URL is stored.

When a project is initialised with a centralised database (MySQL, MariaDB, etc.) the connection URL is written to this file so that subsequent calls to set_project_home() with only the directory path can reconnect without requiring the caller to supply the URL again.

autowisp.database.interface.get_db_engine()[source]

Return the database engine.

autowisp.database.interface.get_project_home()[source]

Return the project home directory currently being used.

autowisp.database.interface.initialize_cmdline_database()[source]

Initialize the current database HDF5 structure tables.

autowisp.database.interface.set_project_home(project_home, db_url=None, new_project=False, *, migrate=False, assume_backed_up=False)[source]

Set the database engine and session for the given project home.

On first use with a non-SQLite db_url the URL is persisted to <project_home>/autowisp_db.url so that subsequent calls with only project_home reconnect to the same database automatically.

Parameters:
  • project_home – Directory used as the project home. For SQLite (the default), the database file autowisp.db is created here. For centralised databases the directory is still used for other project files (HDF5 products, etc.). Pass None to use the platform-appropriate user data directory.

  • db_url – SQLAlchemy connection URL. When omitted (or None) the function first checks for a previously saved URL in <project_home>/autowisp_db.url; if none is found it falls back to an SQLite database in project_home: sqlite:///<project_home>/autowisp.db?timeout=100&uri=true. To connect to a centralised server pass the full URL, e.g.: "mysql+pymysql://user:password@host:3306/dbname" "mariadb+pymysql://user:password@host:3306/dbname" Passing an explicit URL raises an error if a saved URL is found.

  • new_project – Pass True when this call is creating a project rather than opening an existing one. The target database must then contain none of the AutoWISP tables, since project creation goes on to drop and recreate them (see initialize_database) – which on a centralised server would silently destroy whichever project already lives in that database. Independent of backend: it equally catches an SQLite project home that already holds a database.

  • migrate – Bring the schema up to date rather than merely requiring that it already is. Off by default because every process opening a project calls this, including every pipeline worker, and concurrent schema changes from dozens of workers is exactly what must not happen. Set it only where a single process is known to be in charge: the wisp-migrate command, the browser interface selecting a project, and the main process of a pipeline run.

  • assume_backed_up – Passed through to migrate_project(); only meaningful with migrate. Required to migrate a server database, which cannot be copied aside automatically.

Returns:

The result of migrate_project() when

migrate is set and the project already existed, otherwise None.

Return type:

dict or None

Raises:

DatabaseError – If new_project is set and the target database already contains AutoWISP tables. Nothing is written or dropped in that case: the URL file is persisted only after this check passes.

autowisp.database.interface.snapshot_row(orm_obj, *, exclude=())[source]

Freeze all mapped columns of a live ORM instance into a FrozenRow.

Must be called while orm_obj is still attached/loaded (i.e. inside the start_db_session() block that produced it), otherwise touching an expired column would raise DetachedInstanceError.

Parameters:
  • orm_obj – A SQLAlchemy ORM instance.

  • exclude (Iterable[str]) – Column keys to omit (e.g. large or sensitive columns).

Returns:

Snapshot of the instance’s column values, detached

from the session and safe to pickle.

Return type:

FrozenRow

autowisp.database.interface.start_db_session()[source]

Context manager to start a database session.