Compare two databases
Published 18 July 2024
You can compare the schema in two different databases using the Flyway CLI.
This is available for SQL Server, Oracle, PostgreSQL, and MySQL databases.
Step-by-step guide
Option 1: Take two snapshots and generate a changes report
- Take a Snapshot of your source database.
flyway snapshot -url=<jdbc:example:database_source> -user=<username> -password=<password> -snapshot.filename=<C:\snapshots\snapshot_source>
- Take a Snapshot of your target database.
flyway snapshot -url=<jdbc:example:database_target> -user=<username> -password=<password> -snapshot.filename=<C:\snapshots\snapshot_target>
- Use the Check Changes report to compare the two snapshots and identify any differences between the two database schema snapshots.
flyway check -changes -check.nextSnapshot="snapshot_source" -check.deployedSnapshot="snapshot_target"
Option 2: Use the flyway diff in combination with flyway prepare to generate a deployment script.
- Run a flyway diff, which will output a diff to the console.Or alternatively, you can diff the snapshots created in option 1.
flyway diff (specifying databases directly)
flyway diff "-environments.development.url=jdbc:sqlserver://localhost:1433;encrypt=false;databaseName=Dev" "-environments.development.user=sa" "-environments.development.password=..." "-environments.prod.url=jdbc:sqlserver://localhost:1433;encrypt=false;databaseName=Prod" "-environments.prod.user=sa" "-environments.prod.password=..." "-diff.source=development" "-diff.target=prod"
flyway diff (snapshots)
flyway diff -diff.source="snapshot:snapshot_source" -diff.target="snapshot:snapshot_target"
- Run flyway prepare to generate a deployment script (named D__deployment.sql by default), which when applied will make the target environment the same as the source.
flyway prepare
flyway prepare