返回顶部
d

database-schema-differ数据库模式对比

Compare database schemas across environments, generate migration scripts, and track schema evolution.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
262
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

database-schema-differ

数据库模式差异比较工具

功能说明

一款命令行工具,用于比较不同环境(开发、测试、生产)之间的数据库模式,生成迁移脚本,并追踪模式随时间的变化。通过SQLAlchemy支持PostgreSQL、MySQL、SQLite等多种数据库。

主要特性:

  • - 模式比较:比较数据库、分支或时间点之间的模式
  • 迁移生成:自动为模式变更生成SQL迁移脚本(向上/向下)
  • 模式快照:捕获并存储模式快照,用于历史比较
  • 漂移检测:识别环境间的模式漂移(开发vs生产等)
  • 多数据库支持:通过SQLAlchemy支持PostgreSQL、MySQL、SQLite、SQL Server、Oracle
  • 导出格式:生成SQL、JSON或可视化差异输出
  • 集成就绪:可与Alembic、Django迁移或独立使用
  • 变更追踪:通过版本控制追踪模式随时间的变化
  • CI/CD友好:输出机器可读格式,适用于自动化流水线

使用场景

  • - 需要比较开发和生产环境之间的数据库模式
  • 想要为模式变更生成迁移脚本
  • 管理多个数据库环境,需要确保一致性
  • 需要检测生产数据库中的模式漂移
  • 正在重构数据库,需要追踪变更
  • 希望在CI/CD流水线中自动化模式验证
  • 需要为合规或团队协调记录模式变更
  • 正在培训新团队成员,需要了解模式演变
  • 想要可视化分支或版本之间的模式差异

使用方法

基本命令:

bash

比较两个数据库连接


python3 scripts/main.py compare postgresql://user:pass@host1/db postgresql://user:pass@host2/db

从模式差异生成迁移脚本

python3 scripts/main.py diff devdb.sql proddb.sql --output migration.sql

创建模式快照供后续比较

python3 scripts/main.py snapshot postgresql://user:pass@host/db --save snapshot.json

比较当前模式与保存的快照

python3 scripts/main.py compare-snapshot postgresql://user:pass@host/db snapshot.json

生成模式间的可视化差异

python3 scripts/main.py visual-diff schema1.sql schema2.sql --html diff.html

在CI流水线中检查模式漂移

python3 scripts/main.py check-drift --expected expectedschema.json --actual actualschema.json

追踪模式随时间的变化

python3 scripts/main.py history postgresql://user:pass@host/db --days 30

示例

示例1:比较开发和生产数据库

bash
python3 scripts/main.py compare \
postgresql://devuser:devpass@localhost/dev_db \
postgresql://produser:prodpass@prod-host/prod_db \
--output diff-report.json

输出:

🔍 正在比较模式:devdb (localhost) vs proddb (prod-host)

📊 摘要:

  • - 表:42 vs 45(开发环境缺少3个)
  • 列:287 vs 295(8处差异)
  • 索引:67 vs 72(5处差异)
  • 约束:34 vs 38(4处差异)

⚠️ 发现15处差异:

  1. 1. 开发环境缺少表 audit_logs

→ CREATE TABLE audit_logs (...)

  1. 2. 开发环境缺少列 users.email_verified
→ ALTER TABLE users ADD COLUMN email_verified BOOLEAN DEFAULT FALSE
  1. 3. 生产环境缺少索引 idxusersemail
→ CREATE INDEX idxusersemail ON users(email)
  1. 4. 约束 fkorderscustomer_id 存在差异
→ ALTER TABLE orders DROP CONSTRAINT fkorderscustomeridold; → ALTER TABLE orders ADD CONSTRAINT fkorderscustomer_id FOREIGN KEY ...

✅ 已生成迁移报告:diff-report.json
✅ SQL迁移脚本:migration20240306143022.sql

示例2:生成迁移脚本

bash
python3 scripts/main.py diff oldschema.sql newschema.sql --format sql --output migration.sql

输出(migration.sql):
sql
-- 生成时间:2024-03-06 14:30:22
-- 数据库:PostgreSQL

-- 向上迁移
CREATE TABLE audit_logs (
id SERIAL PRIMARY KEY,
user_id INTEGER,
action VARCHAR(255),
created_at TIMESTAMP DEFAULT NOW()
);

ALTER TABLE users ADD COLUMN email_verified BOOLEAN DEFAULT FALSE;

CREATE INDEX idxusersemail ON users(email);

ALTER TABLE orders
DROP CONSTRAINT fkorderscustomeridold,
ADD CONSTRAINT fkorderscustomer_id
FOREIGN KEY (customer_id) REFERENCES customers(id)
ON DELETE CASCADE;

-- 向下迁移(回滚)
DROP TABLE IF EXISTS audit_logs;

ALTER TABLE users DROP COLUMN IF EXISTS email_verified;

DROP INDEX IF EXISTS idxusersemail;

ALTER TABLE orders
DROP CONSTRAINT fkorderscustomer_id,
ADD CONSTRAINT fkorderscustomeridold
FOREIGN KEY (customer_id) REFERENCES customers(id);

示例3:在CI中检查模式漂移

bash
python3 scripts/main.py check-drift \
--expected schemas/expected/prod.json \
--actual schemas/actual/prod.json \
--fail-on-drift

输出(CI失败):

❌ 检测到模式漂移!

差异:

  1. 1. 生产环境中存在意外表 tempbackup
  2. 生产环境中缺少索引 idxordersstatus
  3. 列 users.lastlogin 类型不同(TIMESTAMP vs TIMESTAMPTZ)

退出代码:1(因 --fail-on-drift 而失败)

示例4:追踪模式演变

bash
python3 scripts/main.py history postgresql://user:pass@host/db --days 90 --format timeline

输出:

📅 模式演变时间线(最近90天)

2024-03-05:添加了audit_logs表(v4.2.0版本发布)
2024-02-28:向users表添加了email_verified列
2024-02-15:为性能优化创建了索引
2024-02-01:为数据完整性添加了外键约束
2024-01-20:初始模式快照(v4.0.0)

📈 变更统计:

  • - 表:+3(42 → 45)
  • 列:+23(272 → 295)
  • 索引:+8(64 → 72)
  • 平均每周变更:2.1

示例5:可视化模式比较

bash
python3 scripts/main.py visual-diff schemav1.sql schemav2.sql --html schema_diff.html

输出:

✨ 已生成可视化差异:schema_diff.html

在浏览器中打开可查看:

  • - 并排模式比较
  • 颜色编码的差异(添加/删除/修改)
  • 表的可交互展开/折叠
  • 文档导出选项

高亮显示的差异:
✅ 添加了5个表(绿色)
❌ 删除了2个表(红色)
🔄 修改了12列(黄色)

要求

  • - Python 3.x
  • SQLAlchemy(用于数据库连接)
  • Alembic(可选,用于迁移生成)
  • 数据库驱动:psycopg2(PostgreSQL)、pymysql(MySQL)等

安装依赖:
bash
pip3 install sqlalchemy alembic psycopg2-binary pymysql

局限性

  • - 需要数据库凭据和网络访问权限才能比较实时数据库
  • 复杂的模式变更可能需要手动审查生成的迁移
  • 对SQLAlchemy未覆盖的数据库特定功能支持有限
  • 对于非常大的模式(1000+表)可能会影响性能
  • 不支持NoSQL数据库(MongoDB、Redis等)
  • 无法比较加密或压缩的数据库转储
  • 对连接问题或权限错误的错误处理有限
  • 不支持跨所有数据库类型比较物化视图或数据库函数
  • 生成的迁移可能无法处理数据迁移或复杂转换
  • 不支持分布式数据库比较
  • 仅限于模式结构;不比较数据或索引优化
  • 对于具有自定义类型或扩展的数据库,可能无法检测所有模式差异
  • 不支持跨所有数据库类型比较数据库触发器或存储过程
  • 对于非常大的表或复杂关系,性能可能会下降
  • 不支持模式版本控制系统(如Liquibase或Flyway)
  • 对格式错误的SQL或损坏的模式文件的错误恢复有限
  • 不支持实时模式变更监控
  • 无法比较不同数据库类型之间的模式(例如PostgreSQL vs MySQL)
  • 对数据库特定优化或扩展的支持有限
  • 没有用于模式漂移警报的内置通知系统
  • 生产环境中可能需要手动调整生成的迁移脚本

目录结构

该工具使用数据库连接字符串、SQL文件或模式快照文件。无需特殊配置目录。

错误处理

  • - 无效的数据库连接会显示包含连接详细信息的帮助性错误消息
-

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 database-schema-differ-1776180918 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 database-schema-differ-1776180918 技能

通过命令行安装

skillhub install database-schema-differ-1776180918

下载

⬇ 下载 database-schema-differ v1.0.0(免费)

文件大小: 9.12 KB | 发布时间: 2026-4-15 11:21

v1.0.0 最新 2026-4-15 11:21
Initial release of database-schema-differ CLI tool:

- Compare database schemas across environments (development, staging, production)
- Automatically generate migration scripts (up/down) based on schema differences
- Capture, store, and compare schema snapshots over time
- Detect schema drift and track schema evolution with versioning
- Supports PostgreSQL, MySQL, SQLite, SQL Server, Oracle via SQLAlchemy/Alembic
- Output SQL, JSON, or visual diff reports; suitable for CI/CD automation

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部