Skip to content

[ObjectQL] 未自动同步所有注册对象 schema 至数据库(缺失 syncSchema 调用) #941

Description

@hotlong

问题描述

ObjectQL 引擎当前的 init() 方法只负责调用所有数据驱动(driver)的 connect(),但不会自动同步步骤中注册到 SchemaRegistry 的对象定义(object schema)到实际数据库表结构。这导致 app 插件(如 plugin-auth、plugin-audit 等)注册的对象(如 sys_user 等)虽然元数据已加载,但数据表可能未被自动创建��更新——首次访问时会出现 SQLITE_ERROR: no such table: sys_user 等报错。

影响范围

  • 所有通过 registerService('app.<id>', manifest) 注册的对象无法保证表结构自动同步到数据库
  • plugin-auth、plugin-audit 等系统插件下的用户/审计等核心表不会自动初始化
  • 使用 SQLite/Turso 等"需自动建表"的驱动时表现尤为明显

期望行为

  • ObjectQL 应在启动流程中,自动遍历所有注册对象(SchemaRegistry.getAllObjects())并调用各 driver 的 syncSchema(object, schema) 方法完成表结构同步
  • 同步操作需保证幂等(多次 sync 不会重复建表或报错,兼容字段/索引新增)
  • 该逻辑应在 Plugin 层(即 ObjectQLPlugin.start() 的 init() 之后)实现,逐对象调用 syncSchema,而不是批量传递对象数组(符合协议定义

推荐修复方案

// packages/objectql/src/plugin.ts// ObjectQLPlugin.start() 中 await this.ql?.init() 后新增:constallObjects=this.ql?.registry?.getAllObjects?.()??[];if(allObjects.length>0){for(constobjofallObjects){constdriver=this.ql!.getDriver(obj.name);awaitdriver.syncSchema(obj.name,obj);// 符合 syncSchema 单对象签名}}
  • 若 driver 不支持 syncSchema,需有容错日志
  • 不建议将 schema 同步逻辑下沉到 ObjectQL.init(),应保持边界分明(Plugin/Kernel 层负责 orchestrate,Engine 层只负责 registry+connect)

协议兼容性检查

  • syncSchema(idempotent) 方法已在 data-driver.ts 协议定义 和 Zod schema 中声明
  • 多驱动、插件动态注册均适配(按需调用 getDriver)
  • 完全符合"Data as Code/幂等基建"理念

相关链接


注:本 Issue 属于 runtime/实现补丁,不涉及协议定义变更。

Metadata

Metadata

Labels

bugSomething isn't workingenhancementNew feature or request

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions