Is it possible to store the alembic connect string outside of alembic.ini?

后端 未结 10 1607
[愿得一人]
[愿得一人] 2021-01-30 15:41

I\'m using Alembic with SQL Alchemy. With SQL Alchemy, I tend to follow a pattern where I don\'t store the connect string with the versioned code. Instead I have file secr

10条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 16:38

    The simplest thing I could come up with to avoid commiting my user/pass was to a) add in interpolation strings to the alembic.ini file, and b) set these interpolation values in env.py

    alembic.ini

    sqlalchemy.url = postgresql://%(DB_USER)s:%(DB_PASS)s@35.197.196.146/nozzle-website
    

    env.py

    import os
    
    from logging.config import fileConfig
    
    from sqlalchemy import engine_from_config
    from sqlalchemy import pool
    
    from alembic import context
    
    # this is the Alembic Config object, which provides
    # access to the values within the .ini file in use.
    config = context.config
    
    # here we allow ourselves to pass interpolation vars to alembic.ini
    # fron the host env
    section = config.config_ini_section
    config.set_section_option(section, "DB_USER", os.environ.get("DB_USER"))
    config.set_section_option(section, "DB_PASS", os.environ.get("DB_PASS"))
    
    ...
    

提交回复
热议问题