Database Schema Differ
by @derick001
Compare database schemas across environments, generate migration scripts, and track schema evolution.
clawhub install database-schema-differπ About This Skill
name: database-schema-differ description: Compare database schemas across environments, generate migration scripts, and track schema evolution. version: 1.0.0 author: skill-factory metadata: openclaw: requires: bins: - python3 python: - sqlalchemy - alembic
Database Schema Differ
What This Does
A CLI tool to compare database schemas across different environments (development, staging, production), generate migration scripts, and track schema evolution over time. Support for PostgreSQL, MySQL, SQLite, and other databases via SQLAlchemy.
Key features:
When To Use
Usage
Basic commands:
# Compare two database connections
python3 scripts/main.py compare postgresql://user:pass@host1/db postgresql://user:pass@host2/dbGenerate migration script from schema differences
python3 scripts/main.py diff dev_db.sql prod_db.sql --output migration.sqlCreate schema snapshot for future comparison
python3 scripts/main.py snapshot postgresql://user:pass@host/db --save snapshot.jsonCompare current schema with saved snapshot
python3 scripts/main.py compare-snapshot postgresql://user:pass@host/db snapshot.jsonGenerate visual diff between schemas
python3 scripts/main.py visual-diff schema1.sql schema2.sql --html diff.htmlCheck for schema drift in CI pipeline
python3 scripts/main.py check-drift --expected expected_schema.json --actual actual_schema.jsonTrack schema evolution over time
python3 scripts/main.py history postgresql://user:pass@host/db --days 30
Examples
Example 1: Compare development and production databases
python3 scripts/main.py compare \
postgresql://dev_user:dev_pass@localhost/dev_db \
postgresql://prod_user:prod_pass@prod-host/prod_db \
--output diff-report.json
Output:
π Comparing schemas: dev_db (localhost) vs prod_db (prod-host)π Summary:
Tables: 42 vs 45 (3 missing in dev)
Columns: 287 vs 295 (8 differences)
Indexes: 67 vs 72 (5 differences)
Constraints: 34 vs 38 (4 differences) β οΈ Differences found (15):
1. Table audit_logs missing in dev
β CREATE TABLE audit_logs (...)
2. Column users.email_verified missing in dev
β ALTER TABLE users ADD COLUMN email_verified BOOLEAN DEFAULT FALSE
3. Index idx_users_email missing in prod
β CREATE INDEX idx_users_email ON users(email)
4. Constraint fk_orders_customer_id differs
β ALTER TABLE orders DROP CONSTRAINT fk_orders_customer_id_old;
β ALTER TABLE orders ADD CONSTRAINT fk_orders_customer_id FOREIGN KEY ...
β
Generated migration: diff-report.json
β
SQL migration script: migration_20240306_143022.sql
Example 2: Generate migration script
python3 scripts/main.py diff old_schema.sql new_schema.sql --format sql --output migration.sql
Output (migration.sql):
-- Generated: 2024-03-06 14:30:22
-- Database: PostgreSQL-- UP Migration
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 idx_users_email ON users(email);
ALTER TABLE orders
DROP CONSTRAINT fk_orders_customer_id_old,
ADD CONSTRAINT fk_orders_customer_id
FOREIGN KEY (customer_id) REFERENCES customers(id)
ON DELETE CASCADE;
-- DOWN Migration (rollback)
DROP TABLE IF EXISTS audit_logs;
ALTER TABLE users DROP COLUMN IF EXISTS email_verified;
DROP INDEX IF EXISTS idx_users_email;
ALTER TABLE orders
DROP CONSTRAINT fk_orders_customer_id,
ADD CONSTRAINT fk_orders_customer_id_old
FOREIGN KEY (customer_id) REFERENCES customers(id);
Example 3: Check for schema drift in CI
python3 scripts/main.py check-drift \
--expected schemas/expected/prod.json \
--actual schemas/actual/prod.json \
--fail-on-drift
Output (CI failure):
β Schema drift detected!Differences:
1. Unexpected table temp_backup in production
2. Missing index idx_orders_status in production
3. Column users.last_login has different type (TIMESTAMP vs TIMESTAMPTZ)
Exit code: 1 (failed due to --fail-on-drift)
Example 4: Track schema evolution
python3 scripts/main.py history postgresql://user:pass@host/db --days 90 --format timeline
Output:
π
Schema Evolution Timeline (last 90 days)2024-03-05: Added audit_logs table (v4.2.0 release)
2024-02-28: Added email_verified column to users table
2024-02-15: Created indexes for performance optimization
2024-02-01: Added foreign key constraints for data integrity
2024-01-20: Initial schema snapshot (v4.0.0)
π Change Statistics:
Tables: +3 (42 β 45)
Columns: +23 (272 β 295)
Indexes: +8 (64 β 72)
Avg changes per week: 2.1
Example 5: Visual schema comparison
python3 scripts/main.py visual-diff schema_v1.sql schema_v2.sql --html schema_diff.html
Output:
β¨ Generated visual diff: schema_diff.htmlOpen in browser to see:
Side-by-side schema comparison
Color-coded differences (added/removed/changed)
Interactive expand/collapse for tables
Export options for documentation Differences highlighted:
β
5 tables added (green)
β 2 tables removed (red)
π 12 columns modified (yellow)
Requirements
Install dependencies:
pip3 install sqlalchemy alembic psycopg2-binary pymysql
Limitations
Directory Structure
The tool works with database connection strings, SQL files, or schema snapshot files. No special configuration directories are required.
Error Handling
Contributing
This is a skill built by the Skill Factory. Issues and improvements should be reported through the OpenClaw project.
β‘ When to Use
π‘ Examples
Example 1: Compare development and production databases
python3 scripts/main.py compare \
postgresql://dev_user:dev_pass@localhost/dev_db \
postgresql://prod_user:prod_pass@prod-host/prod_db \
--output diff-report.json
Output:
π Comparing schemas: dev_db (localhost) vs prod_db (prod-host)π Summary:
Tables: 42 vs 45 (3 missing in dev)
Columns: 287 vs 295 (8 differences)
Indexes: 67 vs 72 (5 differences)
Constraints: 34 vs 38 (4 differences) β οΈ Differences found (15):
1. Table audit_logs missing in dev
β CREATE TABLE audit_logs (...)
2. Column users.email_verified missing in dev
β ALTER TABLE users ADD COLUMN email_verified BOOLEAN DEFAULT FALSE
3. Index idx_users_email missing in prod
β CREATE INDEX idx_users_email ON users(email)
4. Constraint fk_orders_customer_id differs
β ALTER TABLE orders DROP CONSTRAINT fk_orders_customer_id_old;
β ALTER TABLE orders ADD CONSTRAINT fk_orders_customer_id FOREIGN KEY ...
β
Generated migration: diff-report.json
β
SQL migration script: migration_20240306_143022.sql
Example 2: Generate migration script
python3 scripts/main.py diff old_schema.sql new_schema.sql --format sql --output migration.sql
Output (migration.sql):
-- Generated: 2024-03-06 14:30:22
-- Database: PostgreSQL-- UP Migration
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 idx_users_email ON users(email);
ALTER TABLE orders
DROP CONSTRAINT fk_orders_customer_id_old,
ADD CONSTRAINT fk_orders_customer_id
FOREIGN KEY (customer_id) REFERENCES customers(id)
ON DELETE CASCADE;
-- DOWN Migration (rollback)
DROP TABLE IF EXISTS audit_logs;
ALTER TABLE users DROP COLUMN IF EXISTS email_verified;
DROP INDEX IF EXISTS idx_users_email;
ALTER TABLE orders
DROP CONSTRAINT fk_orders_customer_id,
ADD CONSTRAINT fk_orders_customer_id_old
FOREIGN KEY (customer_id) REFERENCES customers(id);
Example 3: Check for schema drift in CI
python3 scripts/main.py check-drift \
--expected schemas/expected/prod.json \
--actual schemas/actual/prod.json \
--fail-on-drift
Output (CI failure):
β Schema drift detected!Differences:
1. Unexpected table temp_backup in production
2. Missing index idx_orders_status in production
3. Column users.last_login has different type (TIMESTAMP vs TIMESTAMPTZ)
Exit code: 1 (failed due to --fail-on-drift)
Example 4: Track schema evolution
python3 scripts/main.py history postgresql://user:pass@host/db --days 90 --format timeline
Output:
π
Schema Evolution Timeline (last 90 days)2024-03-05: Added audit_logs table (v4.2.0 release)
2024-02-28: Added email_verified column to users table
2024-02-15: Created indexes for performance optimization
2024-02-01: Added foreign key constraints for data integrity
2024-01-20: Initial schema snapshot (v4.0.0)
π Change Statistics:
Tables: +3 (42 β 45)
Columns: +23 (272 β 295)
Indexes: +8 (64 β 72)
Avg changes per week: 2.1
Example 5: Visual schema comparison
python3 scripts/main.py visual-diff schema_v1.sql schema_v2.sql --html schema_diff.html
Output:
β¨ Generated visual diff: schema_diff.htmlOpen in browser to see:
Side-by-side schema comparison
Color-coded differences (added/removed/changed)
Interactive expand/collapse for tables
Export options for documentation Differences highlighted:
β
5 tables added (green)
β 2 tables removed (red)
π 12 columns modified (yellow)