DBAtlas · Database operations atlas

Diff two schemas/environments (schema diff)

Oracle

-- Extract the schema DDL from both environments and take a text diff
-- (generate deterministic DDL with DBMS_METADATA)
EXEC DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SQLTERMINATOR',TRUE);
EXEC DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',FALSE);
SELECT DBMS_METADATA.GET_DDL('TABLE', table_name) FROM user_tables ORDER BY table_name;
-- Spool the output to a file in both environments and compare with diff/fc.

-- Export the schema as a changelog with SQLcl's Liquibase integration:
-- SQL> lb generate-schema
-- Commercial: the Oracle SQL Developer 'Database Diff' tool is also used.

Oracle has no built-in single-command schema diff. The common method is to generate deterministic DDL from both environments with DBMS_METADATA.GET_DDL (with STORAGE/segment attributes turned off) and take a text diff. For visual/automated comparison, use SQL Developer's Database Diff wizard or SQLcl.

PostgreSQL

# Dump only the schema from both databases and diff them
pg_dump --schema-only --no-owner --no-privileges sourcedb > source_schema.sql
pg_dump --schema-only --no-owner --no-privileges targetdb  > target_schema.sql
diff -u source_schema.sql target_schema.sql

# Tool that generates applicable ALTERs: apgdiff (Another PostgreSQL Diff Tool)
# apgdiff source_schema.sql target_schema.sql > migration.sql
# Alternatives: migra, Liquibase diffChangeLog

The simplest way is to take pg_dump --schema-only output in both environments and compare it with diff. apgdiff and migra are tools that turn the difference directly into executable ALTER statements. Add --no-owner --no-privileges for deterministic output.

MySQL

# Schema/object comparison with MySQL Utilities (5.x):
mysqldbcompare --server1=root:password@host1 --server2=root:password@host2 \
  shop:shop --difftype=sql --run-all-tests

# Built-in method: take CREATE statements from both environments and diff them
mysqldump --no-data --skip-comments shop > source_schema.sql
# SHOW CREATE TABLE customer;  (to see a single table definition)

The mysqldbcompare tool from MySQL Utilities reports object/schema differences between two servers (as applicable statements with difftype=sql). The tool-free method is to take mysqldump --no-data (schema only) or SHOW CREATE TABLE output from both environments and compare with diff. mysqldbcompare is now deprecated; for MariaDB/8.x, Liquibase or Skeema is preferred.

SQL Server

-- Schema comparison/deploy report with SqlPackage (what will change before applying):
sqlpackage /Action:DeployReport /SourceFile:source.dacpac \
  /TargetConnectionString:"Server=target;Database=shop;Integrated Security=true" \
  /OutputPath:diff_report.xml

-- For data differences (row level), the tablediff utility:
tablediff -sourceserver S1 -sourcedatabase shop -sourcetable customer \
  -destinationserver S2 -destinationdatabase shop -destinationtable customer

For schema differences, SqlPackage /Action:DeployReport returns the changes to be applied to the target as an XML report; /Action:Script generates an executable diff script. Visual Studio / Azure Data Studio 'Schema Compare' uses the same engine. For row-level data differences there is the tablediff command-line utility.

MongoDB

// The schema is implicit; extract and compare the field structure of both environments.
// Read the $jsonSchema validator definition (collection rules):
db.getCollectionInfos({ name: "customer" })[0].options.validator

// External tools for inferred schema comparison by sampling:
// mongodb-schema (npm) or the Compass 'Schema' tab
// Take samples with mongoexport and diff the field/type distribution of both environments.

MongoDB is schemaless; there is no fixed DDL. Comparison is done in two ways: compare the $jsonSchema validator rules defined on the collection (via getCollectionInfos), or generate an inferred schema from the data with mongodb-schema / Compass and compare field and type distributions. For indexes, diff the db.collection.getIndexes() output.

Redis

# Not applicable: Redis is a schemaless key-value store; there are no
# table/column definitions (DDL) to compare.
# Closest equivalent: compare the key space (namespace) structure
# redis-cli --scan --pattern 'customer:*' | sort > source_keys.txt

Not applicable: Redis has no schema concept (table/column/DDL), so a classic schema diff does not apply. If a structural comparison is needed, only the key naming pattern (namespace) can be listed with SCAN and compared.

Elasticsearch

# Fetch the index mapping from both clusters and compare
curl -s 'http://source:9200/customer/_mapping?pretty' > source_mapping.json
curl -s 'http://target:9200/customer/_mapping?pretty'  > target_mapping.json
diff -u source_mapping.json target_mapping.json

# To compare settings as well:
# GET customer/_settings   and   GET customer/_mapping

In Elasticsearch the equivalent of a schema is the index mapping. The standard method is to take the GET <index>/_mapping output (and _settings if needed) from both clusters and compare with diff. Because mapping fields are mostly immutable, a detected difference may require a new index + _reindex.

Open in DBAtlas →