Dou*_* T. 52 python sqlalchemy alembic
我正在使用Alembic和SQL Alchemy.使用SQL Alchemy,我倾向于遵循一种模式,即我不将连接字符串与版本化代码一起存储.相反,我的文件secret.py包含任何机密信息.我把这个文件名丢给我,.gitignore所以它不会在GitHub上结束.
这种模式工作正常,但现在我开始使用Alembic进行迁移.看来我无法隐藏连接字符串.而是在alembic.ini中,将连接字符串作为配置参数放置:
# the 'revision' command, regardless of autogenerate
# revision_environment = false
sqlalchemy.url = driver://user:pass@localhost/dbname
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembi
Run Code Online (Sandbox Code Playgroud)
我担心我会不小心为我的数据库提交一个包含用户名/密码信息的文件.我宁愿将这个连接字符串存储在一个地方,并避免意外将其提交给版本控制的风险.
我有什么选择?
Tam*_*ren 57
昨天我遇到了同样的问题,并找到了以下解决方案.我做了以下几点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'))
Run Code Online (Sandbox Code Playgroud)
ems_config 是一个保存配置数据的外部模块.
config.set_main_option(...)基本上覆盖文件部分的sqlalchemy.url密钥.在我的配置中,我只是把它留黑.[alembic]alembic.ini
dan*_*nio 14
Alembic文档建议使用create_engine数据库URL(而不是在代码中修改sqlalchemy.url).
您还应修改run_migrations_offline以使用新URL.Allan Simon 在他的博客上有一个例子,但总的来说,将env.py修改为:
提供一个共享函数以某种方式获取URL(这里它来自命令行):
def get_url():
url = context.get_x_argument(as_dictionary=True).get('url')
assert url, "Database URL must be specified on command line with -x url=<DB_URL>"
return url
Run Code Online (Sandbox Code Playgroud)在离线模式下使用URL:
def run_migrations_offline():
...
url = get_url()
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True)
...
Run Code Online (Sandbox Code Playgroud)使用在线模式中的URL create_engine代替engine_from_config:
def run_migrations_online():
...
connectable = create_engine(get_url())
with connectable.connect() as connection:
...
Run Code Online (Sandbox Code Playgroud)所以看起来有效的是在env.py中重新实现引擎创建,这显然是进行这种定制的地方而不是在ini中使用sqlalchemy连接字符串:
engine = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)
Run Code Online (Sandbox Code Playgroud)
您可以替换并指定自己的引擎配置:
import store
engine = store.engine
Run Code Online (Sandbox Code Playgroud)
事实上,文档似乎暗示这是可以的:
sqlalchemy.url - 通过SQLAlchemy连接到数据库的URL.实际上,该密钥仅在env.py文件中引用,该文件特定于"通用"配置; 可由开发人员自定义的文件.多数据库配置可以在此响应多个键,或者可以引用该文件的其他部分.
为了避免提交用户/密码,我想出的最简单的方法是:a)在alembic.ini文件中添加插值字符串,然后b)在env.py
阿勒比比尼
sqlalchemy.url = postgresql://%(DB_USER)s:%(DB_PASS)s@35.197.196.146/nozzle-website
Run Code Online (Sandbox Code Playgroud)
信封
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"))
...
Run Code Online (Sandbox Code Playgroud)
我一直在寻找如何管理多数据库的方法
这就是我所做的。我有两个数据库:logs和ohlc
根据文档,我已经像这样设置了alembic
alembic init --template multidb
Run Code Online (Sandbox Code Playgroud)
databases = logs, ohlc
[logs]
sqlalchemy.url = postgresql://botcrypto:botcrypto@localhost/logs
[ohlc]
sqlalchemy.url = postgresql://botcrypto:botcrypto@localhost/ohlc
Run Code Online (Sandbox Code Playgroud)
[...]
# 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')
[...]
Run Code Online (Sandbox Code Playgroud)
databases:
logs: postgresql://botcrypto:botcrypto@127.0.0.1:5432/logs
ohlc: postgresql://botcrypto:botcrypto@127.0.0.1:5432/ohlc
Run Code Online (Sandbox Code Playgroud)
用法
SETTINGS=config.yml alembic upgrade head
Run Code Online (Sandbox Code Playgroud)
希望它能有所帮助!
小智 6
from alembic.config import Config
alembic_cfg = Config()
alembic_cfg.set_main_option("sqlalchemy.url", getenv('PG_URI'))
Run Code Online (Sandbox Code Playgroud)
https://alembic.sqlalchemy.org/en/latest/api/config.html
| 归档时间: |
|
| 查看次数: |
9538 次 |
| 最近记录: |