SQL Schema Diff and Migration Generator
Project description
SchemaDiff
SQL Schema 对比工具 - 比较两个 SQL 文件或目录,生成 Migration 脚本。
功能特性
- 🔍 Schema 对比 - 比较两个数据库 schema 的差异
- 📝 Migration 生成 - 自动生成 ALTER 语句
- 📁 目录支持 - 支持目录级别的批量对比
- 🗄️ 多方言支持 - MySQL / PostgreSQL
安装
从 PyPI 安装 (推荐)
pip install nevernet-sql-diff
从源码安装
git clone https://github.com/nevernet/SchemaDiff.git
cd SchemaDiff
pip install -e .
开发模式安装
pip install -e .[dev]
使用方法
命令行
# 对比两个 SQL 文件
nevernet-sql-diff users_v1.sql users_v2.sql
# 对比两个目录
nevernet-sql-diff backup/20260301 SQL
# 输出到文件
nevernet-sql-diff old.sql new.sql -o migration.sql
# 指定方言
nevernet-sql-diff old.sql new.sql -d postgres
Python 模块
from migration import parse_sql_file, SchemaDiff
# 解析 SQL
source = parse_sql_file("users_v1.sql", "mysql")
target = parse_sql_file("users_v2.sql", "mysql")
# 对比
diff = SchemaDiff(source, target)
changes = diff.compare()
# 生成迁移脚本
migration = diff.generate_migration()
print(migration)
发布到 PyPI
1. 安装发布工具
pip install build twine
2. 构建包
python -m build
3. 发布到 PyPI
twine upload dist/*
或者使用测试 PyPI:
twine upload --repository testpypi dist/*
4. 版本管理
更新版本号请修改:
setup.py中的version- Git tag:
git tag v0.1.0 && git push --tags
测试样本
# 单文件对比
./run.sh SQL/users_v1.sql SQL/users_v2.sql
# 目录对比
./run.sh backup/20260301 SQL
# 输出到文件
./run.sh SQL/users_v1.sql SQL/users_v2.sql -o migration.sql
支持的 SQL 特性
| 特性 | 状态 |
|---|---|
| CREATE TABLE | ✅ |
| ADD COLUMN | ✅ |
| DROP COLUMN | ✅ |
| ALTER COLUMN | ✅ |
| PRIMARY KEY | ✅ |
| FOREIGN KEY | ✅ |
| UNIQUE INDEX | ✅ |
| DEFAULT VALUE | ✅ |
| AUTO_INCREMENT | ✅ |
| COMMENT | ✅ |
技术栈
- Python 3.8+
- sqlglot (SQL 解析)
许可证
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nevernet_sql_diff-0.1.2.tar.gz.
File metadata
- Download URL: nevernet_sql_diff-0.1.2.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed7539d12be292796b7e111b877d1fc91f4000e75f593efe8b9ae4449569f448
|
|
| MD5 |
9beb1cbc857476eb2fb96390381d38af
|
|
| BLAKE2b-256 |
346bd2e6c3f1ffa5ca9f501ea7d766c749c73928c9bf7cf31ce7ca0e6e988723
|
File details
Details for the file nevernet_sql_diff-0.1.2-py3-none-any.whl.
File metadata
- Download URL: nevernet_sql_diff-0.1.2-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f29fcdda16a81d8d0931f7c01bac34f35d27ecbc6c7bfd7aeb89b5ba6df9270
|
|
| MD5 |
238682555912190e0e4ec947d973b8f0
|
|
| BLAKE2b-256 |
ff42ed316c14299b21fffe749735c8395eb195d372eb92265861923766dc690b
|