Doctrine 1.2.4
Doctrine_Migration Class Reference

Public Member Functions

 __construct ($directory=null, $connection=null)
 
 addError (Exception $e)
 
 clearErrors ()
 
 getCurrentVersion ()
 
 getErrors ()
 
 getLatestVersion ()
 
 getMigrationClass ($num)
 
 getMigrationClasses ()
 
 getMigrationClassesDirectory ()
 
 getNextMigrationClassVersion ()
 
 getNextVersion ()
 
 getNumErrors ()
 
 getTableName ()
 
 hasErrors ()
 
 hasMigrated ()
 
 loadMigrationClass ($name, $path=null)
 
 loadMigrationClassesFromDirectory ($directory=null)
 
 migrate ($to=null, $dryRun=false)
 
 migrateDryRun ($to=null)
 
 setCurrentVersion ($number)
 
 setTableName ($tableName)
 

Protected Member Functions

 _createMigrationTable ()
 
 _doMigrate ($to)
 
 _doMigrateStep ($direction, $num)
 
 _throwErrorsException ()
 

Detailed Description

Definition at line 35 of file Migration.php.

Constructor & Destructor Documentation

Doctrine_Migration::__construct (   $directory = null,
  $connection = null 
)

Specify the path to the directory with the migration classes. The classes will be loaded and the migration table will be created if it does not already exist

Parameters
string$directoryThe path to your migrations directory
mixed$connectionThe connection name or instance to use for this migration
Returns
void

Definition at line 57 of file Migration.php.

{
$this->_reflectionClass = new ReflectionClass('Doctrine_Migration_Base');
if (is_null($connection)) {
$this->_connection = Doctrine_Manager::connection();
} else {
if (is_string($connection)) {
$this->_connection = Doctrine_Manager::getInstance()
->getConnection($connection);
} else {
$this->_connection = $connection;
}
}
$this->_process = new Doctrine_Migration_Process($this);
if ($directory != null) {
$this->_migrationClassesDirectory = $directory;
}
}

Member Function Documentation

Doctrine_Migration::_createMigrationTable ( )
protected

Create the migration table and return true. If it already exists it will silence the exception and return false

Returns
boolean $created Whether or not the table was created. Exceptions are silenced when table already exists

Definition at line 546 of file Migration.php.

{
if ($this->_migrationTableCreated) {
return true;
}
$this->_migrationTableCreated = true;
try {
$this->_connection->export->createTable($this->_migrationTableName, array('version' => array('type' => 'integer', 'size' => 11)));
return true;
} catch(Exception $e) {
return false;
}
}
Doctrine_Migration::_doMigrate (   $to)
protected

Do the actual migration process

Parameters
integer$to
Returns
integer $to
Exceptions
Doctrine_Exception

Definition at line 467 of file Migration.php.

{
$from = $this->getCurrentVersion();
if ($from == $to) {
throw new Doctrine_Migration_Exception('Already at version # ' . $to);
}
$direction = $from > $to ? 'down':'up';
if ($direction === 'up') {
for ($i = $from + 1; $i <= $to; $i++) {
$this->_doMigrateStep($direction, $i);
}
} else {
for ($i = $from; $i > $to; $i--) {
$this->_doMigrateStep($direction, $i);
}
}
return $to;
}
Doctrine_Migration::_doMigrateStep (   $direction,
  $num 
)
protected

Perform a single migration step. Executes a single migration class and processes the changes

Parameters
string$directionDirection to go, 'up' or 'down'
integer$num
Returns
void

Definition at line 498 of file Migration.php.

{
try {
$migration = $this->getMigrationClass($num);
$method = 'pre' . $direction;
$migration->$method();
if (method_exists($migration, $direction)) {
$migration->$direction();
} else if (method_exists($migration, 'migrate')) {
$migration->migrate($direction);
}
if ($migration->getNumChanges() > 0) {
$changes = $migration->getChanges();
if ($direction == 'down' && method_exists($migration, 'migrate')) {
$changes = array_reverse($changes);
}
foreach ($changes as $value) {
list($type, $change) = $value;
$funcName = 'process' . Doctrine_Inflector::classify($type);
if (method_exists($this->_process, $funcName)) {
try {
$this->_process->$funcName($change);
} catch (Exception $e) {
$this->addError($e);
}
} else {
throw new Doctrine_Migration_Exception(sprintf('Invalid migration change type: %s', $type));
}
}
}
$method = 'post' . $direction;
$migration->$method();
} catch (Exception $e) {
$this->addError($e);
}
}
Doctrine_Migration::_throwErrorsException ( )
protected

Throw an exception with all the errors trigged during the migration

Returns
void
Exceptions
Doctrine_Migration_Exception::$e

Definition at line 443 of file Migration.php.

{
$messages = array();
$num = 0;
foreach ($this->getErrors() as $error) {
$num++;
$messages[] = ' Error #' . $num . ' - ' .$error->getMessage() . "\n" . $error->getTraceAsString() . "\n";
}
$title = $this->getNumErrors() . ' error(s) encountered during migration';
$message = $title . "\n";
$message .= str_repeat('=', strlen($title)) . "\n";
$message .= implode("\n", $messages);
throw new Doctrine_Migration_Exception($message);
}
Doctrine_Migration::addError ( Exception  $e)

Add an error to the stack. Excepts some type of Exception

Parameters
Exception$e
Returns
void

Definition at line 406 of file Migration.php.

{
$this->_errors[] = $e;
}
Doctrine_Migration::clearErrors ( )

Clears the error exceptions

Returns
void

Definition at line 395 of file Migration.php.

{
$this->_errors = array();
}
Doctrine_Migration::getCurrentVersion ( )

Get the current version of the database

Returns
integer $version

Definition at line 240 of file Migration.php.

{
$result = $this->_connection->fetchColumn("SELECT version FROM " . $this->_migrationTableName);
return isset($result[0]) ? $result[0]:0;
}
Doctrine_Migration::getErrors ( )

Get all the error exceptions

Returns
array $errors

Definition at line 385 of file Migration.php.

{
return $this->_errors;
}
Doctrine_Migration::getLatestVersion ( )

Gets the latest possible version from the loaded migration classes

Returns
integer $latestVersion

Definition at line 268 of file Migration.php.

{
$versions = array_keys($this->_migrationClasses);
rsort($versions);
return isset($versions[0]) ? $versions[0]:0;
}
Doctrine_Migration::getMigrationClass (   $num)

Get instance of migration class for number/version specified

Parameters
integer$num
Exceptions
Doctrine_Migration_Exception::$e

Definition at line 427 of file Migration.php.

{
if (isset($this->_migrationClasses[$num])) {
$className = $this->_migrationClasses[$num];
return new $className();
}
throw new Doctrine_Migration_Exception('Could not find migration class for migration step: '.$num);
}
Doctrine_Migration::getMigrationClasses ( )

Get all the loaded migration classes. Array where key is the number/version and the value is the class name.

Returns
array $migrationClasses

Definition at line 215 of file Migration.php.

{
return $this->_migrationClasses;
}
Doctrine_Migration::getMigrationClassesDirectory ( )

Get the migration classes directory

Returns
string $migrationClassesDirectory

Definition at line 96 of file Migration.php.

{
return $this->_migrationClassesDirectory;
}
Doctrine_Migration::getNextMigrationClassVersion ( )

Get the next incremented class version based on the loaded migration classes

Returns
integer $nextMigrationClassVersion

Definition at line 292 of file Migration.php.

{
if (empty($this->_migrationClasses)) {
return 1;
} else {
$nums = array_keys($this->_migrationClasses);
$num = end($nums) + 1;
return $num;
}
}
Doctrine_Migration::getNextVersion ( )

Get the next incremented version number based on the latest version number using getLatestVersion()

Returns
integer $nextVersion

Definition at line 282 of file Migration.php.

{
return $this->getLatestVersion() + 1;
}
Doctrine_Migration::getNumErrors ( )

Get the number of errors

Returns
integer $numErrors

Definition at line 375 of file Migration.php.

{
return count($this->_errors);
}
Doctrine_Migration::getTableName ( )

Get the table name for storing the version number for this migration instance

Returns
string $migrationTableName

Definition at line 106 of file Migration.php.

{
return $this->_migrationTableName;
}
Doctrine_Migration::hasErrors ( )

Whether or not the migration instance has errors

Returns
boolean

Definition at line 416 of file Migration.php.

{
return $this->getNumErrors() > 0 ? true:false;
}
Doctrine_Migration::hasMigrated ( )

hReturns true/false for whether or not this database has been migrated in the past

Returns
boolean $migrated

Definition at line 254 of file Migration.php.

{
$result = $this->_connection->fetchColumn("SELECT version FROM " . $this->_migrationTableName);
return isset($result[0]) ? true:false;
}
Doctrine_Migration::loadMigrationClass (   $name,
  $path = null 
)

Load the specified migration class name in to this migration instances queue of migration classes to execute. It must be a child of Doctrine_Migration in order to be loaded.

Parameters
string$name
Returns
void

Definition at line 177 of file Migration.php.

{
$class = new ReflectionClass($name);
while ($class->isSubclassOf($this->_reflectionClass)) {
$class = $class->getParentClass();
if ($class === false) {
break;
}
}
if ($class === false) {
return false;
}
if (empty($this->_migrationClasses)) {
$classMigrationNum = 1;
} else {
$nums = array_keys($this->_migrationClasses);
$num = end($nums);
$classMigrationNum = $num + 1;
}
$this->_migrationClasses[$classMigrationNum] = $name;
if ($path) {
$dir = dirname($path);
self::$_migrationClassesForDirectories[$dir][$classMigrationNum] = $name;
}
}
Doctrine_Migration::loadMigrationClassesFromDirectory (   $directory = null)

Load migration classes from the passed directory. Any file found with a .php extension will be passed to the loadMigrationClass()

Parameters
string$directoryDirectory to load migration classes from
Returns
void

Definition at line 130 of file Migration.php.

{
$directory = $directory ? $directory:$this->_migrationClassesDirectory;
$classesToLoad = array();
$classes = get_declared_classes();
foreach ((array) $directory as $dir) {
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::LEAVES_ONLY);
if (isset(self::$_migrationClassesForDirectories[$dir])) {
foreach (self::$_migrationClassesForDirectories[$dir] as $num => $className) {
$this->_migrationClasses[$num] = $className;
}
}
foreach ($it as $file) {
$info = pathinfo($file->getFileName());
if (isset($info['extension']) && $info['extension'] == 'php') {
require_once($file->getPathName());
$array = array_diff(get_declared_classes(), $classes);
$className = end($array);
if ($className) {
$e = explode('_', $file->getFileName());
$timestamp = $e[0];
$classesToLoad[$timestamp] = array('className' => $className, 'path' => $file->getPathName());
}
}
}
}
ksort($classesToLoad, SORT_NUMERIC);
foreach ($classesToLoad as $class) {
$this->loadMigrationClass($class['className'], $class['path']);
}
}
Doctrine_Migration::migrate (   $to = null,
  $dryRun = false 
)

Perform a migration process by specifying the migration number/version to migrate to. It will automatically know whether you are migrating up or down based on the current version of the database.

Parameters
integer$toVersion to migrate to
boolean$dryRunWhether or not to run the migrate process as a dry run
Returns
integer $to Version number migrated to
Exceptions
Doctrine_Exception

Definition at line 313 of file Migration.php.

{
$this->clearErrors();
$this->_connection->beginTransaction();
try {
// If nothing specified then lets assume we are migrating from
// the current version to the latest version
if ($to === null) {
$to = $this->getLatestVersion();
}
$this->_doMigrate($to);
} catch (Exception $e) {
$this->addError($e);
}
if ($this->hasErrors()) {
$this->_connection->rollback();
if ($dryRun) {
return false;
} else {
}
} else {
if ($dryRun) {
$this->_connection->rollback();
if ($this->hasErrors()) {
return false;
} else {
return $to;
}
} else {
$this->_connection->commit();
$this->setCurrentVersion($to);
return $to;
}
}
return false;
}
Doctrine_Migration::migrateDryRun (   $to = null)

Run the migration process but rollback at the very end. Returns true or false for whether or not the migration can be ran

Parameters
string$to
Returns
boolean $success

Definition at line 365 of file Migration.php.

{
return $this->migrate($to, true);
}
Doctrine_Migration::setCurrentVersion (   $number)

Set the current version of the database

Parameters
integer$number
Returns
void

Definition at line 226 of file Migration.php.

{
if ($this->hasMigrated()) {
$this->_connection->exec("UPDATE " . $this->_migrationTableName . " SET version = $number");
} else {
$this->_connection->exec("INSERT INTO " . $this->_migrationTableName . " (version) VALUES ($number)");
}
}
Doctrine_Migration::setTableName (   $tableName)

Set the table name for storing the version number for this migration instance

Parameters
string$tableName
Returns
void

Definition at line 117 of file Migration.php.

{
$this->_migrationTableName = $this->_connection
->formatter->getTableName($tableName);
}

The documentation for this class was generated from the following file: