11from __future__ import with_statement
2+
3+ import os
4+
25from alembic import context
36from sqlalchemy import engine_from_config , pool
47from logging .config import fileConfig
2730# ... etc.
2831
2932
33+ def get_url ():
34+ user = os .getenv ("POSTGRES_USER" , "postgres" )
35+ password = os .getenv ("POSTGRES_PASSWORD" , "" )
36+ server = os .getenv ("POSTGRES_SERVER" , "db" )
37+ db = os .getenv ("POSTGRES_DB" , "app" )
38+ return f"postgresql://{ user } :{ password } @{ server } /{ db } "
39+
40+
3041def run_migrations_offline ():
3142 """Run migrations in 'offline' mode.
3243
@@ -39,7 +50,7 @@ def run_migrations_offline():
3950 script output.
4051
4152 """
42- url = config . get_main_option ( "sqlalchemy.url" )
53+ url = get_url ( )
4354 context .configure (
4455 url = url , target_metadata = target_metadata , literal_binds = True , compare_type = True
4556 )
@@ -55,8 +66,10 @@ def run_migrations_online():
5566 and associate a connection with the context.
5667
5768 """
69+ configuration = config .get_section (config .config_ini_section )
70+ configuration ['sqlalchemy.url' ] = get_url ()
5871 connectable = engine_from_config (
59- config . get_section ( config . config_ini_section ) ,
72+ configuration ,
6073 prefix = "sqlalchemy." ,
6174 poolclass = pool .NullPool ,
6275 )
0 commit comments