Created
March 16, 2016 16:00
-
-
Save Langmans/5cc01ed9b85ef5982477 to your computer and use it in GitHub Desktop.
database migrator source DB (altered tables or columns) > target DB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "config": { | |
| "platform": { | |
| "php": "5.3.10" | |
| } | |
| }, | |
| "require": { | |
| "doctrine/dbal": "^2.5", | |
| "jdorn/sql-formatter": "^1.2" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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