Last active
April 22, 2016 12:49
-
-
Save JeffPeters/7535348 to your computer and use it in GitHub Desktop.
Example using Doctrine Migration with a Custom Configuration File from code in https://github.com/JeffPeters/migrations/tree/custom-configuration-class
This file contains 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 | |
class CustomConfiguration extends \Doctrine\DBAL\Migrations\Configuration\Configuration | |
{ | |
protected function createVersion($version, $class) | |
{ | |
return new CustomVersion($this, $version, $class); | |
} | |
//createMigrationTable with an extra excuted date column | |
protected $migrationTableCreated=false; | |
public function createMigrationTable() { | |
$this->validate(); | |
if ($this->migrationTableCreated) { | |
return false; | |
} | |
$schema = $this->getConnection()->getSchemaManager(); | |
if ( ! $schema->tablesExist(array($this->getMigrationsTableName()))) { | |
$columns = array( | |
'version' => new Column('version', Type::getType('string'), array('length' => 255)), | |
'excuted' => new Column('excuted', Type::getType('string'), array('length' => 255)), | |
); | |
$table = new Table($this->getMigrationsTableName(), $columns); | |
$table->setPrimaryKey(array('version')); | |
$schema->createTable($table); | |
$this->migrationTableCreated = true; | |
return true; | |
} else if (count($schema->listTableColumns($this->getMigrationsTableName())) == 1) { | |
$columns = array( | |
'excuted' => new Column('excuted', Type::getType('string'), array('length' => 255)), | |
); | |
$tableDiff = new \Doctrine\DBAL\Schema\TableDiff($this->getMigrationsTableName(),$columns); | |
$schema->alterTable($tableDiff); | |
$this->migrationTableCreated = true; | |
return true; | |
} | |
return false; | |
} | |
} |
This file contains 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\Schema\Schema; | |
use Doctrine\DBAL\Migrations\Version; | |
class CustomVersion extends Version | |
{ | |
public function markMigrated() | |
{ | |
$this->getConfiguration()->createMigrationTable(); | |
$this->getConfiguration()->getConnection()->executeQuery( | |
"INSERT INTO " . $this->getConfiguration()->getMigrationsTableName() | |
. " (version,excuted) VALUES (?,?)", | |
array($this->getVersion(),date('Y-m-d H:i:s'))); | |
} | |
} |
This file contains 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 | |
class MigrationConfigurationHelper extends \Symfony\Component\Console\Helper\Helper | |
{ | |
public function getName() { | |
return "migration-configuration"; | |
} | |
public function getConfiguration($conn, $outputWriter, $input) { | |
$configuration = new CustomConfiguration($conn, $outputWriter); | |
return $configuration; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment