Schema
class¶
edgy.core.connection.schemas.Schema
¶
Schema(registry)
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
28 29 30 31 32 33 34 35 36 |
|
database
property
¶
database
Provides direct access to the default database configured in the registry.
RETURNS | DESCRIPTION |
---|---|
Database
|
The default database instance from the registry. |
get_default_schema
¶
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
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
activate_schema_path
async
¶
activate_schema_path(database, schema, is_shared=True)
Activates a specific schema within the database connection's search path.
This method modifies the search_path
for the current database session,
allowing queries to implicitly reference objects within the specified schema.
Warning: This method is deprecated and considered insecure due to improper schema escaping. It should not be used in production environments.
PARAMETER | DESCRIPTION |
---|---|
database
|
The database instance on which to activate the schema path.
TYPE:
|
schema
|
The name of the schema to add to the search path.
TYPE:
|
is_shared
|
If True, adds 'shared' to the search path along with the specified schema. Defaults to True.
TYPE:
|
Source code in edgy/core/connection/schemas.py
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 |
|
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.
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
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
drop_schema
async
¶
drop_schema(schema, cascade=False, if_exists=False, databases=(None,))
Drops an existing database schema, optionally cascading the drop to all contained objects.
PARAMETER | DESCRIPTION |
---|---|
schema
|
The name of the schema to be dropped.
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
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
get_metadata_of_all_schemes
async
¶
get_metadata_of_all_schemes(database, *, no_reflect=False)
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
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
get_schemes_tree
async
¶
get_schemes_tree(*, no_reflect=False)
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. |
Source code in edgy/core/connection/schemas.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
|