DatabaseContext¶
- class felis.db.utils.DatabaseContext(metadata, engine)¶
Bases:
object
Manage the database connection and SQLAlchemy metadata.
- Parameters:
Methods Summary
Create all tables in the schema using the metadata object.
create_mock_engine
(engine_url[, output_file])Create a mock engine for testing or dumping DDL statements.
drop
()Drop the schema in the database if it exists.
execute
(statement)Execute a SQL statement on the engine and return the result.
Create the schema in the database if it does not exist.
Methods Documentation
- static create_mock_engine(engine_url, output_file=None)¶
Create a mock engine for testing or dumping DDL statements.
- drop()¶
Drop the schema in the database if it exists.
- Raises:
ValueError – Raised if the database is not supported.
- Return type:
Notes
In MySQL, this will drop a database. In PostgreSQL, it will drop a schema. A SQlite database will have all its tables dropped. For other database variants, this is currently an unsupported operation.
- execute(statement)¶
Execute a SQL statement on the engine and return the result.
- Parameters:
statement (
Any
) – The SQL statement to execute.- Returns:
The result of the statement execution.
- Return type:
sqlalchemy.engine.ResultProxy
Notes
This is just a wrapper around the execution method of the connection object, which may execute on a real or mock connection.
- initialize()¶
Create the schema in the database if it does not exist.
- Raises:
ValueError – Raised if the database is not supported or it already exists.
sqlalchemy.exc.SQLAlchemyError – Raised if there is an error creating the schema.
- Return type:
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.