Skip to content

Instantly share code, notes, and snippets.

@JeffPeters
Last active April 22, 2016 12:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffPeters/7535348 to your computer and use it in GitHub Desktop.
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
<?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;
}
}
<?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')));
}
}
<?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