Created
August 6, 2019 15:32
-
-
Save moraisaugusto/70aa938e8eca04b6d681d4e53addb8a9 to your computer and use it in GitHub Desktop.
SqlAlchemy + Alembic + DYNACONF +SSL enabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import with_statement | |
import sys,os | |
from logging.config import fileConfig | |
from sqlalchemy import engine_from_config | |
from sqlalchemy import pool | |
from alembic import context | |
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) | |
from models import Base | |
# Please define Database name to be used | |
DATABASE = "DATABASE" | |
# 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. | |
fileConfig(config.config_file_name) | |
# add your model's MetaData object here | |
# for 'autogenerate' support | |
# from myapp import mymodel | |
# target_metadata = mymodel.Base.metadata | |
target_metadata = Base.metadata | |
# other values from the config, defined by the needs of env.py, | |
# can be acquired: | |
# my_important_option = config.get_main_option("my_important_option") | |
# ... etc. | |
def get_db_url(): | |
"""Get url DB connection based on environment variables | |
more details on README.MD file | |
Args: | |
None | |
Returns: | |
url db connect (string) | |
""" | |
import yaml | |
env = os.getenv("ENV_FOR_DYNACONF", "production") | |
secrets_file = os.getenv("SECRETS_FOR_DYNACONF") | |
if not secrets_file: | |
raise "Error: Not found 'secrets.yaml' file. Please, configure the env: SECRETS_FOR_DYNACONF" | |
with open(secrets_file, 'r') as stream: | |
data_loaded = yaml.safe_load(stream) | |
db_credentials = data_loaded[env]["databases"][DATABASE] | |
if db_credentials is None: | |
raise "Error: No database configuration found in secrets.yaml" | |
sqlalchemy_url = "{}://{}:{}@{}/{}".format( | |
db_credentials["driver"], | |
db_credentials["user"], | |
db_credentials["pass"], | |
db_credentials["host"], | |
db_credentials["name"] | |
) | |
return sqlalchemy_url | |
def run_migrations_offline(): | |
"""Run migrations in 'offline' mode. | |
This configures the context with just a URL | |
and not an Engine, though an Engine is acceptable | |
here as well. By skipping the Engine creation | |
we don't even need a DBAPI to be available. | |
Calls to context.execute() here emit the given string to the | |
script output. | |
""" | |
url = config.get_main_option("sqlalchemy.url") | |
context.configure( | |
url=url, target_metadata=target_metadata, literal_binds=True, compare_type=True | |
) | |
with context.begin_transaction(): | |
context.run_migrations() | |
def run_migrations_online(): | |
"""Run migrations in 'online' mode. | |
In this scenario we need to create an Engine | |
and associate a connection with the context. | |
""" | |
# change sqlalchemy.url | |
db_config = config.get_section(config.config_ini_section) | |
db_config["sqlalchemy.url"] = get_db_url() | |
connectable = engine_from_config( | |
db_config, | |
prefix="sqlalchemy.", | |
poolclass=pool.NullPool, | |
connect_args={'ssl':'true'} | |
) | |
with connectable.connect() as connection: | |
context.configure( | |
connection=connection, target_metadata=target_metadata, compare_type=True | |
) | |
with context.begin_transaction(): | |
context.run_migrations() | |
if context.is_offline_mode(): | |
run_migrations_offline() | |
else: | |
run_migrations_online() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment