跳到主要内容

Schema 检查

VEF 内置了一个 schema 检查 service,以及一个可通过应用 API 读取数据库结构的内置资源。

内置实现只检查 primary data source。它通过 Atlas 支持 PostgreSQL、MySQL 和 SQLite。PostgreSQL 会使用已配置的 vef.datasource.*.schema,未配置时默认 为 public;MySQL 检查当前 DATABASE();SQLite 检查 main schema。

模块输出

schema 模块会提供:

输出含义
schema.Serviceschema 检查服务
sys/schema内置 RPC 资源

schema.Service 接口

公开的 schema service 暴露如下方法:

方法返回类型作用
ListTables(ctx)[]schema.Table列出当前 schema / database 下的表
GetTableSchema(ctx, name)*schema.TableSchema检查单张表的详细结构
ListViews(ctx)[]schema.View列出视图

内置资源

schema 模块会注册:

资源
sys/schema

当前 action:

Action输入参数输出类型说明
list_tables[]schema.Table列出当前表
get_table_schemaname: stringschema.TableSchema表不存在时会返回专门的 schema-table-not-found 业务错误
list_views[]schema.View列出当前视图

源码层面的实现细节:

  • 每个 action 当前都单独设置了 Max = 60 的限流上限
  • get_table_schema 会校验 name 必填
  • 表不存在时会映射到 schema.ErrCodeTableNotFoundErrCodeTableNotFound,对应 schema.ErrTableNotFound / ErrTableNotFound sentinel)
  • RPC 响应仍使用标准 result envelope;业务错误写在 body 里,不通过不同 HTTP status 区分

错误 API

API含义
schema.ErrTableNotFound请求的表不存在时返回的业务错误
schema.ErrCodeTableNotFound表不存在对应的数值业务错误码
schema.ErrTableMissing普通 Go sentinel(v0.38),Service 检查找不到请求的表时返回——供直接使用 service 的 Go 调用方;RPC 层仍映射为 ErrTableNotFound

公共 Schema 类型

所有公开 schema DTO 的 JSON 字段名就是 wire contract。带 omitempty 的字段在 为空时会被省略,例如 schemacommentprimaryKeyindexesuniqueKeysforeignKeyschecksdefaultisPrimaryKeyisAutoIncrementonUpdateonDelete,以及 schema.Viewcolumns

schema.Table

字段类型含义
namestring表名
schemastringschema 名
commentstring表注释

schema.TableSchema

字段类型含义
namestring表名
schemastringschema 名
commentstring表注释
columns[]schema.Column全部列
primaryKey*schema.PrimaryKey主键定义
indexes[]schema.Index非唯一索引
uniqueKeys[]schema.UniqueKey唯一约束
foreignKeys[]schema.ForeignKey外键约束
checks[]schema.Checkcheck 约束

schema.Column

字段类型含义
namestring列名
typestring原始数据库类型
nullablebool是否可空
defaultstring默认表达式
commentstring列注释
isPrimaryKeybool是否属于主键
isAutoIncrementbool是否自增

isAutoIncrement 会识别 MySQL AUTO_INCREMENT、SQLite AUTOINCREMENT、 PostgreSQL identity column,以及 PostgreSQL serialbigserialsmallserial raw type。

schema.PrimaryKey

字段类型含义
namestring主键名称
columns[]string主键列

schema.Index

字段类型含义
namestring索引名
columns[]string索引列

schema.UniqueKey

字段类型含义
namestring唯一键名
columns[]string唯一列
predicatestring条件唯一索引(partial index)的谓词(v0.38;为空时省略)
hasExpressionsbool唯一索引含表达式列时为 true(v0.38;这类键不保证纯列组合的唯一性)

schema.ForeignKey

字段类型含义
namestring外键名
columns[]string本地列
refTablestring引用表
refColumns[]string引用列
onUpdatestring更新策略
onDeletestring删除策略

schema.Check

字段类型含义
namestringcheck 约束名称
exprstringcheck 表达式

schema.View

字段类型含义
namestring视图名
schemastringschema 名
definitionstring视图定义 SQL
commentstring视图注释
columns[]string输出列

视图列表使用数据库方言专属查询:PostgreSQL 和 MySQL 读取 information_schema.views;SQLite 读取 sqlite_schema,并排除内部 sqlite_% 视图。

最小请求示例

{
"resource": "sys/schema",
"action": "get_table_schema",
"version": "v1",
"params": {
"name": "sys_user"
}
}

典型用途

这个功能很适合:

  • 后台管理工具
  • 内部开发者工具
  • 需要 schema 感知的集成能力
  • 需要数据库元信息的 MCP / prompt 工作流

下一步

继续阅读 内置资源,看 sys/schema 在整个框架内置 RPC 资源体系里所处的位置。