DatabaseContext

class felis.db.utils.DatabaseContext(metadata, engine)

Bases: object

Manage the database connection and SQLAlchemy metadata.

Parameters:
  • metadata (MetaData) – The SQLAlchemy metadata object.

  • engine (Engine | MockConnection) – The SQLAlchemy engine or mock connection object.

Methods Summary

create_all()

Create all tables in the schema using the metadata object.

create_if_not_exists()

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

create_mock_engine(engine_url[, output_file])

Create a mock engine for testing or dumping DDL statements.

drop_if_exists()

Drop the schema in the database if it exists.

Methods Documentation

create_all()

Create all tables in the schema using the metadata object.

Return type:

None

create_if_not_exists()

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

Raises:
Return type:

None

Notes

In MySQL, this will create a new database and, in PostgreSQL, it will create a new schema. For other variants, this is an unsupported operation.

static create_mock_engine(engine_url, output_file=None)

Create a mock engine for testing or dumping DDL statements.

Parameters:
  • engine_url (str | URL) – The SQLAlchemy engine URL.

  • output_file (Optional[IO[str]], default: None) – The file to write the SQL statements to. If None, the statements will be written to stdout.

Returns:

The mock connection object.

Return type:

sqlalchemy.engine.mock.MockConnection

drop_if_exists()

Drop the schema in the database if it exists.

Raises:

ValueError – If the database is not supported.

Return type:

None

Notes

In MySQL, this will drop a database. In PostgreSQL, it will drop a schema. For other variants, this is an unsupported operation.