Schema class¶
edgy.core.connection.schemas.Schema
¶
Manages all schema-related operations within the Edgy framework.
This class encapsulates functionalities for creating, dropping, and introspecting database schemas, ensuring proper model integration within multi-tenant or schema-isolated environments.
Initializes the Schema manager with a given registry.
| PARAMETER | DESCRIPTION |
|---|---|
registry
|
The Edgy registry instance, providing access to models, databases, and other core components.
TYPE:
|
Source code in edgy/core/connection/schemas.py
database
property
¶
Provides direct access to the default database configured in the registry.
| RETURNS | DESCRIPTION |
|---|---|
Database
|
The default database instance from the registry. |
get_default_schema
¶
Retrieves the default schema name from the underlying database dialect.
This method caches the default schema name after its first retrieval to optimize subsequent calls.
| RETURNS | DESCRIPTION |
|---|---|
str | None
|
The name of the default schema, or None if not applicable or found. |
Source code in edgy/core/connection/schemas.py
create_schema
async
¶
create_schema(
schema,
if_not_exists=False,
init_models=False,
init_tenant_models=False,
update_cache=True,
databases=(None,),
)
Creates a new database schema and optionally initializes models within it.
This method handles the creation of a new schema and can populate it with tables for both regular models and tenant-specific models, respecting global field constraints.
| PARAMETER | DESCRIPTION |
|---|---|
schema
|
The name of the schema to be created or None for main.
TYPE:
|
if_not_exists
|
If True, the schema will only be created if it does not already exist, preventing an error. Defaults to False.
TYPE:
|
init_models
|
If True, all models registered with the registry will have their tables created within the new schema. Defaults to False.
TYPE:
|
init_tenant_models
|
If True, tenant-specific models will have their tables created within the new schema. This operation temporarily bypasses global field constraints. Defaults to False.
TYPE:
|
update_cache
|
If True, the model's schema cache will be updated. Defaults to True.
TYPE:
|
databases
|
A sequence of database names (keys from
TYPE:
|
Raises: SchemaError: If there is an issue during schema creation or table initialization within the schema.
Source code in edgy/core/connection/schemas.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
drop_schema
async
¶
Drops an existing database schema, optionally cascading the drop to all contained objects.
| PARAMETER | DESCRIPTION |
|---|---|
schema
|
The name of the schema to be dropped. If None fallback to the classic drop_all logic.
TYPE:
|
cascade
|
If True, all objects (tables, views, etc.) within the schema will also be dropped. Defaults to False.
TYPE:
|
if_exists
|
If True, the schema will only be dropped if it exists, preventing an error if it does not. Defaults to False.
TYPE:
|
databases
|
A sequence of database names (keys from
TYPE:
|
Raises: SchemaError: If there is an issue during schema drop operation.
Source code in edgy/core/connection/schemas.py
get_metadata_of_all_schemes
async
¶
Retrieves metadata and a list of all schema names for a given database.
This method reflects the table structures for registered models
within each discovered schema if no_reflect is False.
| PARAMETER | DESCRIPTION |
|---|---|
database
|
The database instance from which to retrieve schema metadata.
TYPE:
|
no_reflect
|
If True, tables will not be reflected into the metadata. Defaults to False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[MetaData, list[str]]
|
A tuple containing:
- sqlalchemy.MetaData: An SQLAlchemy MetaData object populated
with reflected tables (if |
Source code in edgy/core/connection/schemas.py
get_schemes_tree
async
¶
Builds a comprehensive tree-like structure of schemas across all registered databases.
Each entry in the resulting dictionary represents a database (identified by its name or None for the default), containing its URL, its SQLAlchemy MetaData, and a list of schema names.
| PARAMETER | DESCRIPTION |
|---|---|
no_reflect
|
If True, tables will not be reflected into the metadata for any schema. Defaults to False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str | None, tuple[str, MetaData, list[str]]]
|
A dictionary where keys are database names (or None for the default |
dict[str | None, tuple[str, MetaData, list[str]]]
|
database) and values are tuples containing: - str: The URL of the database. - sqlalchemy.MetaData: The MetaData object for the database. - list[str]: A list of schema names found in that database. |