Activate a runner and registry for the duration of a with block.
Example
from pelican import use_context
from pelican import loader
with use_context(database_url="postgresql://user:password@localhost/mydb") as runner:
registry = loader.load_migrations("db/migrations")
for migration in registry:
runner.upgrade(migration)
Source code in pelican/_context.py
59
60
61
62
63
64
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 | @contextmanager
def use_context(
runner: MigrationRunner | None = None,
*,
database_url: str | None = None,
) -> Iterator[MigrationRunner]:
"""Activate a runner and registry for the duration of a `with` block.
## Example
```python
from pelican import use_context
from pelican import loader
with use_context(database_url="postgresql://user:password@localhost/mydb") as runner:
registry = loader.load_migrations("db/migrations")
for migration in registry:
runner.upgrade(migration)
```
"""
active = runner or MigrationRunner(database_url=database_url)
registry = MigrationRegistry()
r_token = _active_runner.set(active)
reg_token = _active_registry.set(registry)
try:
yield active
finally:
_active_runner.reset(r_token)
_active_registry.reset(reg_token)
|