Emit DDL event for ALTER TABLE ADD CONSTRAINT with non-unique constraint - #5603
Emit DDL event for ALTER TABLE ADD CONSTRAINT with non-unique constraint#5603fudianchn wants to merge 1 commit into
Conversation
|
|
wenshao
left a comment
There was a problem hiding this comment.
Reviewed — no blockers. Suggestions are inline.
中文说明
已审查——无阻断问题。 建议见行内评论。
— qwen3.8-max-preview via Qwen Code /review
| public void testAlterAddConstraintForeignKey() { | ||
| String queryString = "alter table retl_mark add constraint fk1 foreign key (a) references p(id)"; | ||
| List<DdlResult> results = DruidDdlParser.parse(queryString, "retl"); |
There was a problem hiding this comment.
[Suggestion] The PR description states it fixes non-UNIQUE constraints (foreign key AND CHECK), but the test only covers FOREIGN KEY. If a future refactor replaces the catch-all else with an explicit instanceof SQLForeignKeyImpl check, ALTER TABLE t ADD CONSTRAINT ck1 CHECK (col > 0) would be silently dropped again — the exact bug this PR fixes — and no test would fail. Consider adding a companion test:
@Test
public void testAlterAddConstraintCheck() {
String queryString = "alter table retl_mark add constraint ck1 check (a > 0)";
List<DdlResult> results = DruidDdlParser.parse(queryString, "retl");
Assert.assertFalse("ADD CONSTRAINT CHECK should emit a DDL event", results.isEmpty());
DdlResult result = results.get(0);
Assert.assertEquals("retl", result.getSchemaName());
Assert.assertEquals("retl_mark", result.getTableName());
Assert.assertEquals(EventType.ALTER, result.getType());
}中文说明
PR 描述中说明修复了非 UNIQUE 约束(外键和 CHECK),但测试仅覆盖了 FOREIGN KEY。如果将来有人将 catch-all else 重构为显式的 instanceof SQLForeignKeyImpl 判断,ALTER TABLE t ADD CONSTRAINT ck1 CHECK (col > 0) 将再次被静默丢弃——即本 PR 修复的 bug——且不会有测试失败。建议补充 CHECK 约束的测试用例。
— qwen3.8-max-preview via Qwen Code /review
DruidDdlParser only enqueued a DdlResult for SQLAlterTableAddConstraint when the constraint was a SQLUnique; for other constraints (FOREIGN KEY, CHECK, ...) the DdlResult was built and processName'd but never added, so ALTER TABLE t ADD CONSTRAINT fk1 FOREIGN KEY (...) emitted no DDL event. Set EventType.ALTER for the non-unique case and always add the result, mirroring the existing catch-all branch.
d02dbaa to
28fef88
Compare
|
Added a CHECK-constraint companion test |
AI 披露:本改动由 AI 编码代理辅助完成,我已逐行审改。
问题(What)
ALTER TABLE ... ADD CONSTRAINT添加非 UNIQUE 约束(外键 / CHECK 等)时,canal 不产生任何 DDL 事件,下游感知不到这次表结构变更。根因(Root cause)
DruidDdlParser对SQLAlterTableAddConstraint分支只在约束为SQLUnique时才把DdlResult加入结果;其余情况(外键/CHECK 等)DdlResult已构造、processName已调用,却被丢弃——既没add,也没设EventType。修复(Fix)
非 UNIQUE 约束按
EventType.ALTER处理并加入结果(与既有 catch-all 分支一致),UNIQUE 仍为CINDEX。仅给该分支补else并把add移出if。测试(Testing)
新增两个用例覆盖
SQLAlterTableAddConstraint的两条路径:testAlterAddConstraintForeignKey:... add constraint fk1 foreign key (a) references p(id),断言产生 1 条 DDL 事件、表名retl_mark、EventType.ALTER。testAlterAddConstraintCheck:... add constraint ck1 check (a > 0),断言同上。CHECK 走 catch-allelse分支,防止将来有人把else收窄成instanceof SQLForeignKeyImpl后 CHECK 再次被静默丢弃。修复前两个用例均失败(结果列表为空,
assertFalse报错);修复后通过。DruidDdlParserTest全部 9 个用例通过。复现(Reproduce)