DatabaseContext

class felis.db.database_context.DatabaseContext

Bases: AbstractContextManager

Interface for managing database operations across different SQL dialects.

Attributes Summary

dialect

The SQLAlchemy dialect for the context (Dialect).

dialect_name

Get the dialect name for this database context (str).

engine

The SQAlchemy engine for the context (Engine).

metadata

The SQLAlchemy metadata representing the database for the context (MetaData).

Methods Summary

close()

Close and clean up database resources.

create_all()

Create all database objects in the schema using the metadata object.

create_indexes()

Create all indexes in the schema using the metadata object.

drop()

Drop the schema in the database if it exists.

drop_indexes()

Drop all indexes in the schema using the metadata object.

execute(statement[, parameters])

Execute a SQL statement and return the result.

initialize()

Create the target schema in the database if it does not exist already.

Attributes Documentation

dialect

The SQLAlchemy dialect for the context (Dialect).

dialect_name

Get the dialect name for this database context (str).

engine

The SQAlchemy engine for the context (Engine).

metadata

The SQLAlchemy metadata representing the database for the context (MetaData).

Methods Documentation

abstract close()

Close and clean up database resources.

Return type:

None

abstract create_all()

Create all database objects in the schema using the metadata object.

Raises:

DatabaseContextError – If there is an error creating the schema objects in the database.

Return type:

None

abstract create_indexes()

Create all indexes in the schema using the metadata object.

Raises:

DatabaseContextError – If there is an error creating the indexes in the database.

Return type:

None

abstract drop()

Drop the schema in the database if it exists.

Implementations should use IF EXISTS semantics to avoid raising an error if the schema does not exist.

Raises:

DatabaseContextError – If there is an error dropping the schema.

Return type:

None

abstract drop_indexes()

Drop all indexes in the schema using the metadata object.

Raises:

DatabaseContextError – If there is an error dropping the indexes in the database.

Return type:

None

abstract execute(statement, parameters=None)

Execute a SQL statement and return the result.

Parameters:
Returns:

The result of the statement execution.

Return type:

Result

Raises:

DatabaseContextError – If there is an error executing the SQL statement.

abstract initialize()

Create the target schema in the database if it does not exist already.

Sub-classes should implement idempotent behavior so that calling this method multiple times has no adverse effects. If the schema already exists, the method should simply return without raising an error. (A warning message may be logged in this case.)

Raises:

DatabaseContextError – If there is an error instantiating the schema.

Return type:

None