db-legacy-migration-agent
MCP server that parses legacy relational database schemas (Oracle, DB2, MySQL, MSSQL) and transpiles them to PostgreSQL with generated Prisma schema and TypeScript query helpers.
README
db-legacy-migration-agent
CLI and MCP Server that parses legacy relational DB schemas (DB2, Oracle PL/SQL, MySQL, MSSQL) and transpiles them automatically to PostgreSQL with a generated Prisma ORM schema and TypeScript query helpers.
Table of Contents
- Overview
- Architecture
- Getting Started
- CLI Commands
- MCP Server
- Type Mapping Reference
- Validation Rules
- Project Structure
- Running Tests
Overview
Legacy enterprise systems often rely on vendor-specific SQL dialects (Oracle PL/SQL, IBM DB2, Microsoft T-SQL) that cannot be migrated directly to modern stacks without significant manual effort. This tool automates the structural translation phase:
| Input | Output |
|---|---|
CREATE TABLE (Oracle, DB2, MySQL, MSSQL) |
schema.prisma model definitions |
PL/SQL CREATE PROCEDURE / CREATE FUNCTION |
Best-effort TypeScript equivalent |
| Any mix of legacy DDL | TypeScript Prisma Client query helpers |
| Full DDL file | Validation report with precision-loss analysis |
Architecture
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)
Core Modules
src/parser/sql-transpiler.ts
Responsible for the full transpilation pipeline:
- Tokenisation — strips comments, normalises whitespace, handles quoted identifiers
- DDL parsing —
CREATE TABLEwith columns, constraints, FKs, indexes - PL/SQL parsing —
CREATE [OR REPLACE] PROCEDURE/FUNCTIONwith parameter directions - Type mapping — 40+ legacy type mappings to
{ prismaType, postgresType } - Prisma schema generation —
@@map,@db.*annotations, composite PKs, FK relations - TypeScript query generation — CRUD helpers using
PrismaClient - PL/SQL structural translation —
BEGIN/END,IF/THEN/ELSIF,FOR/WHILE LOOP,:=,DBMS_OUTPUT
src/engine/schema-validator.ts
Runs a rule engine over the transpiled table definitions and emits structured ValidationIssue records:
- Critical — data loss guaranteed (e.g.,
BIGINT_OVERFLOW,NULLABLE_PK) - Warning — semantic mismatch requiring review (e.g.,
ORACLE_DATE_HAS_TIME,XMLTYPE_NO_NATIVE) - Info — informational notes (e.g.,
LOB_TO_TEXT,DB2_GRAPHIC_TYPE)
src/mcp/server.ts
MCP server exposing three tools over stdio transport:
| Tool | Description |
|---|---|
parse_legacy_ddl |
Full parse + generate: returns AST, Prisma schema, TS queries |
generate_prisma_schema |
Returns only the schema.prisma content |
validate_type_mapping |
Returns structured or text validation report |
Getting Started
Prerequisites
- Node.js ≥ 18
- npm ≥ 9
Install
npm install
Build
npm run build
Link CLI globally (optional)
npm link
db-migrate --help
CLI Commands
transpile <file>
Parses a DDL file and generates schema.prisma, queries.ts, and ast.json in the output directory.
npx ts-node src/cli.ts transpile ./examples/oracle_hr.sql \
--dialect oracle \
--out ./output
Options:
| Flag | Default | Description |
|---|---|---|
-d, --dialect |
oracle |
Source dialect: db2 | oracle | mysql | mssql |
-o, --out |
./output |
Output directory |
--no-ts |
— | Skip TypeScript query generation |
--no-validate |
— | Skip post-transpile validation |
validate <file>
Validates type mappings and outputs a structured report.
npx ts-node src/cli.ts validate ./examples/oracle_hr.sql \
--dialect oracle \
--format text
Options:
| Flag | Default | Description |
|---|---|---|
-d, --dialect |
oracle |
Source dialect |
-f, --format |
text |
text or json |
--fail-on-warnings |
— | Exit code 1 if warnings found (for CI pipelines) |
Exit codes:
| Code | Meaning |
|---|---|
0 |
No issues or info only |
1 |
Warnings found (only with --fail-on-warnings) |
2 |
Critical issues found |
parse-inline <ddl>
Quick test — parse a DDL string directly from the command line.
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
Start the MCP server over stdio (for AI assistant integration).
npx ts-node src/cli.ts mcp
MCP Server
The MCP server can be registered with any MCP-compatible AI assistant (e.g., Claude Desktop, IBM Bob).
Tool: parse_legacy_ddl
{
"tool": "parse_legacy_ddl",
"input": {
"ddl": "CREATE TABLE EMPLOYEES (...);",
"dialect": "oracle",
"include_typescript": true
}
}
Returns: full AST, Prisma schema, TypeScript queries, warnings.
Tool: generate_prisma_schema
{
"tool": "generate_prisma_schema",
"input": {
"ddl": "CREATE TABLE EMPLOYEES (...);",
"dialect": "oracle"
}
}
Returns: schema.prisma content as a plain string.
Tool: validate_type_mapping
{
"tool": "validate_type_mapping",
"input": {
"ddl": "CREATE TABLE EMPLOYEES (...);",
"dialect": "oracle",
"format": "json"
}
}
Returns: structured ValidationReport JSON or human-readable text.
Type Mapping Reference
| Legacy Type | Prisma Type | PostgreSQL Type | Notes |
|---|---|---|---|
NUMBER(p) / NUMERIC |
Decimal |
DECIMAL(p) |
Precision preserved |
NUMBER(p,s) |
Decimal |
DECIMAL(p,s) |
Scale preserved |
NUMBER(p) p≤9 |
Int |
INTEGER |
Fits 32-bit |
NUMBER(p) 10≤p≤18 |
BigInt |
BIGINT |
Fits 64-bit |
NUMBER(p) p>18 |
Decimal |
DECIMAL(p) |
⚠ BigInt would overflow |
VARCHAR2(n) |
String |
VARCHAR(n) |
|
CHAR(n) |
String |
CHAR(n) |
Fixed-length padding |
CLOB / NCLOB / LONG |
String |
TEXT |
ℹ No separate LOB segment |
BLOB / RAW |
Bytes |
BYTEA |
ℹ Inline storage |
DATE (Oracle) |
DateTime |
DATE |
⚠ Oracle DATE includes time |
TIMESTAMP |
DateTime |
TIMESTAMP |
|
TIMESTAMP WITH TIME ZONE |
DateTime |
TIMESTAMPTZ |
|
BINARY_FLOAT |
Float |
REAL |
⚠ Single precision |
BINARY_DOUBLE |
Float |
DOUBLE PRECISION |
|
XMLTYPE |
String |
XML |
⚠ No Prisma native XML |
BIGINT |
BigInt |
BIGINT |
|
DECIMAL(p,s) |
Decimal |
DECIMAL(p,s) |
|
BOOLEAN |
Boolean |
BOOLEAN |
|
JSON / JSONB |
Json |
JSON / JSONB |
Validation Rules
| Code | Severity | Trigger | Recommendation |
|---|---|---|---|
ORACLE_NUMBER_NO_SCALE |
warning | NUMBER(p) without scale → could be integer or float |
Add explicit scale |
BIGINT_OVERFLOW |
critical | NUMBER(p) p>18 mapped to BigInt |
Use Decimal / NUMERIC |
FLOAT_SINGLE_PRECISION |
warning | BINARY_FLOAT or FLOAT(≤24) → REAL |
Use DOUBLE PRECISION |
LOB_TO_TEXT |
info | CLOB/NCLOB/LONG → TEXT |
Update LOB streaming APIs |
BLOB_TO_BYTEA |
info | BLOB/RAW → BYTEA |
Use lo API for > 1 GB values |
ORACLE_DATE_HAS_TIME |
warning | Oracle DATE → PostgreSQL DATE |
Use TIMESTAMP if time needed |
LOCAL_TZ_SEMANTICS |
warning | TIMESTAMP WITH LOCAL TIME ZONE |
Verify TZ conversion logic |
CHAR_LARGE_LENGTH |
warning | CHAR(n) n>255 |
Replace with VARCHAR(n) |
VARCHAR2_EXCEEDS_ORACLE_LIMIT |
info | VARCHAR2(n) n>4000 |
Use TEXT for unbounded |
XMLTYPE_NO_NATIVE |
warning | XMLTYPE |
Use $queryRaw for XML ops |
DB2_GRAPHIC_TYPE |
info | DB2 GRAPHIC/VARGRAPHIC |
Verify UTF-8 transcoding |
NO_PRIMARY_KEY |
warning | Table has no PK | Add id or @@id |
NULLABLE_PK |
critical | PK column parsed as nullable | Fix source DDL |
Project Structure
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
Running Tests
# Run all tests
npm test
# With coverage
npm test -- --coverage
# Watch mode
npm test -- --watch
Expected output: 40+ assertions across transpiler parsing, type mapping, PL/SQL translation, and validator rules.
Contributing
- Fork and clone the repository
- Run
npm installto install dependencies - Add your feature/fix in
src/ - Add or update tests in
tests/ - Run
npm testandnpm run typecheckbefore submitting a PR
License
MIT
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。