ARTICLE DETAIL

资讯详情

深耕编程入门与网站建设的一线实战洞察。

Apache Airflow 元数据库连接串升级:`postgresql://` 隐式驱动切换与 `postgresql+psycopg2://` 显式方言迁移指南

Apache Airflow 元数据库连接串升级:`postgresql://` 隐式驱动切换与 `postgresql+psycopg2://` 显式方言迁移指南 Apache Airflow 元数据库连接串升级postgresql://隐式驱动切换与postgresqlpsycopg2://显式方言迁移指南【免费下载链接】airflowApache Airflow - A platform to programmatically author, schedule, and monitor workflows项目地址: https://gitcode.com/GitHub_Trending/ai/airflowAirflow 官方默认 PostgreSQL 连接串已从裸postgresql://协议切换为显式的postgresqlpsycopg2://SQLAlchemy 方言。本文以 airflow-core/newsfragments/68314.improvement.rst 为骨架结合 airflow-core/src/airflow/configuration.py 与 airflow-core/tests/unit/core/test_configuration.py 的源码实现完整说明迁移原因、自动升级机制、手动迁移步骤、异步连接与结果后端Celeryresult_backend的处理方式以及可验证的测试证据帮助你在升级到 SQLAlchemy 2.1 前安全完成连接串改造。一、背景为什么裸postgresql://不再安全在 SQLAlchemy 中连接 URL 的 scheme 决定方言与驱动。历史上postgresql://隐含使用 psycopg2 驱动但 SQLAlchemy 2.1 改变了这一隐式默认将裸postgresql://的默认驱动从 psycopg2 切换为 psycopg (v3)。这意味着如果sql_alchemy_conn或 Celeryresult_backend中仍然写裸postgresql://升级 SQLAlchemy 后会在不修改任何代码的情况下静默更换数据库驱动。驱动切换看似无缝但 psycopg2 与 psycopg v3 在连接参数、批量执行executemany行为、事务语义、SSL 选项等方面存在差异静默替换可能在运行时产生难以排查的异常。Airflow 通过新闻片段newsfragment68314.improvement.rst 记录了这一变更并给出了明确的迁移指引将postgresql://显式改为postgresqlpsycopg2://继续使用 psycopg2或postgresqlpsycopg://有意迁移到 psycopg v3。二、变更要点速览连接串 scheme含义建议状态postgresql://裸协议SQLAlchemy 2.1 起默认驱动变为 psycopg (v3)不推荐需显式指定驱动postgresqlpsycopg2://显式使用 psycopg2 驱动与旧行为一致推荐沿用 psycopg2 时postgresqlpsycopg://显式使用 psycopg (v3) 驱动推荐有意迁移到 psycopg v3 时postgres://历史写法旧式别名启动时自动升级见下文三、Airflow 的自动升级机制_upgrade_postgres_metastore_connAirflow 并没有简单粗暴地拒绝旧写法而是在启动阶段提供了一次性自动升级。3.1 校验器注册在 airflow-core/src/airflow/configuration.py 中_upgrade_postgres_metastore_conn被注册进_validators列表与_validate_sqlite3_version、_validate_enums等校验器一起在配置解析阶段被调用property def _validators(self) - list[Callable[[], None]]: Overring _validators from shared base class to add core-specific validators. return [ self._validate_sqlite3_version, self._validate_enums, self._validate_deprecated_values, self._upgrade_postgres_metastore_conn, ]3.2 升级逻辑实现核心实现位于 airflow-core/src/airflow/configuration.pydef _upgrade_postgres_metastore_conn(self): Upgrade SQL schemas. As of SQLAlchemy 1.4, schemes postgrespsycopg2 and postgres must be replaced with postgresqlpsycopg if the psycopg (v3) driver is installed, or postgresqlpsycopg2 otherwise. The bare postgresql scheme is upgraded the same way to make the driver explicit. section, key database, sql_alchemy_conn old_value self.get(section, key, _extra_stacklevel1) bad_schemes [postgrespsycopg2, postgres, postgresql] ... good_scheme postgresqlpsycopg if find_spec(psycopg) is not None else postgresqlpsycopg2 parsed urlsplit(old_value) if parsed.scheme in bad_schemes: warnings.warn( fBad scheme in Airflow configuration [database] sql_alchemy_conn: {parsed.scheme}. As of SQLAlchemy 1.4 (adopted in Airflow 2.3) this is no longer supported. You must fchange to {good_scheme} before the next Airflow release., FutureWarning, stacklevel1, ) self.upgraded_values[(section, key)] old_value new_value re.sub(^ re.escape(f{parsed.scheme}://), f{good_scheme}://, old_value) self._update_env_var(sectionsection, namekey, new_valuenew_value) # if the old value is set via env var, we need to wipe it # otherwise, itll win over our adjusted value old_env_var self._env_var_name(core, key) os.environ.pop(old_env_var, None)从源码可以提炼出几个关键行为识别范围bad_schemes包含postgrespsycopg2、postgres、postgresql三种旧写法即历史遗留的postgres://、postgresql://以及不规范的postgrespsycopg2://都会被升级。目标方言动态选择通过find_spec(psycopg)检测当前环境是否安装了 psycopg v3 包——已安装则升级为postgresqlpsycopg://未安装则升级为postgresqlpsycopg2://。这正是Airflow will continue to auto-upgrade legacypostgres://schemes on startup的具体实现。发出警告升级发生时抛出FutureWarning提示用户手动修改配置因为自动升级仅存在于当前启动流程不应长期依赖。环境变量兜底如果旧值来自AIRFLOW__DATABASE__SQL_ALCHEMY_CONN环境变量升级时会将该环境变量从os.environ中清除防止其在后续配置读取中压过调整后的值。3.3 测试验证双路径行为airflow-core/tests/unit/core/test_configuration.py 用参数化测试完整覆盖了该升级逻辑的两条路径psycopg v3 已安装find_spec返回非 None时postgres://user:passhost/db→postgresqlpsycopg://user:passhost/db、postgresql://user:passhost/db→postgresqlpsycopg://user:passhost/db、postgresqlpsycopg2://...保持不变nooppsycopg v3 未安装find_spec返回 None时postgres://user:passhost/db→postgresqlpsycopg2://user:passhost/db且postgresqlpsycopg2://...原样保留非 PostgreSQL 的mysql://等 scheme 完全不受影响。这说明无论你最终使用哪个驱动只要连接串以postgresqlpsycopg2://或postgresqlpsycopg://显式声明自动升级就是 no-op无操作。四、迁移操作指南4.1 元数据库sql_alchemy_connsql_alchemy_conn位于 airflow-core/src/airflow/config_templates/config.yml是 Airflow 元数据库metadata database的 SQLAlchemy 连接串默认值为sqlite:///{AIRFLOW_HOME}/airflow.db。使用 PostgreSQL 作为元数据库时请检查你的airflow.cfg[database] # 旧写法不推荐SQLAlchemy 2.1 起会静默切换驱动 sql_alchemy_conn postgresql://airflow:airflowlocalhost:5432/airflow # 新写法 1继续使用 psycopg2与历史行为完全一致 sql_alchemy_conn postgresqlpsycopg2://airflow:airflowlocalhost:5432/airflow # 新写法 2有意迁移到 psycopg v3 sql_alchemy_conn postgresqlpsycopg://airflow:airflowlocalhost:5432/airflow如果通过环境变量配置等价写法为export AIRFLOW__DATABASE__SQL_ALCHEMY_CONNpostgresqlpsycopg2://airflow:airflowlocalhost:5432/airflow4.2 Celery 结果后端result_backendCelery Executor 的result_backend同样可能使用 PostgreSQL若你的配置形如[celery] result_backend postgresql://airflow:airflowlocalhost:5432/airflow_results请同样改为显式方言[celery] result_backend postgresqlpsycopg2://airflow:airflowlocalhost:5432/airflow_results注意新闻片段与_upgrade_postgres_metastore_conn的自动升级只覆盖[database] sql_alchemy_connCeleryresult_backend中的旧写法不会被 Airflow 自动改写需要手动迁移否则在 SQLAlchemy 2.1 下同样会遭遇静默驱动切换。4.3 验证迁移结果迁移后可通过以下方式确认连接串已被正确解析# 输出当前生效的连接串密码会被掩码 airflow config get-value database sql_alchemy_conn # 或直接查看 airflow info从源码侧airflow config get-value走的是 airflow-core/src/airflow/settings.py 中SQL_ALCHEMY_CONN.startswith(postgresqlpsycopg2)的判断逻辑该开关还用于决定是否启用 psycopg2 专属的批量执行调优参数executemany_mode: values_plus_batch、executemany_batch_page_size: 2000见 airflow-core/src/airflow/settings.py。也就是说显式写成postgresqlpsycopg2://不仅避免驱动切换还能让 Airflow 正确识别驱动并应用对应的引擎调优参数。五、异步连接与相关配置联动Airflow 3.x 新增了异步元数据库连接串 airflow-core/src/airflow/config_templates/config.yml[database] sql_alchemy_conn_async postgresqlasyncpg://postgres:airflowpostgres/airflow说明如下sql_alchemy_conn_async默认不设置default: ~未设置时 Airflow 会自动从sql_alchemy_conn推导出异步连接串由于同步/异步驱动之间的转换逻辑并非总能正确工作官方建议在存在兼容性问题时直接显式设置该值version_added: 3.1.0该配置同样属于sensitive: trueairflow config get-value输出时会被掩码处理对应 airflow-core/tests/unit/cli/commands/test_info_command.py 中postgresqlpsycopg2://p...s:PASSWORD...的掩码行为。六、常见问题FAQQ1升级后我的postgresql://连接串会被自动改掉吗启动时会被自动升级仅sql_alchemy_conn同时抛出FutureWarning提醒你手动修改。自动升级是临时兜底手段请尽快在airflow.cfg或环境变量中改为显式方言。Q2应该选postgresqlpsycopg2还是postgresqlpsycopg取决于你的依赖安装情况。若继续使用 psycopg2pip install apache-airflow[postgres]中仍含 psycopg2 生态写postgresqlpsycopg2://若已切换到 psycopg v3写postgresqlpsycopg://。Airflow 的自动升级逻辑正是按find_spec(psycopg)的结果在两者之间选择的跟随环境即可保持一致。Q3postgres://旧写法还能用吗能用但每次启动都会触发FutureWarning并自动升级。它等价于postgresql://属于历史遗留别名建议一并改为显式方言。Q4切换驱动会影响现有数据吗连接串只是连接协议声明不改变元数据库中的数据。但驱动实现差异如 psycopg v3 的批次插入行为可能影响部分操作的执行路径因此强烈建议在测试环境先验证再升级生产。七、总结本次变更是 SQLAlchemy 2.1 上游行为变更在 Airflow 侧的显式化裸postgresql://的隐式驱动从 psycopg2 变为 psycopg (v3)Airflow 通过_upgrade_postgres_metastore_conn在启动阶段自动升级sql_alchemy_conn中的旧 scheme并发出FutureWarning引导用户显式声明驱动方言。对部署者的行动清单检查sql_alchemy_conn改为postgresqlpsycopg2://或postgresqlpsycopg://检查 Celeryresult_backend同上手动修改不受自动升级覆盖如需异步连接确认或显式设置sql_alchemy_conn_async参考测试用例 airflow-core/tests/unit/core/test_configuration.py 验证迁移逻辑确保驱动与连接串一致后再上线。【免费下载链接】airflowApache Airflow - A platform to programmatically author, schedule, and monitor workflows项目地址: https://gitcode.com/GitHub_Trending/ai/airflow创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表