Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nevernet_sql_diff-0.1.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nevernet_sql_diff-0.1.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file nevernet_sql_diff-0.1.1.tar.gz.

File metadata

  • Download URL: nevernet_sql_diff-0.1.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for nevernet_sql_diff-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a3385133e1312285b9e6d8e806e73d2737088bce5bbe85f645a2ef86ff5276d5
MD5 3950c9cbe00d1b8454cd5732cbfeb158
BLAKE2b-256 041a709874856b7ac628f3f2bb3fa6a68d9b0e6d185975a3867cd1002582d03f

See more details on using hashes here.

File details

Details for the file nevernet_sql_diff-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for nevernet_sql_diff-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f62095b85c921ea2645431617c6df17e5a970e5eefa5380700db105208804d3
MD5 61ad882ac523bd736243d932bfeb15f1
BLAKE2b-256 7f87989cb524d616d4f0e34f4ffc2cbc19505774f0b8fe374bbde2341c78e3c1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page