Created
September 4, 2013 15:22
-
-
Save mac2000/6438535 to your computer and use it in GitHub Desktop.
Doctrine Migrations Example, without command line console aplication, that could be run from command line and from web browser
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
{ | |
"require": { | |
"doctrine/dbal": "*", | |
"doctrine/migrations": "dev-master" | |
}, | |
"minimum-stability": "dev", | |
"autoload": { | |
"psr-0": {"": "src/"} | |
} | |
} |
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 | |
use Doctrine\DBAL\DriverManager; | |
use Doctrine\DBAL\Migrations\Configuration\Configuration; | |
use Doctrine\DBAL\Migrations\Migration; | |
require_once 'vendor/autoload.php'; | |
$db = DriverManager::getConnection(array( | |
'dbname' => 'doctine_migrations', | |
'user' => 'root', | |
'password' => 'root', | |
'host' => 'localhost', | |
'driver' => 'pdo_mysql', | |
'charset' => 'utf8', | |
'driverOptions' => array( | |
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' | |
) | |
)); | |
$config = new Configuration($db); | |
$config->setMigrationsTableName('version'); | |
$config->setMigrationsNamespace('Acme\\Migrations'); | |
$config->setMigrationsDirectory('src/Acme/Migrations'); | |
$config->registerMigrationsFromDirectory($config->getMigrationsDirectory()); | |
$migration = new Migration($config); | |
$migration->migrate(); |
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 # index.php | |
use Doctrine\DBAL\DriverManager; | |
use Doctrine\DBAL\Migrations\Configuration\Configuration; | |
use Doctrine\DBAL\Migrations\Migration; | |
use Doctrine\DBAL\Migrations\OutputWriter; | |
require_once 'vendor/autoload.php'; | |
$nl = PHP_SAPI == 'cli' ? PHP_EOL : '<br>'; // Optional will be used for output | |
$to = null; // Optional integer - migrate to version, if null - will migrate to latest available version | |
#region Optional get argument | |
$index = PHP_SAPI == 'cli' ? 1 : 'to'; | |
$arguments = PHP_SAPI == 'cli' ? $argv : $_REQUEST; | |
$to = isset($arguments[$index]) && filter_var($arguments[$index], FILTER_VALIDATE_INT) ? intval($arguments[$index]) : null; | |
#endregion | |
#region Doctrine Connection | |
// Silex: $app['db'] | |
// Symfony controller: $this->get('database_connection') | |
$db = DriverManager::getConnection(array( | |
'dbname' => 'doctine_migrations', | |
'user' => 'root', | |
'password' => 'root', | |
'host' => 'localhost', | |
'driver' => 'pdo_mysql', | |
'charset' => 'utf8', | |
'driverOptions' => array( | |
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' | |
) | |
)); | |
#endregion | |
#region Config | |
$config = new Configuration($db /*, new OutputWriter(function ($message) { echo $message . PHP_EOL; })*/); // OutputWriter is optional and by default do nothing, accepts closure for writing logs | |
//$config->setName('My Migrations'); // Optional name for your migrations | |
$config->setMigrationsTableName('version'); // Table name that will store migrations log (will be created automatically, default name is: doctrine_migration_versions) | |
$config->setMigrationsNamespace('Acme\\Migrations'); // Namespace of your migration classes, do not forget escape slashes, do not add last slash | |
$config->setMigrationsDirectory('src/Acme/Migrations'); // Directory where your migrations are located | |
$config->registerMigrationsFromDirectory($config->getMigrationsDirectory()); // Load your migrations | |
#endregion | |
$migration = new Migration($config); // Create Migration based on provided configuration | |
$versions = $migration->getSql($to); // Retrieve SQL queries that should be run to migrate you schema to $to version, if $to == null - schema will be migrated to latest version | |
#region Some dummy output | |
foreach ($versions as $version => $queries) { | |
echo 'VERSION: ' . $version . $nl; | |
echo '----------------------------------------------' . $nl . $nl; | |
foreach ($queries as $query) { | |
echo $query . $nl . $nl; | |
} | |
echo $nl . $nl; | |
} | |
#endregion | |
try { | |
$migration->migrate($to); // Execute migration! | |
echo 'DONE' . $nl; | |
} catch (Exception $ex) { | |
echo 'ERROR: ' . $ex->getMessage() . $nl; | |
} |
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 # src/Acme/Migrations/Version1.php | |
namespace Acme\Migrations; | |
use Doctrine\DBAL\Migrations\AbstractMigration; | |
use Doctrine\DBAL\Schema\Schema; | |
/** | |
* Class Version1 | |
* | |
* Notice that file and class names MUST be Version*.php | |
* | |
* @package Acme\Migrations | |
*/ | |
class Version1 extends AbstractMigration | |
{ | |
public function up(Schema $schema) | |
{ | |
$users = $schema->createTable('users'); | |
$users->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true)); | |
$users->addColumn('username', 'string', array('length' => 128)); | |
$users->addColumn('password', 'string', array('length' => 128)); | |
$users->setPrimaryKey(array('id')); | |
//$this->addSql('CREATE TABLE addresses (id INT NOT NULL, street VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB'); | |
} | |
public function down(Schema $schema) | |
{ | |
$schema->dropTable('users'); | |
//$this->addSql('DROP TABLE addresses'); | |
} | |
// Other helpful functions | |
//public function preUp(Schema $schema) {} | |
//public function postUp(Schema $schema) {} | |
//public function preDown(Schema $schema) {} | |
//public function postDown(Schema $schema) {} | |
} |
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 # src/Acme/Migrations/Version2.php | |
namespace Acme\Migrations; | |
use Doctrine\DBAL\Migrations\AbstractMigration; | |
use Doctrine\DBAL\Schema\Schema; | |
class Version2 extends AbstractMigration { | |
public function up(Schema $schema) | |
{ | |
$posts = $schema->createTable('posts'); | |
$posts->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true)); | |
$posts->addColumn('title', 'string', array('length' => 256)); | |
$posts->addColumn('content', 'text'); | |
$posts->setPrimaryKey(array('id')); | |
} | |
public function down(Schema $schema) | |
{ | |
$schema->dropTable('posts'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment