Skip to content

[Bug]: DATA BRANCH CREATE DATABASE rejects valid cyclic foreign-key schemas #26111

Closed

Description

@Ariznawlll

Environment

  • Branch: main
  • Commit: 2eb7f523e5a10f352d3e258183ed8440510388de
  • Deployment: local single-node MatrixOne

Problem

DATA BRANCH CREATE DATABASE rejects a valid database whose foreign-key graph contains a cycle. The source schema and data are valid and usable, but branch creation stops with:

ERROR 20101 (HY000): internal error: There is a cycle in dependency graph

The target database is not created.

Reproduction

DROP DATABASE IF EXISTS g4d_fk_cycle_src;
DROP DATABASE IF EXISTS g4d_fk_cycle_dst;

CREATE DATABASE g4d_fk_cycle_src;

CREATE TABLE g4d_fk_cycle_src.a(
  id INT PRIMARY KEY,
  b_id INT
);

CREATE TABLE g4d_fk_cycle_src.b(
  id INT PRIMARY KEY,
  a_id INT,
  CONSTRAINT fk_b_a
    FOREIGN KEY(a_id) REFERENCES g4d_fk_cycle_src.a(id)
);

ALTER TABLE g4d_fk_cycle_src.a
  ADD CONSTRAINT fk_a_b
  FOREIGN KEY(b_id) REFERENCES g4d_fk_cycle_src.b(id);

INSERT INTO g4d_fk_cycle_src.a VALUES (1, NULL);
INSERT INTO g4d_fk_cycle_src.b VALUES (1, 1);
UPDATE g4d_fk_cycle_src.a SET b_id = 1 WHERE id = 1;

SELECT * FROM g4d_fk_cycle_src.a;
SELECT * FROM g4d_fk_cycle_src.b;

DATA BRANCH CREATE DATABASE g4d_fk_cycle_dst
  FROM g4d_fk_cycle_src;

Actual result

The source tables contain valid mutually referencing rows, but database branch creation fails:

ERROR 20101 (HY000): internal error: There is a cycle in dependency graph

After the failure:

SELECT COUNT(*)
FROM mo_catalog.mo_database
WHERE datname = 'g4d_fk_cycle_dst';
-- 0

The failure is atomic, but the valid database cannot be branched.

Expected result

Database branch creation should support a valid cyclic foreign-key graph, for example by separating table creation from foreign-key restoration, or otherwise restoring the constraints after all referenced tables exist.

Reproducibility

Reproduced 3/3 times with separate source and destination database names.

Code observation

collectCloneDatabaseSource calls fkTablesTopoSort. The current topological sorter returns an error whenever the foreign-key graph contains a cycle, so DATA BRANCH CREATE DATABASE exits before cloning any object.

Activity

  1. added this to the MO-202607 milestone on Jul 23, 2026
  2. added
    kind/bug
    on Jul 23, 2026
  3. added theissue type on Jul 23, 2026
  4. VioletQwQ-0 commented on Jul 30, 2026

    @VioletQwQ-0
    Collaborator

    QA handoff

    Merged fix: #26292, merge commit 52ea9f874f492c368a20efef16b46ab8ced2de37 (final PR head 622e09571687fb7d06f59f9acad1d3db4f7140af).
    Current main at handoff: 41332062a6740978f269088a92ee362a888c7bd7.

    QA Decision

    • required: yes
    • reason: the change affects Data Branch creation, persisted cyclic-FK metadata, cross-account behavior, and session-state restoration
    • production_entrypoint: DATA BRANCH CREATE DATABASE from live and named-snapshot sources containing a valid mutual-FK cycle
    • automated_terminal_coverage: the merged embedded regression covers live/snapshot and same-account/cross-account cases, destination-local FK IDs and enforcement, repeated-target failure behavior, and restoration of foreign_key_checks
    • remaining_validation: execute those paths in a deployed environment, including a failed/repeated branch attempt followed by ordinary FK-protected DML in the same session

    PASS requires successful branch creation and copied rows, destination-local FK metadata, invalid writes rejected, and foreign_key_checks restored after both success and failure without altering a pre-existing target. FAIL includes clone rejection, source-object IDs in target metadata, disabled constraints, session-state leakage, or target mutation after a failed statement.

    Please record the tested build/commit, environment, source mode, account context, and terminal results. Keep this issue open until explicit QA PASS evidence is posted.

  5. Ariznawlll commented on Aug 1, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:15a2d07c687a62a0984cf62846b092912a28c0f1
    • 部署方式:本地双 CN + Proxy。
    • 测试日期:2026-08-01

    原问题验证

    构造 A→B、B→A 的循环 FK,写入闭环数据后分别从实时库和命名 snapshot 创建数据库 Data Branch。连续 3 轮均成功;两个目标库连接查询均得到 1 行,且 target-local FK 元数据各为 2 条。

    Happy Path 覆盖

    1. 覆盖实时源和 snapshot='<name>' 两种建库路径。
    2. 验证循环引用数据、目标库内 FK 指向和 foreign_key_checks 会话值均保持正确。

    Unhappy Path 覆盖

    1. 先创建同名目标库后重复建 branch,语句被拒绝;失败前后的 foreign_key_checks 完全一致。
    2. 在两个成功目标库中向 A、B 分别插入悬空引用,均被拒绝;无部分行写入。

    白盒验证

    • 不适用:循环依赖处理和失败原子性均可通过 SQL 与 catalog 观察。

    回归测试

    • 本轮未新增 motr/BVT 文件;涉及 snapshot 的后续回归需按共享 catalog 状态评估调度。

    测试证据

    • 执行:Data Branch/CLONE 黑盒审计连续 3 轮。
    • 每轮均记录:PASS 26111_cyclic_foreign_keys_live_snapshot_failure_atomicity。

    测试结论

    测试通过。

  6. Ariznawlll commented on Aug 1, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:15a2d07c687a62a0984cf62846b092912a28c0f1
    • 部署方式:本地单 CN MatrixOne,motr 通过 MySQL 协议执行 SQL 黑盒用例
    • 测试日期:2026-08-01

    原问题验证

    构造相互引用的合法循环外键 schema:表 a 通过 b_id 引用 b,表 b 通过 a_id 引用 a。写入相互关联的合法行后执行 DATA BRANCH CREATE DATABASE motr_cycle_dst FROM motr_cycle_src,验证目标库中的环形外键和数据。

    本次新增黑盒回归连续执行 3 轮,每轮 47 条语句/断言,3/3 均成功;与 #26113、#26070 共用的回归用例总计 141 条语句/断言成功。

    Happy Path 覆盖

    1. Data Branch 从含循环外键的源库创建目标库成功,不再因表间引用循环而拒绝 DDL。
    2. 在目标库 join a、b,得到 (a.id=1, a.b_id=1, b.a_id=1),确认两张表的数据和相互引用均被正确保留。
    3. 源表包含主键和后续 ALTER TABLE ... ADD CONSTRAINT 的约束建立方式,覆盖非一次性建表的正常 schema。

    Unhappy Path 覆盖

    1. 向目标库 b 插入指向不存在 a.id=999 的行,稳定返回外键错误 20101/1452。
    2. 错误后检查 b 的记录数保持为 1,非法写入没有部分落库,验证复制后的环形外键仍有效且拒绝原子。

    白盒验证

    未增加独立故障注入。循环依赖解析与约束恢复都可通过公开的 DATA BRANCH CREATE DATABASE、查询和非法写入验证,因此以端到端黑盒契约为准。

    回归测试

    已加入 motr 黑盒回归:PR #74,测试文件:issue_26113_26111_26070_branch_integrity.test。

    测试证据

    执行命令:./motr --config /tmp/motr-git4data-fulltext-blackbox-config.yml --repeat 3 --do-test 'issue_(26144|26113)' -v。其中本用例 3 轮均为 success,共 141/141 条语句/断言成功;对应回归提交:8f0f759。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugphase/testing

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions