From 7abb977dfe6edc6c18b1dbb05b3f53c6c57c78f5 Mon Sep 17 00:00:00 2001 From: ebreton Date: Fri, 12 Apr 2019 20:51:16 +0200 Subject: [PATCH 1/4] removed postgres_password from alembic.ini, read it from env var instead --- .../backend/app/alembic.ini | 3 --- .../backend/app/alembic/env.py | 18 ++++++++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/{{cookiecutter.project_slug}}/backend/app/alembic.ini b/{{cookiecutter.project_slug}}/backend/app/alembic.ini index 61bc2fdcbf..921aaf17b8 100755 --- a/{{cookiecutter.project_slug}}/backend/app/alembic.ini +++ b/{{cookiecutter.project_slug}}/backend/app/alembic.ini @@ -35,9 +35,6 @@ script_location = alembic # are written from script.py.mako # output_encoding = utf-8 -sqlalchemy.url = postgresql://postgres:{{cookiecutter.postgres_password}}@db/app - - # Logging configuration [loggers] keys = root,sqlalchemy,alembic diff --git a/{{cookiecutter.project_slug}}/backend/app/alembic/env.py b/{{cookiecutter.project_slug}}/backend/app/alembic/env.py index ae47ee586c..b7770629dd 100755 --- a/{{cookiecutter.project_slug}}/backend/app/alembic/env.py +++ b/{{cookiecutter.project_slug}}/backend/app/alembic/env.py @@ -1,4 +1,7 @@ from __future__ import with_statement + +import os + from alembic import context from sqlalchemy import engine_from_config, pool from logging.config import fileConfig @@ -27,6 +30,15 @@ # ... etc. +def get_url(): + return "postgresql://%s:%s@%s/%s" % ( + os.getenv("POSTGRES_USER", "postgres"), + os.getenv("POSTGRES_PASSWORD", ""), + os.getenv("POSTGRES_SERVER", "db"), + os.getenv("POSTGRES_DB", "app"), + ) + + def run_migrations_offline(): """Run migrations in 'offline' mode. @@ -39,7 +51,7 @@ def run_migrations_offline(): script output. """ - url = config.get_main_option("sqlalchemy.url") + url = get_url() context.configure( url=url, target_metadata=target_metadata, literal_binds=True, compare_type=True ) @@ -55,8 +67,10 @@ def run_migrations_online(): and associate a connection with the context. """ + configuration = config.get_section(config.config_ini_section) + configuration['sqlalchemy.url'] = get_url() connectable = engine_from_config( - config.get_section(config.config_ini_section), + configuration, prefix="sqlalchemy.", poolclass=pool.NullPool, ) From 14fe548bcab8834274896628a86107e8cdcfde10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sat, 20 Apr 2019 19:48:51 +0400 Subject: [PATCH 2/4] :recycle: use f-strings for PostgreSQL URL --- .../backend/app/alembic/env.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/{{cookiecutter.project_slug}}/backend/app/alembic/env.py b/{{cookiecutter.project_slug}}/backend/app/alembic/env.py index b7770629dd..df37198286 100755 --- a/{{cookiecutter.project_slug}}/backend/app/alembic/env.py +++ b/{{cookiecutter.project_slug}}/backend/app/alembic/env.py @@ -31,12 +31,11 @@ def get_url(): - return "postgresql://%s:%s@%s/%s" % ( - os.getenv("POSTGRES_USER", "postgres"), - os.getenv("POSTGRES_PASSWORD", ""), - os.getenv("POSTGRES_SERVER", "db"), - os.getenv("POSTGRES_DB", "app"), - ) + user = os.getenv("POSTGRES_USER", "postgres") + password = os.getenv("POSTGRES_PASSWORD", "") + server = os.getenv("POSTGRES_SERVER", "db") + db = os.getenv("POSTGRES_DB", "app") + return f"postgresql://{user}:{password}@{server}/{db}" def run_migrations_offline(): From d54e868b80828ce1633d6dfe5ba7ea99393d019e Mon Sep 17 00:00:00 2001 From: ebreton Date: Mon, 20 May 2019 20:24:05 +0200 Subject: [PATCH 3/4] Make sure to import all SQLalchemy models --- {{cookiecutter.project_slug}}/backend/app/app/db/init_db.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/{{cookiecutter.project_slug}}/backend/app/app/db/init_db.py b/{{cookiecutter.project_slug}}/backend/app/app/db/init_db.py index 4f1d6f5aa3..c4b046d32d 100644 --- a/{{cookiecutter.project_slug}}/backend/app/app/db/init_db.py +++ b/{{cookiecutter.project_slug}}/backend/app/app/db/init_db.py @@ -2,6 +2,8 @@ from app.core import config from app.models.user import UserCreate +from .base import * # noqa + def init_db(db_session): # Tables should be created with Alembic migrations From e9a8fd799f79026ef7270e62f50bc147ec04ba91 Mon Sep 17 00:00:00 2001 From: ebreton Date: Wed, 22 May 2019 07:14:34 +0200 Subject: [PATCH 4/4] Add comment to document fix --- {{cookiecutter.project_slug}}/backend/app/app/db/init_db.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/{{cookiecutter.project_slug}}/backend/app/app/db/init_db.py b/{{cookiecutter.project_slug}}/backend/app/app/db/init_db.py index c4b046d32d..6374273132 100644 --- a/{{cookiecutter.project_slug}}/backend/app/app/db/init_db.py +++ b/{{cookiecutter.project_slug}}/backend/app/app/db/init_db.py @@ -2,7 +2,10 @@ from app.core import config from app.models.user import UserCreate -from .base import * # noqa +# make sure all SQL Alchemy models are imported before initializing DB +# otherwise, SQL Alchemy might fail to initialize properly relationships +# for more details: https://github.com/tiangolo/full-stack-fastapi-postgresql/issues/28 +from app.db import base def init_db(db_session):