Skip to content

Instantly share code, notes, and snippets.

@Langmans
Created March 16, 2016 16:00
Show Gist options
  • Select an option

  • Save Langmans/5cc01ed9b85ef5982477 to your computer and use it in GitHub Desktop.

Select an option

Save Langmans/5cc01ed9b85ef5982477 to your computer and use it in GitHub Desktop.
database migrator source DB (altered tables or columns) > target DB
{
"config": {
"platform": {
"php": "5.3.10"
}
},
"require": {
"doctrine/dbal": "^2.5",
"jdorn/sql-formatter": "^1.2"
}
}
<?php
require 'vendor/autoload.php';
use Doctrine\DBAL\DriverManager;
$source = DriverManager::getConnection(array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'user' => 'xxx',
'password' => 'xxx',
'dbname' => 'db1',
));
// schema that needs to update its structure from $source
$target = DriverManager::getConnection(array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'user' => 'xxx',
'password' => 'xxx',
'dbname' => 'db2',
));
// add type mappings when needed.
$sourcePlatform = $source->getDatabasePlatform();
$sourcePlatform->registerDoctrineTypeMapping('enum', 'string');
$targetPlatform = $target->getDatabasePlatform();
$targetPlatform->registerDoctrineTypeMapping('enum', 'string');
$sourceSchema = $source->getSchemaManager()->createSchema();
$targetSchema = $target->getSchemaManager()->createSchema();
$sql = $sourceSchema->getMigrateToSql($targetSchema, $targetPlatform);
array_unshift($sql, 'START TRANSACTION');
$sql[] = 'COMMIT';
foreach ($sql as $c) {
echo SqlFormatter::format($c.';');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment