删除 storage_objects.parent_object_id 死列和孤儿索引 idx_storage_parent #15

Closed
opened 2026-08-21 09:56:01 +08:00 by tao.chen · 1 comment
Owner

背景

storage_objects.parent_object_id 列和 idx_storage_parent 索引是死代码:

  • 全仓 grep parent_object_id 只有 4 处出现:migrations/.../e1f2a3b4c5d6_rebuild_baseline.py:337, 357common/db/models/storage.py:23, 89
  • 没有任何业务代码写过、读过、查过这个列
  • 树结构完全由 relative_path materialized path 维护(backend/src/backend/scripts.py:list_workspace_tree, list_workspace_directories)

保留这个列会让后来人基于它写错误查询(就像本次会话已发生的:用户曾提议建邻接表方案,正是基于这个误导性列)。

改动

1. 写迁移 —— migrations/versions/

新迁移文件,根据下一个 revision ID 递增:

def upgrade() -> None:
    # MySQL 8.0 不支持 DROP INDEX IF EXISTS / DROP COLUMN IF EXISTS
    op.drop_index("idx_storage_parent", table_name="storage_objects")
    op.drop_column("storage_objects", "parent_object_id")

def downgrade() -> None:
    op.add_column(
        "storage_objects",
        sa.Column("parent_object_id", mysql.CHAR(length=26), nullable=True),
    )
    op.create_index(
        "idx_storage_parent", "storage_objects", ["parent_object_id"], unique=False
    )

注意 MySQL 8.0 限制: 不要用 IF EXISTS / IF NOT EXISTS 标志,虽然 Alembic 暴露这些参数,但 MySQL 不支持(Alembic 会发出原生 SQL 报错)。

2. 改模型注释 —— common/src/common/db/models/storage.py

StorageObjects 类的 __table_args__ 注释里把"邻接表"幻觉掐死:

{"comment": "Workspace 文件和 RustFS 对象的统一元数据;tree 走 materialized path (relative_path),不要 join parent_object_id —— 该列已删除。"},

3. 验证

uv run --package backend alembic upgrade head
uv run --package backend pytest backend/tests -q
  • 测试里如果有 mock StorageObjects(parent_object_id=...) 的,需要清理
  • 跑一遍 list_workspace_tree / list_workspace_directories / list_scripts 三个端点的 smoke test,确认没回归

不做什么

  • 不动 relative_path —— materialized path 是唯一真相
  • 不重构 list_workspace_directories 等查询
  • 不删 path_hash / idx_storage_workspace_path —— 这两个被 LIKE prefix 查询服务,真在用

关联

  • 来自任务 #33 (listScripts parent_path 过滤) 的同会话 review
  • Linus 判决:A 路线胜出,parent_object_id 是 B 路线残留物,该清理

Task: gpm8lz9z9h59siarlapfekmf

## 背景 `storage_objects.parent_object_id` 列和 `idx_storage_parent` 索引是死代码: - 全仓 `grep parent_object_id` 只有 4 处出现:`migrations/.../e1f2a3b4c5d6_rebuild_baseline.py:337, 357` 和 `common/db/models/storage.py:23, 89` - **没有任何业务代码写过、读过、查过这个列** - 树结构完全由 `relative_path` materialized path 维护(`backend/src/backend/scripts.py:list_workspace_tree`, `list_workspace_directories`) 保留这个列会让后来人基于它写错误查询(就像本次会话已发生的:用户曾提议建邻接表方案,正是基于这个误导性列)。 ## 改动 ### 1. 写迁移 —— `migrations/versions/` 新迁移文件,根据下一个 revision ID 递增: ```python def upgrade() -> None: # MySQL 8.0 不支持 DROP INDEX IF EXISTS / DROP COLUMN IF EXISTS op.drop_index("idx_storage_parent", table_name="storage_objects") op.drop_column("storage_objects", "parent_object_id") def downgrade() -> None: op.add_column( "storage_objects", sa.Column("parent_object_id", mysql.CHAR(length=26), nullable=True), ) op.create_index( "idx_storage_parent", "storage_objects", ["parent_object_id"], unique=False ) ``` **注意 MySQL 8.0 限制**: 不要用 `IF EXISTS` / `IF NOT EXISTS` 标志,虽然 Alembic 暴露这些参数,但 MySQL 不支持(Alembic 会发出原生 SQL 报错)。 ### 2. 改模型注释 —— `common/src/common/db/models/storage.py` 在 `StorageObjects` 类的 `__table_args__` 注释里把"邻接表"幻觉掐死: ```python {"comment": "Workspace 文件和 RustFS 对象的统一元数据;tree 走 materialized path (relative_path),不要 join parent_object_id —— 该列已删除。"}, ``` ### 3. 验证 ```bash uv run --package backend alembic upgrade head uv run --package backend pytest backend/tests -q ``` - 测试里如果有 mock `StorageObjects(parent_object_id=...)` 的,需要清理 - 跑一遍 `list_workspace_tree` / `list_workspace_directories` / `list_scripts` 三个端点的 smoke test,确认没回归 ## 不做什么 - 不动 `relative_path` —— materialized path 是唯一真相 - 不重构 list_workspace_directories 等查询 - 不删 `path_hash` / `idx_storage_workspace_path` —— 这两个被 LIKE prefix 查询服务,真在用 ## 关联 - 来自任务 #33 (listScripts parent_path 过滤) 的同会话 review - Linus 判决:A 路线胜出,parent_object_id 是 B 路线残留物,该清理 --- <sub>Task: gpm8lz9z9h59siarlapfekmf</sub>
tao.chen added the priority:lowstatus:to-do labels 2026-08-21 09:56:03 +08:00
tao.chen added status:in-progress and removed status:to-do labels 2026-08-21 10:00:12 +08:00
tao.chen added status:done and removed status:in-progress labels 2026-08-21 10:01:59 +08:00
Author
Owner

tao.chen commented:

Done — commit 0ea6456 on develop.

Applied migration f7a8b9c0d1e2. DB at head. TABLE COMMENT updated. 43/43 tests pass.

Codex review (deepseek-v4-flash, danger-full-access, on-request): 1 low finding fixed (model comment now reaches DB via ALTER TABLE ... COMMENT), 1 informational note left (downgrade column-order, no functional impact).

Next: #34 (listScripts parent_path filter).

**tao.chen** commented: > Done — commit `0ea6456` on `develop`. Applied migration `f7a8b9c0d1e2`. DB at head. TABLE COMMENT updated. 43/43 tests pass. Codex review (deepseek-v4-flash, danger-full-access, on-request): 1 low finding fixed (model comment now reaches DB via `ALTER TABLE ... COMMENT`), 1 informational note left (downgrade column-order, no functional impact). Next: #34 (listScripts parent_path filter).
tao.chen added status:archived and removed status:done labels 2026-08-24 10:09:10 +08:00
tao.chen reopened this issue 2026-08-24 10:09:16 +08:00
tao.chen added the status:in-review label 2026-08-24 14:21:15 +08:00
tao.chen added status:archived and removed status:in-review labels 2026-08-24 14:25:21 +08:00
tao.chen reopened this issue 2026-08-24 14:25:33 +08:00
tao.chen added the status:to-do label 2026-08-24 14:25:44 +08:00
tao.chen added status:done and removed status:to-do labels 2026-08-24 14:50:46 +08:00
tao.chen added status:in-review and removed status:done labels 2026-08-24 14:51:31 +08:00
tao.chen reopened this issue 2026-08-24 14:51:37 +08:00
tao.chen added status:done and removed status:in-review labels 2026-08-24 14:53:12 +08:00
Sign in to join this conversation.