Repository navigation
fix(generator): 方言类型 import 修正 + jsonb 统一为 JSONType(关闭 #542 核心路径) - #547
Merged
Merged
Conversation
issue #542 的核心路径修复(此前只提供了基类 JSONType,生成器仍会产出非法 import): - GenConstant.DB_TO_SQLALCHEMY(PG 分支):jsonb → JSONType;bit / bit varying → BIT - template_safety 新增 DIALECT_TYPE_IMPORTS:JSONType → app.core.base_model; INET / CIDR / MACADDR / BIT / TSVECTOR / TSQUERY → sqlalchemy.dialects.postgresql; Geometry → geoalchemy2 - 生成器拼 import 时按该表取模块,杜绝 `from sqlalchemy import JSONB` 这类非法语句 (这些类型不在顶层 sqlalchemy 命名空间,实测 AttributeError;生成的模型在 import 阶段就失败) 回归测试(backend/tests/test_generator_template_safety.py,新增 5 个): - DIALECT_TYPE_IMPORTS 的目标模块/类名均可解析(geoalchemy2 为可选依赖,跳过) - PG 方言下 8 种方言类型的 import 均正确、且不出现顶层 import - PG 方言下 jsonb 列渲染 JSONType、bit 渲染 BIT - 端到端渲染出的模型可 ast.parse,且从基类导入 JSONType - JSONType 在 PG 渲染 JSONB、SQLite/MySQL 渲染 JSON 红绿验证:临时还原三处源码后 4 个新测试失败(报错正是 from sqlalchemy import JSONB); 修复后 5 passed;全量 102 passed;ruff All checks passed;basedpyright 0 errors。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
修复 issue #542 的核心故障路径(此前 #545 只提供了基类
JSONType,生成器仍会产出非法 import)。问题
GenConstant.DB_TO_SQLALCHEMY['jsonb'] = 'JSONB',生成器拼 import 时得到from sqlalchemy import JSONB—— JSONB 不在顶层 sqlalchemy 命名空间,实测AttributeError,生成的模型在 import 阶段即失败(比 issue 描述的 SQLite
CompileError更早)。INET / CIDR / MACADDR / Bit / TSVECTOR / TSQUERY(均非顶层导出);Geometry会先加geoalchemy2再被覆盖成非法的from sqlalchemy import Geometry。改动
jsonb → JSONType(跨方言)、bit / bit varying → BITDIALECT_TYPE_IMPORTS(template_safety.py):按类型名给出正确的 (模块, 类名)验证
ast.parse)from sqlalchemy import JSONB);修复后 5 passedpytest:102 passed;ruff通过;basedpyright0 errors