Skip to content

Commit bece399

Browse files
ebretontiangolo
authored andcommitted
♻️ removed postgres_password from alembic.ini (#9)
♻️ removed postgres_password from alembic.ini (#9)
1 parent 5dd83c6 commit bece399

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

{{cookiecutter.project_slug}}/backend/app/alembic.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ script_location = alembic
3535
# are written from script.py.mako
3636
# output_encoding = utf-8
3737

38-
sqlalchemy.url = postgresql://postgres:{{cookiecutter.postgres_password}}@db/app
39-
40-
4138
# Logging configuration
4239
[loggers]
4340
keys = root,sqlalchemy,alembic

{{cookiecutter.project_slug}}/backend/app/alembic/env.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
from __future__ import with_statement
2+
3+
import os
4+
25
from alembic import context
36
from sqlalchemy import engine_from_config, pool
47
from logging.config import fileConfig
@@ -27,6 +30,14 @@
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+
3041
def 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

Comments
 (0)