Commit ffd43c2
Jonas Danke
feat: refactor database engine management with dual-backend support (OEP + local egon-data)
This commit introduces a comprehensive refactoring of how eDisGo manages
database connections, enabling seamless switching between the
OpenEnergyPlatform (OEP) and local egon-data PostgreSQL databases.
## Engine as EDisGo property (edisgo/edisgo.py)
- Add lazy-initialized `engine` property to the EDisGo class, replacing
the previous pattern of passing engine explicitly to every function call
- Support multiple initialization modes via new constructor parameters:
`engine`, `db_config_path`, `db_url`, `db_ssh`, `db_token`
- Default to OEP engine when no configuration is provided
- Add `__deepcopy__` method that excludes the unpicklable SQLAlchemy
engine (contains _thread._local objects) and lets the copy lazily
recreate its own connection
## Optional engine parameter in all DB functions
Make the `engine` parameter optional (default=None) in all 33 functions
across 8 modules that access the database. When engine is None, each
function falls back to `edisgo_object.engine`. This maintains full
backward compatibility — callers can still pass an explicit engine.
Modified modules:
- edisgo/io/timeseries_import.py (10 functions)
- edisgo/io/electromobility_import.py (4 functions)
- edisgo/io/dsm_import.py (3 functions)
- edisgo/io/heat_pump_import.py (2 functions)
- edisgo/io/generators_import.py (1 function)
- edisgo/io/storage_import.py (1 function)
- edisgo/network/heat.py (2 engine fallbacks)
- edisgo/network/timeseries.py (1 engine fallback)
## Local DB schema resolution (edisgo/tools/config.py)
Extend `import_tables_from_oep()` to handle local egon-data databases:
- Auto-resolve schema mismatches: tables may reside in different schemas
locally vs on OEP (e.g. egon_etrago_bus is in "grid" locally but
imported via "supply" on OEP). The method now searches all schemas
when a table is not found in the expected schema.
- Handle tables without primary keys (e.g. egon_map_zensus_grid_districts,
egon_daily_heat_demand_per_climate_zone) by passing all columns as
synthetic PK via `__mapper_args__["primary_key"]`.
## SSH tunnel lifecycle management (edisgo/io/db.py)
- `ssh_tunnel()` now returns `tuple[str, SSHTunnelForwarder]` instead of
just the port string, so the server object is no longer lost
- `engine()` stores the SSH server as `engine._ssh_server` for later
cleanup via `server.stop()`
- This fixes the "I/O operation on closed file" logging errors caused by
orphaned paramiko keepalive threads writing to closed log handlers
## Test infrastructure (tests/conftest.py)
- Add `--runlocal` flag to run all DB tests against both OEP and local
egon-data database
- Add `--egon-data-config` flag for custom YAML config path
- Parametrize tests via `db_engine` fixture: each DB test runs as
`test_name[oep]` and `test_name[local]` when --runlocal is active
- Add `pytest_sessionfinish` hook that disposes engines and stops SSH
tunnels cleanly after all tests complete
- Suppress paramiko DEBUG keepalive logging (defense-in-depth)
- Migrate all 29 test methods across 10 test files from hardcoded
`pytest.engine` to parametrized `db_engine` fixture
## New files
- tests/io/test_db.py: 4 tests for SSH tunnel lifecycle (tunnel returns
server, engine stores server, cleanup stops tunnel, OEP has no tunnel)
- egon-data.configuration.yaml: Template config file with placeholder
credentials for local egon-data database connections1 parent e5c9f0c commit ffd43c2
28 files changed
Lines changed: 621 additions & 221 deletions
File tree
- edisgo
- io
- network
- opf
- tools
- tests
- io
- network
- tools
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
35 | 34 | | |
36 | 35 | | |
37 | 36 | | |
| |||
131 | 130 | | |
132 | 131 | | |
133 | 132 | | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
134 | 153 | | |
135 | 154 | | |
136 | 155 | | |
| |||
157 | 176 | | |
158 | 177 | | |
159 | 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 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
160 | 209 | | |
161 | 210 | | |
162 | 211 | | |
163 | 212 | | |
164 | 213 | | |
165 | 214 | | |
166 | 215 | | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
167 | 223 | | |
168 | 224 | | |
169 | 225 | | |
| |||
232 | 288 | | |
233 | 289 | | |
234 | 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 | + | |
| 318 | + | |
| 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 | + | |
235 | 351 | | |
236 | 352 | | |
237 | 353 | | |
| |||
559 | 675 | | |
560 | 676 | | |
561 | 677 | | |
562 | | - | |
| 678 | + | |
563 | 679 | | |
564 | 680 | | |
565 | 681 | | |
| |||
982 | 1098 | | |
983 | 1099 | | |
984 | 1100 | | |
985 | | - | |
| 1101 | + | |
986 | 1102 | | |
987 | 1103 | | |
988 | 1104 | | |
| |||
1130 | 1246 | | |
1131 | 1247 | | |
1132 | 1248 | | |
1133 | | - | |
| 1249 | + | |
| 1250 | + | |
1134 | 1251 | | |
1135 | 1252 | | |
1136 | 1253 | | |
1137 | 1254 | | |
1138 | | - | |
| 1255 | + | |
| 1256 | + | |
1139 | 1257 | | |
1140 | 1258 | | |
1141 | 1259 | | |
| |||
2032 | 2150 | | |
2033 | 2151 | | |
2034 | 2152 | | |
| 2153 | + | |
| 2154 | + | |
| 2155 | + | |
2035 | 2156 | | |
2036 | 2157 | | |
2037 | 2158 | | |
| |||
2136 | 2257 | | |
2137 | 2258 | | |
2138 | 2259 | | |
2139 | | - | |
| 2260 | + | |
| 2261 | + | |
| 2262 | + | |
2140 | 2263 | | |
2141 | 2264 | | |
2142 | 2265 | | |
| |||
2212 | 2335 | | |
2213 | 2336 | | |
2214 | 2337 | | |
| 2338 | + | |
| 2339 | + | |
| 2340 | + | |
2215 | 2341 | | |
2216 | 2342 | | |
2217 | 2343 | | |
| |||
2308 | 2434 | | |
2309 | 2435 | | |
2310 | 2436 | | |
2311 | | - | |
| 2437 | + | |
2312 | 2438 | | |
2313 | 2439 | | |
2314 | 2440 | | |
| |||
2327 | 2453 | | |
2328 | 2454 | | |
2329 | 2455 | | |
2330 | | - | |
2331 | | - | |
| 2456 | + | |
| 2457 | + | |
2332 | 2458 | | |
2333 | 2459 | | |
2334 | 2460 | | |
| |||
2340 | 2466 | | |
2341 | 2467 | | |
2342 | 2468 | | |
| 2469 | + | |
| 2470 | + | |
| 2471 | + | |
2343 | 2472 | | |
2344 | 2473 | | |
2345 | 2474 | | |
| |||
2351 | 2480 | | |
2352 | 2481 | | |
2353 | 2482 | | |
2354 | | - | |
| 2483 | + | |
2355 | 2484 | | |
2356 | 2485 | | |
2357 | 2486 | | |
| |||
2379 | 2508 | | |
2380 | 2509 | | |
2381 | 2510 | | |
2382 | | - | |
2383 | | - | |
| 2511 | + | |
| 2512 | + | |
2384 | 2513 | | |
2385 | 2514 | | |
| 2515 | + | |
| 2516 | + | |
| 2517 | + | |
2386 | 2518 | | |
2387 | 2519 | | |
2388 | 2520 | | |
| |||
3640 | 3772 | | |
3641 | 3773 | | |
3642 | 3774 | | |
3643 | | - | |
3644 | | - | |
| 3775 | + | |
| 3776 | + | |
| 3777 | + | |
| 3778 | + | |
3645 | 3779 | | |
3646 | 3780 | | |
3647 | 3781 | | |
| |||
0 commit comments