-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.py
54 lines (38 loc) · 1.52 KB
/
env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# ruff: noqa: INP001
from logging.config import fileConfig
from alembic import context
import sqlalchemy
import tenacity
from sample_backend.core.config import SETTINGS
from sample_backend.entities import db_models
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = db_models.Base.metadata
def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
sql_engine = sqlalchemy.create_engine(
SETTINGS.postgres_url,
poolclass=sqlalchemy.pool.NullPool,
)
# When we run migrations during development or testing Postgres from Docker Compose may still be starting.
_wait_for_postgres(sql_engine)
with sql_engine.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
@tenacity.retry(stop=tenacity.stop_after_delay(10), wait=tenacity.wait_fixed(0.2), reraise=True)
def _wait_for_postgres(sql_engine: sqlalchemy.Engine) -> None:
with sql_engine.connect():
pass
if context.is_offline_mode():
raise NotImplementedError("We don't have offline migrations")
else:
run_migrations_online()