Database Diff Utils
If you're looking for an all-in-one database diff tool, checkout DB Diff, (*1)
Intro
A simple set of utilities to be used for generating a diff between 2 database connections and their structures (not data)., (*2)
Also contains a Laravel service provider and console command that can diff 2 given databases., (*3)
Installation
Install through composer., (*4)
composer require pm-connect/db-diff-utils
Optional Add The Laravel Service Provider
Add the following line to your config/app.php
file within the providers array if you wish to use the provided artisan command., (*5)
...
PMConnect\DBDiff\Utils\DBDiffUtilServiceProvider::class,
...
Usage
The provided diff utilities can be used in 2 ways., (*6)
- Using the provided objects where you need them.
- Using the provided artisan console command
db:diff
.
Using The Provided Objects
The package provides the following objects for your use:, (*7)
PMConnect\DBDiff\Utils\Diff
This object provides a basic wrapper to the other available objects and is capable of running a full diff., (*8)
Example Use
The Diff
object accepts in the following as constructor parameters., (*9)
-
PMConnect\DBDiff\Utils\Contracts\Output
- Create an implementation of this to save the output of the diff.
-
Illuminate\Database\Connection
- Provide connection instances to the databases you wish to diff.
- This can be done easily using
Illuminate\Database\Connectors\ConnectionFactory
- This can also be done by manually creating instances of the connections you wish to use.
Illuminate\Database\MySqlConnection
Illuminate\Database\PostgresConnection
Illuminate\Database\SQLiteConnection
<?php
use PMConnect\DBDiff\Utils\Diff;
use Illuminate\Database\MySqlConnection;
$config1 = [
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '3306,
'database' => 'some_database',
'collation' => 'utf8mb4_general_ci',
'username' => 'username',
'password' => 'password'
];
$config2 = [
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '3306,
'database' => 'some_other_database',
'collation' => 'utf8mb4_general_ci',
'username' => 'username',
'password' => 'password'
];
// Connect to the required databases.
$dsn1 = "{$config1['driver']}:host={$config1['host']};port={$config1['port']};dbname={$config1['database']}";
$pdo1 = new PDO($dsn1, $config1['username'], $config1['password']);
$dsn2 = "{$config2['driver']}:host={$config2['host']};port={$config2['port']};dbname={$config2['database']}";
$pdo2 = new PDO($dsn2, $config2['username'], $config2['password']);
$connection1 = new MySqlConnection($pdo1, $config1['database'], $prefix = '', $config1);
$connection2 = new MySqlConnection($pdo2, $config2['database'], $prefix = '', $config2);
// Create an instance of your Output implementation to inject.
$output = new YourImplementationOfOutputContract;
// Create a diff instance with the required injections.
$diff = new Diff(
$output,
$connection1,
$connection2
);
// Run the diff
$diff->diff();
Using The Laravel Artisan Console Command
This package provides an artisan console command that can diff 2 given databases for you and output the results into the console., (*10)
Provided you have added the service provider (as mentioned above) you can use the command as follows:, (*11)
>>> php artisan db:diff --default-collation=utf8mb4_general_ci
This command will automatically prompt for the database connection details from you, then run the diff and output any errors to the screen., (*12)
The --default-collation
option is optional and will default to 'utf8mb4_general_ci' if not provided, but can still be provided during the config of each database., (*13)
Issues
Please submit any issues using GitHubs build in issue management., (*14)