db-legacy-migration-agent
db-legacy-migration-agent
CLI 与 MCP 服务器,解析遗留关系型数据库模式(DB2、Oracle PL/SQL、MySQL、MSSQL),并自动将其转译为带生成的 Prisma ORM 模式和 TypeScript 查询辅助函数的 PostgreSQL。
目录
Related MCP server: db-mcp
概述
遗留企业系统通常依赖特定于供应商的 SQL 方言(Oracle PL/SQL、IBM DB2、Microsoft T-SQL),如果不进行大量手动工作,就无法直接迁移到现代技术栈。该工具自动化了结构转换阶段:
输入 | 输出 |
|
|
PL/SQL | 尽力而为的 TypeScript 等价实现 |
任意混合的遗留 DDL | TypeScript Prisma Client 查询辅助函数 |
完整 DDL 文件 | 带精度损失分析的验证报告 |
架构
src/
├── parser/
│ └── sql-transpiler.ts # DDL lexer/parser + Prisma/TS code generator
├── engine/
│ └── schema-validator.ts # Precision-loss & semantic mismatch validator
├── mcp/
│ └── server.ts # MCP server (stdio transport)
└── cli.ts # Commander.js interactive CLI
tests/
└── transpiler.test.ts # Jest unit tests (40+ assertions)核心模块
src/parser/sql-transpiler.ts
负责完整的转译流水线:
词法分析 — 去除注释、规范化空白、处理带引号的标识符
DDL 解析 —
CREATE TABLE,包含列、约束、外键、索引PL/SQL 解析 —
CREATE [OR REPLACE] PROCEDURE/FUNCTION,包含参数方向类型映射 — 40 多种遗留类型映射到
{ prismaType, postgresType }Prisma 模式生成 —
@@map、@db.*注解、复合主键、外键关系TypeScript 查询生成 — 使用
PrismaClient的 CRUD 辅助函数PL/SQL 结构转换 —
BEGIN/END、IF/THEN/ELSIF、FOR/WHILE LOOP、:=、DBMS_OUTPUT
src/engine/schema-validator.ts
对转译后的表定义运行规则引擎,并输出结构化的 ValidationIssue 记录:
严重 — 保证数据丢失(例如,
BIGINT_OVERFLOW、NULLABLE_PK)警告 — 需要审查的语义不匹配(例如,
ORACLE_DATE_HAS_TIME、XMLTYPE_NO_NATIVE)信息 — 提示性说明(例如,
LOB_TO_TEXT、DB2_GRAPHIC_TYPE)
src/mcp/server.ts
MCP 服务器,通过 stdio 传输暴露三个工具:
工具 | 描述 |
| 完整解析 + 生成:返回 AST、Prisma 模式、TS 查询 |
| 仅返回 |
| 返回结构化或文本验证报告 |
快速开始
前置要求
Node.js ≥ 18
npm ≥ 9
安装
npm install构建
npm run build全局链接 CLI(可选)
npm link
db-migrate --helpCLI 命令
transpile <file>
解析 DDL 文件,并在输出目录中生成 schema.prisma、queries.ts 和 ast.json。
npx ts-node src/cli.ts transpile ./examples/oracle_hr.sql \
--dialect oracle \
--out ./output选项:
标志 | 默认值 | 描述 |
|
| 源方言: |
|
| 输出目录 |
| — | 跳过 TypeScript 查询生成 |
| — | 跳过转译后验证 |
validate <file>
验证类型映射并输出结构化报告。
npx ts-node src/cli.ts validate ./examples/oracle_hr.sql \
--dialect oracle \
--format text选项:
标志 | 默认值 | 描述 |
|
| 源方言 |
|
|
|
| — | 发现警告时退出码为 1(用于 CI 流水线) |
退出码:
代码 | 含义 |
| 无问题或仅有信息级别问题 |
| 发现警告(仅在 |
| 发现严重问题 |
parse-inline <ddl>
快速测试 — 直接从命令行解析 DDL 字符串。
npx ts-node src/cli.ts parse-inline \
"CREATE TABLE T (ID NUMBER(10) NOT NULL, NAME VARCHAR2(100), CONSTRAINT PK_T PRIMARY KEY (ID));"mcp
通过 stdio 启动 MCP 服务器(用于 AI 助手集成)。
npx ts-node src/cli.ts mcpMCP 服务器
MCP 服务器可以注册到任何兼容 MCP 的 AI 助手(例如,Claude Desktop、IBM Bob)。
工具:parse_legacy_ddl
{
"tool": "parse_legacy_ddl",
"input": {
"ddl": "CREATE TABLE EMPLOYEES (...);",
"dialect": "oracle",
"include_typescript": true
}
}返回:完整 AST、Prisma 模式、TypeScript 查询、警告。
工具:generate_prisma_schema
{
"tool": "generate_prisma_schema",
"input": {
"ddl": "CREATE TABLE EMPLOYEES (...);",
"dialect": "oracle"
}
}返回:schema.prisma 内容作为纯字符串。
工具:validate_type_mapping
{
"tool": "validate_type_mapping",
"input": {
"ddl": "CREATE TABLE EMPLOYEES (...);",
"dialect": "oracle",
"format": "json"
}
}返回:结构化的 ValidationReport JSON 或人类可读文本。
类型映射参考
遗留类型 | Prisma 类型 | PostgreSQL 类型 | 说明 |
|
|
| 保留精度 |
|
|
| 保留小数位 |
|
|
| 适配 32 位 |
|
|
| 适配 64 位 |
|
|
| ⚠ BigInt 会溢出 |
|
|
| |
|
|
| 定长填充 |
|
|
| ℹ 无独立 LOB 段 |
|
|
| ℹ 内联存储 |
|
|
| ⚠ Oracle DATE 包含时间 |
|
|
| |
|
|
| |
|
|
| ⚠ 单精度 |
|
|
| |
|
|
| ⚠ Prisma 无原生 XML 支持 |
|
|
| |
|
|
| |
|
|
| |
|
|
|
验证规则
代码 | 严重级别 | 触发条件 | 建议 |
| 警告 |
| 添加显式小数位 |
| 严重 |
| 使用 |
| 警告 |
| 使用 |
| 信息 |
| 更新 LOB 流式 API |
| 信息 |
| 对于 > 1 GB 的值使用 lo API |
| 警告 | Oracle | 如果需要时间,使用 |
| 警告 |
| 验证时区转换逻辑 |
| 警告 |
| 替换为 |
| 信息 |
| 对于无界长度使用 |
| 警告 |
| 对 XML 操作使用 |
| 信息 | DB2 | 验证 UTF-8 转码 |
| 警告 | 表没有主键 | 添加 |
| 严重 | 主键列被解析为可空 | 修复源 DDL |
项目结构
db-legacy-migration-agent/
├── src/
│ ├── parser/
│ │ └── sql-transpiler.ts # Type mappings, DDL parser, Prisma & TS generators
│ ├── engine/
│ │ └── schema-validator.ts # Rule engine, ValidationReport, formatter
│ ├── mcp/
│ │ └── server.ts # MCP server with 3 tools
│ └── cli.ts # Commander.js CLI entrypoint
├── tests/
│ └── transpiler.test.ts # Jest unit tests
├── dist/ # Compiled output (after `npm run build`)
├── output/ # Generated files (schema.prisma, queries.ts, ast.json)
├── package.json
├── tsconfig.json
└── README.md运行测试
# Run all tests
npm test
# With coverage
npm test -- --coverage
# Watch mode
npm test -- --watch预期输出:40 多个断言,涵盖转译器解析、类型映射、PL/SQL 转换和验证器规则。
贡献指南
Fork 并克隆仓库
运行
npm install安装依赖在
src/中添加你的功能/修复在
tests/中添加或更新测试提交 PR 前运行
npm test和npm run typecheck
许可证
MIT
This server cannot be installed
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Servers
- AlicenseNot gradedqualityCmaintenanceAn extensible MCP server for database operations that supports PostgreSQL for managing schemas, tables, data, and user permissions. It features automatic migration recording for DDL changes and integrates with various AI-powered editors like Cursor, Zed, and Claude Code.222MIT
- AlicenseAqualityCmaintenanceA lightweight MCP server for relational databases, enabling dynamic connections to PostgreSQL and MySQL, SQL execution, and transaction control.7511MIT
- AlicenseNot gradedqualityDmaintenanceMCP server that analyzes TypeScript/Prisma projects, builds dependency graphs, and protects against dangerous modifications and silent regressions.141MIT
- AlicenseAqualityAmaintenanceMCP server that reads your database schema from SQL DDL, Prisma, Drizzle, TypeORM, or SQLAlchemy, generates a Mermaid ER diagram, and writes it into your documentation, with drift detection to keep diagrams up-to-date.51MIT
Related MCP Connectors
MCP server for managing Prisma Postgres.
MCP server for interacting with the Supabase platform
Butterbase MCP server — manage your backend: schemas, auth, functions, storage, RAG, deploys.
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/felipeassis10/db-legacy-migration-agent'
If you have feedback or need assistance with the MCP directory API, please join our Discord server