comparing-database-schemas
Process use when you need to work with schema comparison. This skill provides database schema diff and sync with comprehensive guidance and automation. Trigger with phrases like "compare schemas", "diff databases", or "sync database schemas".
Allowed Tools
Provided by Plugin
database-diff-tool
Database plugin for database-diff-tool
Installation
This skill is included in the database-diff-tool plugin:
/plugin install database-diff-tool@claude-code-plugins-plus
Click to copy
Instructions
Database Diff Tool
Overview
Compare database schemas between two environments (development vs. staging, staging vs.
Prerequisites
- Connection credentials to both source and target databases
psqlormysqlCLI configured to connect to both environments- Read access to
informationschemaandpgcatalog(PostgreSQL) orinformation_schema(MySQL) - Permission to run
pg_dump --schema-onlyfor full schema extraction - Understanding of which environment is the "source of truth" (typically the migration-managed environment)
Instructions
- Extract the full schema from both databases for comparison:
- PostgreSQL:
pgdump --schema-only --no-owner --no-privileges -f schemasource.sql sourcedband repeat for targetdb - MySQL:
mysqldump --no-data --routines --triggers sourcedb > schemasource.sql - Alternatively, query
information_schemadirectly for programmatic comparison
- Compare tables present in each database:
SELECT tablename FROM informationschema.tables WHERE tableschema = 'public' AND tablecatalog = 'sourcedb' EXCEPT SELECT tablename FROM informationschema.tables WHERE tableschema = 'public' AND tablecatalog = 'targetdb'- This reveals tables that exist in source but not in target (and vice versa)
- Compare columns for each shared table:
- Query
informationschema.columnsfrom both databases for:columnname,datatype,charactermaximumlength,isnullable,columndefault,ordinalposition - Flag differences in data type, nullability, default values, and column ordering
- Detect added columns (in source, not target) and dropped columns (in target, not source)
- Compare indexes:
- PostgreSQL: Query
pg_indexesforindexname,indexdefon each database - MySQL: Query
informationschema.STATISTICSforINDEXNAME,COLUMNNAME,NONUNIQUE - Flag missing, extra, or differently-defined indexes
- Compare constraints (primary keys, foreign keys, unique, check):
- Query
informationschema.tableconstraintsandinformationschema.keycolumn_usage - Detect missing foreign keys, changed constraint names, and altered check constraint expressions
- Compare functions, stored procedures, and triggers:
- PostgreSQL: Query
pgprocfor function signatures andpgtriggerfor trigger definitions - MySQL: Query
informationschema.ROUTINESandinformationschema.TRIGGERS - Compare function bodies for logical differences
- Compare enum types and custom types (PostgreSQL):
- Query
pgtypeandpgenumfor enum label differences - Detect added or removed enum values (note: PostgreSQL only supports adding enum values, not removing)
- Generate a structured diff report categorizing differences as:
- Added: Objects in source not present in target (require CREATE statements)
- Removed: Objects in target not present in source (require DROP statements, confirm intentional)
- Modified: Objects differing between source and target (require ALTER statements)
- Generate migration SQL to synchronize the target database to match the source:
- CREATE TABLE for new tables, ALTER TABLE ADD COLUMN for new columns
- ALTER TABLE ALTER COLUMN for type changes, ALTER TABLE DROP COLUMN for removed columns
- CREATE INDEX / DROP INDEX for index differences
- Include transaction wrapping and rollback-safe operations
- Validate the generated migration by applying it to a copy of the target database and re-running the diff. The second diff should report zero differences, confirming the migration produces the expected state.
Output
- Schema diff report listing all differences categorized by type (added, removed, modified)
- Migration SQL script to synchronize target schema to match source
- Rollback SQL script to reverse the migration if needed
- Side-by-side comparison of differing object definitions
- Drift detection summary highlighting changes not tracked in migration files
Error Handling
| Error | Cause | Solution |
|---|---|---|
| Connection refused to one database | Network or credential issue on source or target | Verify connection strings; check firewall rules; confirm credentials work with direct psql or mysql connection |
Permission denied on pg_catalog queries |
User lacks read access to system catalogs | Grant pgreadallsettings role; or use pgdump --schema-only which requires fewer privileges |
| False positive differences from default value formatting | PostgreSQL normalizes default expressions differently in different versions | Normalize default value strings before comparison; ignore whitespace differences; compare semantic equivalence |
| Enum type modification blocked | PostgreSQL does not support removing enum values or reordering | Create a new enum type, migrate the column, drop the old type; document this as a multi-step migration |
| Generated migration fails on target | Target has data that violates new constraints | Add data validation queries before constraint creation; backfill default values; handle edge cases in migration |
Examples
Detecting schema drift between staging and production: After 3 months without auditing, the diff reveals: 2 columns added to production manually (not in migrations), 1 index missing from staging, and 3 functions with different implementations. A migration script is generated to bring staging in sync, and the manual production changes are backported into migration files.
Pre-deployment schema validation: Before deploying a release with 5 migration files, run the diff between the post-migration staging schema and the expected schema. The diff catches a migration that accidentally dropped a constraint that a later migration depends on. The migration ordering is fixed before production deployment.
Comparing PostgreSQL schemas across major version upgrade: Schema extracted from PostgreSQL 14 and compared against PostgreSQL 16 after migration. Diff reveals function signature changes for built-in function calls, updated default values for new parameters, and deprecated syntax in stored procedures. Migration script updates function definitions for the new version.
Resources
- PostgreSQL information_schema: https://www.postgresql.org/docs/current/information-schema.html
- MySQL information_schema: https://dev.mysql.com/doc/refman/8.0/en/information-schema.html
- pg_dump schema-only mode: https://www.postgresql.org/docs/current/app-pgdump.html
- migra (PostgreSQL schema diff tool): https://github.com/djrobstep/migra
- Skeema (MySQL schema management): https://www.skeema.io/