feat(db): Change default alembic env.py to get connection string from env vars
This commit is contained in:
parent
684f60d2ce
commit
e8bb5ce009
31
db/env.py
31
db/env.py
@ -1,6 +1,10 @@
|
|||||||
|
import os
|
||||||
from logging.config import fileConfig
|
from logging.config import fileConfig
|
||||||
|
|
||||||
from sqlalchemy import engine_from_config
|
# #3 Get connection string from env vars instead of a Git-committed env.py
|
||||||
|
# http://allan-simon.github.io/blog/posts/python-alembic-with-environment-variables/
|
||||||
|
# from sqlalchemy import engine_from_config
|
||||||
|
from sqlalchemy import engine_from_config, create_engine
|
||||||
from sqlalchemy import pool
|
from sqlalchemy import pool
|
||||||
|
|
||||||
from alembic import context
|
from alembic import context
|
||||||
@ -26,6 +30,18 @@ target_metadata = None
|
|||||||
# ... etc.
|
# ... etc.
|
||||||
|
|
||||||
|
|
||||||
|
def get_url():
|
||||||
|
# #3 Get connection string from env vars instead of a Git-committed env.py
|
||||||
|
# http://allan-simon.github.io/blog/posts/python-alembic-with-environment-variables/
|
||||||
|
return "%s://%s:%s@%s/%s" % (
|
||||||
|
os.getenv("DB_DIALECTDRIVER", "mysql"),
|
||||||
|
os.getenv("DB_USER", "kodi-timekeeper"),
|
||||||
|
os.getenv("DB_PASSWORD", "kodi-timekeeper"),
|
||||||
|
os.getenv("DB_HOST", "localhost"),
|
||||||
|
os.getenv("DB_NAME", "kodi-timekeeper"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def run_migrations_offline():
|
def run_migrations_offline():
|
||||||
"""Run migrations in 'offline' mode.
|
"""Run migrations in 'offline' mode.
|
||||||
|
|
||||||
@ -38,7 +54,10 @@ def run_migrations_offline():
|
|||||||
script output.
|
script output.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
url = config.get_main_option("sqlalchemy.url")
|
# #3 Get connection string from env vars instead of a Git-committed env.py
|
||||||
|
# http://allan-simon.github.io/blog/posts/python-alembic-with-environment-variables/
|
||||||
|
# url = config.get_main_option("sqlalchemy.url")
|
||||||
|
url = get_url()
|
||||||
context.configure(
|
context.configure(
|
||||||
url=url,
|
url=url,
|
||||||
target_metadata=target_metadata,
|
target_metadata=target_metadata,
|
||||||
@ -57,11 +76,9 @@ def run_migrations_online():
|
|||||||
and associate a connection with the context.
|
and associate a connection with the context.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
connectable = engine_from_config(
|
# #3 Get connection string from env vars instead of a Git-committed env.py
|
||||||
config.get_section(config.config_ini_section),
|
# http://allan-simon.github.io/blog/posts/python-alembic-with-environment-variables/
|
||||||
prefix="sqlalchemy.",
|
connectable = create_engine(get_url())
|
||||||
poolclass=pool.NullPool,
|
|
||||||
)
|
|
||||||
|
|
||||||
with connectable.connect() as connection:
|
with connectable.connect() as connection:
|
||||||
context.configure(
|
context.configure(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user