Skip to main content

diff

The diff command compares your current database schema (JSON file from extract) with your desired schema (SQL files) and displays the differences. Use this to preview what changes will be made before generating migrations.

Usage

pgtofu diff [flags]

Flags

FlagDescriptionRequired
--currentPath to current schema JSON file (from extract)Yes
--desiredPath to desired schema SQL file or directoryYes
--help, -hHelp for diffNo

Examples

Basic Comparison

# Compare with a directory of SQL files
pgtofu diff --current current-schema.json --desired ./schema

# Compare with a single SQL file
pgtofu diff --current current-schema.json --desired schema.sql

Docker

docker run --rm \
  -v "$(pwd):/workspace" \
  -w /workspace \
  accented/pgtofu:latest diff \
  --current current-schema.json \
  --desired ./schema

Output Format

The diff command outputs a summary of detected changes grouped by severity:
Schema Comparison Summary
========================

Changes detected: 8

SAFE Changes (5):
  - ADD_TABLE: Add table: public.users
  - ADD_TABLE: Add table: public.orders
  - ADD_INDEX: Add index: idx_users_email on public.users
  - ADD_INDEX: Add index: idx_orders_user_id on public.orders
  - ADD_EXTENSION: Add extension: uuid-ossp

POTENTIALLY_BREAKING Changes (2):
  - DROP_INDEX: Drop index: idx_old_unused on public.legacy
  - MODIFY_COLUMN_DEFAULT: Change default for: public.users.status

BREAKING Changes (1):
  - DROP_TABLE: Drop table: public.deprecated_table

Total: 8 changes
  Safe: 5
  Potentially Breaking: 2
  Breaking: 1

Change Severities

pgtofu classifies all detected changes by their potential impact:

SAFE

Low risk - Additions and non-destructive changes
  • Adding tables, columns, indexes
  • Adding constraints
  • Creating views and functions

POTENTIALLY_BREAKING

Medium risk - May affect queries or applications
  • Dropping unused indexes
  • Changing defaults
  • Renaming objects

BREAKING

High risk - May cause data loss or downtime
  • Dropping tables or columns
  • Dropping constraints
  • Removing functions used by applications

DATA_MIGRATION_REQUIRED

Requires intervention - Cannot be automated
  • Changing column types incompatibly
  • Adding NOT NULL to existing columns without defaults
  • Complex schema transformations

Change Types

pgtofu detects over 40 different types of schema changes:
Change TypeSeverityDescription
ADD_TABLESAFENew table created
DROP_TABLEBREAKINGTable removed
MODIFY_TABLE_COMMENTSAFETable comment changed
Change TypeSeverityDescription
ADD_COLUMNSAFENew column added
DROP_COLUMNBREAKINGColumn removed
MODIFY_COLUMN_TYPEVariesColumn type changed
MODIFY_COLUMN_NULLABILITYVariesNULL/NOT NULL changed
MODIFY_COLUMN_DEFAULTPOTENTIALLY_BREAKINGDefault value changed
Change TypeSeverityDescription
ADD_CONSTRAINTSAFENew constraint added
DROP_CONSTRAINTPOTENTIALLY_BREAKINGConstraint removed
MODIFY_CONSTRAINTPOTENTIALLY_BREAKINGConstraint modified
Change TypeSeverityDescription
ADD_INDEXSAFENew index created
DROP_INDEXPOTENTIALLY_BREAKINGIndex removed
MODIFY_INDEXPOTENTIALLY_BREAKINGIndex definition changed
Change TypeSeverityDescription
ADD_VIEWSAFENew view created
DROP_VIEWPOTENTIALLY_BREAKINGView removed
MODIFY_VIEWPOTENTIALLY_BREAKINGView definition changed
Change TypeSeverityDescription
ADD_FUNCTIONSAFENew function created
DROP_FUNCTIONPOTENTIALLY_BREAKINGFunction removed
MODIFY_FUNCTIONPOTENTIALLY_BREAKINGFunction body changed
Change TypeSeverityDescription
ADD_HYPERTABLESAFETable converted to hypertable
ADD_COMPRESSION_POLICYSAFECompression policy added
ADD_RETENTION_POLICYSAFERetention policy added
ADD_CONTINUOUS_AGGREGATESAFEContinuous aggregate created

Type Compatibility

When comparing column types, pgtofu understands safe type widening:

Safe Widening (No Data Loss)

smallint → integer → bigint
VARCHAR(50) → VARCHAR(100)
NUMERIC(10,2) → NUMERIC(12,2)

Incompatible Changes (Data Migration Required)

bigint → smallint (narrowing)
TEXT → INTEGER (type change)
VARCHAR(100) → VARCHAR(50) (length reduction)

Desired Schema Format

The desired schema can be a single SQL file or a directory structure:

Single File

pgtofu diff --current current.json --desired schema.sql
schema/
├── extensions/
│   └── uuid-ossp.sql
├── types/
│   └── enums.sql
├── tables/
│   ├── users.sql
│   └── orders.sql
├── views/
│   └── reports.sql
├── functions/
│   └── utilities.sql
└── timescaledb/
    └── policies.sql
pgtofu automatically discovers and parses all .sql files in the directory tree.
Organize SQL files by type for maintainability. pgtofu resolves dependencies automatically regardless of file organization.

Troubleshooting

Ensure your SQL files use the same schema names as the database. By default, PostgreSQL uses the public schema.
Check that your SQL syntax is valid. pgtofu supports standard PostgreSQL syntax including:
  • Dollar-quoted strings
  • Quoted identifiers
  • Complex expressions
View normalization may detect changes due to formatting differences. Use --preview with generate to see exact SQL.

See Also