.ini文件加载环境变量
.ini file load environment variable
我正在使用 Alembic 在 Flask
项目中实施迁移。有一个 alembic.ini
文件,其中必须指定数据库配置:
sqlalchemy.url = driver://user:password@host/dbname
有没有办法从环境变量中指定参数?我尝试以这种方式加载它们 $(env_var)
但没有成功。谢谢!
我已经按照@dirn 的建议在env.py
中设置sqlalchemy.url
解决了这个问题。
config.set_main_option('sqlalchemy.url', <db_uri>)
成功了,其中 <db_uri>
可以从环境或配置文件中加载。
我一直在寻找如何为多数据库管理它
这是我所做的。我有两个数据库:logs 和 ohlc
根据doc,
我已经像那样设置了蒸馏器
alembic init --template multidb
alembic.ini
databases = logs, ohlc
[logs]
sqlalchemy.url = postgresql://botcrypto:botcrypto@localhost/logs
[ohlc]
sqlalchemy.url = postgresql://botcrypto:botcrypto@localhost/ohlc
env.py
[...]
# 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)
logger = logging.getLogger('alembic.env')
# overwrite alembic.ini db urls from the config file
settings_path = os.environ.get('SETTINGS')
if settings_path:
with open(settings_path) as fd:
settings = conf.load(fd, context=os.environ) # loads the config.yml
config.set_section_option("ohlc", "sqlalchemy.url", settings["databases"]["ohlc"])
config.set_section_option("logs", "sqlalchemy.url", settings["databases"]["logs"])
else:
logger.warning('Environment variable SETTINGS missing - use default alembic.ini configuration')
[...]
config.yml
databases:
logs: postgresql://botcrypto:botcrypto@127.0.0.1:5432/logs
ohlc: postgresql://botcrypto:botcrypto@127.0.0.1:5432/ohlc
用法
SETTINGS=config.yml alembic upgrade head
希望对您有所帮助!
我正在使用 Alembic 在 Flask
项目中实施迁移。有一个 alembic.ini
文件,其中必须指定数据库配置:
sqlalchemy.url = driver://user:password@host/dbname
有没有办法从环境变量中指定参数?我尝试以这种方式加载它们 $(env_var)
但没有成功。谢谢!
我已经按照@dirn 的建议在env.py
中设置sqlalchemy.url
解决了这个问题。
config.set_main_option('sqlalchemy.url', <db_uri>)
成功了,其中 <db_uri>
可以从环境或配置文件中加载。
我一直在寻找如何为多数据库管理它
这是我所做的。我有两个数据库:logs 和 ohlc
根据doc, 我已经像那样设置了蒸馏器
alembic init --template multidb
alembic.ini
databases = logs, ohlc
[logs]
sqlalchemy.url = postgresql://botcrypto:botcrypto@localhost/logs
[ohlc]
sqlalchemy.url = postgresql://botcrypto:botcrypto@localhost/ohlc
env.py
[...]
# 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)
logger = logging.getLogger('alembic.env')
# overwrite alembic.ini db urls from the config file
settings_path = os.environ.get('SETTINGS')
if settings_path:
with open(settings_path) as fd:
settings = conf.load(fd, context=os.environ) # loads the config.yml
config.set_section_option("ohlc", "sqlalchemy.url", settings["databases"]["ohlc"])
config.set_section_option("logs", "sqlalchemy.url", settings["databases"]["logs"])
else:
logger.warning('Environment variable SETTINGS missing - use default alembic.ini configuration')
[...]
config.yml
databases:
logs: postgresql://botcrypto:botcrypto@127.0.0.1:5432/logs
ohlc: postgresql://botcrypto:botcrypto@127.0.0.1:5432/ohlc
用法
SETTINGS=config.yml alembic upgrade head
希望对您有所帮助!