Skip to content

feat: add schema dump command for AI database discovery#28

Merged
zx06 merged 10 commits intomainfrom
feature/schema-dump
Feb 11, 2026
Merged

feat: add schema dump command for AI database discovery#28
zx06 merged 10 commits intomainfrom
feature/schema-dump

Conversation

@zx06
Copy link
Owner

@zx06 zx06 commented Feb 11, 2026

概述

新增 xsql schema dump 命令,导出数据库结构信息(表、列、类型、约束、索引),供 AI/agent 自动理解数据库 schema。

变更内容

新增功能

  • xsql schema dump 命令,支持导出数据库结构
  • 支持 MySQL 和 PostgreSQL
  • 输出包含:表、列、索引、外键
  • 支持表名过滤(通配符 *?
  • 支持 JSON/YAML/Table 输出格式

文件变更

文件 说明
internal/db/schema.go Schema 数据结构和接口
internal/db/mysql/schema.go MySQL schema 提取实现
internal/db/pg/schema.go PostgreSQL schema 提取实现
cmd/xsql/schema.go CLI 命令实现
internal/output/writer.go Table 格式化支持
internal/app/app.go Tool spec 更新
docs/rfcs/0005-schema-dump.md RFC 设计文档
docs/cli-spec.md CLI 规范更新
docs/ai.md AI 集成文档更新
README.md 项目说明更新

测试

  • ? 单元测试:internal/db/schema_test.go
  • ? E2E 测试:tests/e2e/schema_test.go(8 个测试用例)

使用示例

# 导出所有表结构
xsql schema dump -p dev -f json

# 过滤特定表(支持通配符)
xsql schema dump -p dev --table "user*"

# 包含系统表
xsql schema dump -p dev --include-system

输出示例

{
  "ok": true,
  "schema_version": 1,
  "data": {
    "database": "mydb",
    "tables": [
      {
        "schema": "public",
        "name": "users",
        "columns": [
          {"name": "id", "type": "bigint", "primary_key": true},
          {"name": "email", "type": "varchar(255)", "nullable": false}
        ]
      }
    ]
  }
}

相关文档

  • RFC: docs/rfcs/0005-schema-dump.md
  • CLI 规范: docs/cli-spec.md
  • AI 集成: docs/ai.md

zx06 added 2 commits February 11, 2026 14:34
- Add {"ok":false,"schema_version":1,"error":{"code":"XSQL_INTERNAL","message":"unknown command \"schema\" for \"xsql\""}} command to export database structure
- Support MySQL and PostgreSQL schema extraction
- Output includes tables, columns, indexes, and foreign keys
- Support table name filtering with wildcards
- Add JSON/YAML/Table output formats
- Add unit tests and E2E tests
- Update documentation (cli-spec.md, ai.md)
- Add RFC 0005 for design documentation

This allows AI agents to automatically discover database schema
without manual intervention.
- Add schema dump to README command list
- Add schema discovery section with examples
- Update AI skill prompt to include schema dump
- Add schema dump to tool spec export
@codecov
Copy link

codecov bot commented Feb 11, 2026

Codecov Report

❌ Patch coverage is 79.54545% with 108 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.76%. Comparing base (2f2688d) to head (d32d5eb).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
internal/db/pg/schema.go 81.33% 21 Missing and 21 partials ⚠️
internal/db/mysql/schema.go 79.51% 17 Missing and 17 partials ⚠️
cmd/xsql/schema.go 55.55% 25 Missing and 3 partials ⚠️
internal/db/schema.go 92.30% 1 Missing and 1 partial ⚠️
internal/output/writer.go 94.73% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #28      +/-   ##
==========================================
+ Coverage   79.02%   80.76%   +1.74%     
==========================================
  Files          32       36       +4     
  Lines        1864     2392     +528     
==========================================
+ Hits         1473     1932     +459     
- Misses        326      357      +31     
- Partials       65      103      +38     
Flag Coverage Δ
e2e 44.98% <70.10%> (-3.66%) ⬇️
integration 44.98% <70.10%> (-3.66%) ⬇️
unittests 66.26% <19.50%> (-12.50%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
14.2% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@zx06 zx06 merged commit ee28b82 into main Feb 11, 2026
13 of 14 checks passed
@zx06 zx06 deleted the feature/schema-dump branch February 11, 2026 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant