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 schemadiff

从源码安装

git clone https://github.com/nevernet/SchemaDiff.git
cd SchemaDiff
pip install -e .

开发模式安装

pip install -e .[dev]

使用方法

命令行

# 对比两个 SQL 文件
schemadiff users_v1.sql users_v2.sql

# 对比两个目录
schemadiff backup/20260301 SQL

# 输出到文件
schemadiff old.sql new.sql -o migration.sql

# 指定方言
schemadiff 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.0.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.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nevernet_sql_diff-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 7d9d357693e51023b34228d23b7ee290d3be9d41ba75d50b690ec84516fa33db
MD5 1a117c558fdc8d6cb843e9aa736fc09b
BLAKE2b-256 32469f2f58fb8c0b8d7b6d699effd4f8727b0fd6f97236b6642d6b843c8aa1ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nevernet_sql_diff-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 773235ffa2dae311b267e120650b795c18f53adf68651a737dd096bb88d8779a
MD5 ae6dfe5284b7cadd9eb89c27fcdb1ad0
BLAKE2b-256 ca5c6544b0c3fd8005d2d6f1a536e258a9536b4c267b069640117e1f1bedda92

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