gpt4 book ai didi

python - 是否可以将 alembic 连接字符串存储在 alembic.ini 之外?

转载 作者:IT老高 更新时间:2023-10-28 20:34:17 38 4
gpt4 key购买 nike

我将 Alembic 与 SQLAlchemy 一起使用。使用 SQLAlchemy,我倾向于遵循不将连接字符串与版本化代码一起存储的模式。相反,我有包含任何 secret 信息的文件 secret.py。我将这个文件名放在我的 .gitignore 中,这样它就不会出现在 GitHub 上。

这种模式运行良好,但现在我开始使用 Alembic 进行迁移。看来我无法隐藏连接字符串。而在 alembic.ini 中,您将连接字符串放置为 configuration parameter :

# the 'revision' command, regardless of autogenerate
# revision_environment = false

sqlalchemy.url = driver://user:pass@localhost/dbname

# Logging configuration
[loggers]
keys = root,sqlalchemy,alembi

我担心我会不小心为我的数据库提交包含用户名/密码信息的文件。我宁愿将此连接字符串存储在一个地方,避免意外将其提交给版本控制的风险。

我有什么选择?

最佳答案

我昨天遇到了同样的问题,并找到了以下解决方案。我在 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 是一个保存我的配置数据的外部模块。

config.set_main_option(...) 实质上覆盖了 [alembic] 部分中的 sqlalchemy.url 键alembic.ini 文件。在我的配置中,我只是将其保留为黑色。

关于python - 是否可以将 alembic 连接字符串存储在 alembic.ini 之外?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22178339/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com