utils
create_base_metadata(base_obj=None, engine=None)
Create Base object's metadata.
Description
Import this function early in your app/script (i.e. main.py) and run as soon as
possible, i.e. after imports.
This function accepts a SQLAlchemy DeclarativeBase object, and creates the table
metadata from that object using the Engine passed.
This function will only ever return True if successful. It does not return False,
as an Exception is raised if metadata creation fails and the program is halted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_obj |
DeclarativeBase
|
A SQLAlchemy |
None
|
engine |
Engine
|
The |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When input values are invalid |
OperationalError
|
When SQLAlchemy runs into an error with the database, usually starting on the database (not in SQLAlchemy) |
DBAPIERROR
|
When SQLAlchemy runs into an issue, generally in the way you've coded a SQLAlchemy statement or operation |
Exception
|
When an uncaught/unhandled exception occurs |
Source code in src/red_utils/ext/sqlalchemy_utils/utils.py
debug_metadata_obj(metadata_obj=None)
Debug-print a SQLAlchemy MetaData object.
Loop over tables and print names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata_obj |
MetaData
|
A SQLAlchemy |
None
|
Source code in src/red_utils/ext/sqlalchemy_utils/utils.py
generate_metadata(metadata_obj=None, engine=None)
Create SQLAlchemy table metadata.
Accept a SQLalchemy MetaData object, run .create_all(engine) to create table metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata_obj |
MetaData
|
A SQLAlchemy |
None
|
engine |
Engine
|
The SQLAlchemy |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When input values are invalid |
OperationalError
|
When SQLAlchemy runs into an error with the database, usually starting on the database (not in SQLAlchemy) |
DBAPIERROR
|
When SQLAlchemy runs into an issue, generally in the way you've coded a SQLAlchemy statement or operation |
Exception
|
When an uncaught/unhandled exception occurs |
Source code in src/red_utils/ext/sqlalchemy_utils/utils.py
get_engine(connection=None, db_type='sqlite', echo=False, pool_pre_ping=False)
Return a SQLAlchemy Engine object.
To use a database other than SQLite, i.e. Postgres or MySQL, pass the lowercase string name of the database.
Currently supported databases
- [x] SQLite
- [x] Postgres
- [ ] MySQL
- [x] MSSQL
- [ ] Azure Cosmos
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection |
(saSQLiteConnection, saPGConnection)
|
Instantiated instance of a custom database connection class |
None
|
db_type |
str
|
The string name (lowercase) of a database type |
'sqlite'
|
echo |
bool
|
If |
False
|
pool_pre_ping |
bool
|
Test connection pool before starting operations |
False
|
Returns
(sqlalchemy.Engine): An initialized SQLAlchemy `Engine` object
Source code in src/red_utils/ext/sqlalchemy_utils/utils.py
get_session_pool(engine=None, autoflush=False, expire_on_commit=False, class_=so.Session)
Define a factory for creating SQLAlchemy sessions.
Returns a sqlalchemy.orm.sessionmaker Session instance. Import this
function in scripts that interact with the database, and create a
SessionLocal object with SessionLocal = get_session(**args)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine |
Engine
|
A SQLAlchemy |
None
|
autoflush |
bool
|
Automatically run |
False
|
expire_on_commit |
bool
|
If |
False
|
class_ |
You can specify a class which should be returned instead of |
Session
|
Returns
(sessionmaker[Session]): An initialized `Session` instance
Source code in src/red_utils/ext/sqlalchemy_utils/utils.py
validate_db_type(in_str=None)
Validate db_type string in functions that utilize db_type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_str |
str
|
A |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the |