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

后端 未结 10 1734
[愿得一人]
[愿得一人] 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:13

    I had the very same problem yesterday and found a following solution to work. I do the following in alembic/env.py:

    # this is the Alembic Config object, which provides
    # access to the values within the .ini file in use.
    config = context.config
    
    # this will overwrite the ini-file sqlalchemy.url path
    # with the path given in the config of the main code
    import config as ems_config
    config.set_main_option('sqlalchemy.url', ems_config.config.get('sql', 'database'))
    

    ems_config is an external module that holds my configuration data.

    config.set_main_option(...) essentially overwrites the sqlalchemy.url key in the [alembic] section of the alembic.ini file. In my configuration I simply leave it black.

提交回复
热议问题