DBDiff

DBDiff logo

Build Status Total Downloads Monthly Downloads License

DBDiff is an automated database schema and data diff tool. It compares two databases, local or remote, and produces a migration file of the differences automatically.

When used alongside a compatible database migration tool, it can help enable database version control within your team or enterprise.

Supporting DBDiff

DBDiff is a MIT-licensed open source project with its ongoing development made possible entirely by the support of backers. For getting a mention in return, please consider:

Features

Pre-requisites

  1. You will need to have access to the command-line, for Linux/Mac a Terminal or on Windows it will be a command prompt (cmd)
  2. You will need to have git installed: http://git-scm.com/downloads
  3. You will need to have PHP installed (version 5.4.x): http://php.net/manual/en/install.php
  4. You will need to have Composer installed which is a Dependency Manager for PHP: https://getcomposer.org

Note: Make a note of where composer.phar is installed as we will need it later on during Setup

Installation

On the command-line, use git to clone the ssh version:

git clone git@github.com:DBDiff/DBDiff.git

Or use git to clone the https version:

git clone https://github.com/DBDiff/DBDiff.git

Or download the .zip archive and unzip it to a folder of your choosing e.g. dbdiff:

https://github.com/DBDiff/DBDiff/archive/master.zip

Or use composer to include DBDiff as a project dependency:

php composer.phar require "dbdiff/dbdiff:@dev"

Or use composer to install DBDiff globally:

composer global require "dbdiff/dbdiff:@dev"

Create a PHAR build

Please first ensure in your php.ini file the phar.readonly setting is set to false , for example:

[Phar]
; http://php.net/phar.readonly
phar.readonly = false

Then in the root of the dbdiff repository to produce a Phar build simply run:

$ ./build

A dist folder should be created containing the following files:

Feel free to rename dbdiff.phar to dbdiff and move it to /usr/local/bin or another directory of your choice.

You can also add it to your system’s path if you wish to make it globally available on your system as a utility.

Setup

Make sure you are in the root of your application for all the following steps, using ‘cd’ to navigate on the command line to where you have placed your “dbdiff” folder

We are going to assume that composer.phar is installed inside your “dbdiff” folder. If it is installed elsewhere you will need to use it’s exact path

  1. If you didn’t install DBDiff with composer, install the dependencies of the project with: php composer.phar install
  2. Make a .dbdiff file by following the File Examples and place it in the root of your “dbdiff” directory
  3. Type ./dbdiff {dbdiff command here e.g. server1.db1:server1.db2} to start the app! See Command-Line API for more details on which commands you can run.

You should see something like…

ℹ Now calculating schema diff for table `foo`
ℹ Now calculating data diff for table `foo`
ℹ Now generating UP migration
ℹ Now generating DOWN migration
ℹ Writing migration file to /path/to/dbdiff/migration.sql
✔ Completed

Congratulations you have installed and ran DBDiff!

Command-Line API

Note: The command-line parameters will always override the settings in the .dbdiff config file

Usage Examples

Example 1

$ ./dbdiff server1.db1:server2.db2

This would by default look for the .dbdiff config file for the DB connection details, if it’s not there the tool would return an error. If it’s there, the connection details would be used to compare the SQL of only the schema and output a commented migration.sql file inside the current directory which includes only the up SQL as per default

Example 2

$ ./dbdiff server1.development.table1:server2.production.table1 --nocomments=true --type=data

This would by default look for the .dbdiff config file for the DB connection details, if it’s not there the tool would return an error. If it’s there, the connection details would be used to compare the SQL of only the data of the specified table1 inside each database and output a .sql file which has no comments inside the current directory which includes only the up SQL as per default

Example 3

$ ./dbdiff --config=config.conf --template=templates/simple-db-migrate.tmpl --include=all server1.db1:server2.db2 --output=./sql/simple-schema.sql

Instead of looking for .dbdiff, this would look for config.conf (which should be valid YAML) for the settings, and then override any of those settings from config.conf for the –template and –include parameters given in the command-line parameters - thus comparing the source with the target database and outputting an SQL file called simple-schema.sql to the ./sql folder, which should already exist otherwise the program will throw an error, and which includes only the schema as an up and down SQL diff in the simple-db-migrate format (as specified by the template). This example would work perfectly alongside the simple-db-migrate tool

File Examples

.dbdiff

server1:
	user: user
	password: password
	port: port # for MySQL this is 3306
	host: host1 # usually localhost or 127.0.0.1
server2:
	user: user
	password: password
	port: port # for MySQL this is 3306
	host: host1 # usually localhost or 127.0.0.1
template: templates/simple-db-migrate.tmpl
type: all
include: all
nocomments: true
tablesToIgnore:
- table1
- table2
- table3
fieldsToIgnore:
	table1:
		- field1
		- field2
		- field3
	table4:
		- field1
		- field4

simple-db-migrate.tmpl

SQL_UP = u"""

"""
SQL_DOWN = u"""

"""

How Does the Diff Actually Work?

The following comparisons run in exactly the following order:

Overall Comparison

Schema Comparison

Data Comparison

Compatible Migration Tools

Project Language / Package Manager Description
Simple DB Migrate Python / PIP Generic database migration tool inpired on Rails migrations
Flyway Java / Maven Database Migrations Made Easy

Please do let us know if you’re using any other migration tools with DBDiff, other than the ones listed here, so we can add it.

Questions & Support :thought_balloon:

Backers :two_hearts:

Sean McNamara

Back DBDiff on Patreon and have your name or logo displayed prominently here!

Contributions :revolving_hearts:

Please make sure to read the Contributing Guide before making a pull request.

Thank you to all the people who already contributed to DBDiff!

Feedback :speech_balloon:

If you’ve made it down here, you’re probably a fan :wink:

Could you please kindly spare 2 minutes to give us your feedback on DBDiff:

https://forms.gle/gjdJxZxdVsz7BRxg7

We read each and every suggestion that comes through.

License

MIT

Made with  :heart:  by   Akal Software logo