Doctrine 1.2.4
Doctrine_Migration_Diff Class Reference

Public Member Functions

 __construct ($from, $to, $migration)
 
 generateChanges ()
 
 generateMigrationClasses ()
 
 setTmpPath ($tmpPath)
 

Protected Member Functions

 _buildChanges ($from, $to)
 
 _buildModelInformation (array $models)
 
 _cleanModelInformation ($info)
 
 _cleanup ()
 
 _diff ($from, $to)
 
 _generateModels ($prefix, $item)
 
 _getItemExtension ($item)
 
 _initializeModels ($path)
 
 getUniqueId ()
 

Detailed Description

Definition at line 34 of file Diff.php.

Constructor & Destructor Documentation

Doctrine_Migration_Diff::__construct (   $from,
  $to,
  $migration 
)

Instantiate new Doctrine_Migration_Diff instance

$diff = new Doctrine_Migration_Diff('/path/to/old_models', '/path/to/new_models', '/path/to/migrations'); $diff->generateMigrationClasses();

Parameters
string$fromThe from schema information source
string$toThe to schema information source
mixed$migrationInstance of Doctrine_Migration or path to migration classes
Returns
void

Definition at line 67 of file Diff.php.

{
$this->_from = $from;
$this->_to = $to;
$this->_startingModelFiles = Doctrine_Core::getLoadedModelFiles();
$this->setTmpPath(sys_get_temp_dir() . DIRECTORY_SEPARATOR . getmypid());
if ($migration instanceof Doctrine_Migration) {
$this->_migration = $migration;
} else if (is_dir($migration)) {
$this->_migration = new Doctrine_Migration($migration);
}
}

Member Function Documentation

Doctrine_Migration_Diff::_buildChanges (   $from,
  $to 
)
protected

Build array of changes between the from and to array of schema information

Parameters
array$fromArray of schema information to generate changes from
array$toArray of schema information to generate changes for
Returns
array $changes

Definition at line 188 of file Diff.php.

{
// Loop over the to schema information and compare it to the from
foreach ($to as $className => $info) {
// If the from doesn't have this class then it is a new table
if ( ! isset($from[$className])) {
$names = array('type', 'charset', 'collate', 'indexes', 'foreignKeys', 'primary');
$options = array();
foreach ($names as $name) {
if (isset($info['options'][$name]) && $info['options'][$name]) {
$options[$name] = $info['options'][$name];
}
}
$table = array('tableName' => $info['tableName'],
'columns' => $info['columns'],
'options' => $options);
$this->_changes['created_tables'][$info['tableName']] = $table;
}
// Check for new and changed columns
foreach ($info['columns'] as $name => $column) {
// If column doesn't exist in the from schema information then it is a new column
if (isset($from[$className]) && ! isset($from[$className]['columns'][$name])) {
$this->_changes['created_columns'][$info['tableName']][$name] = $column;
}
// If column exists in the from schema information but is not the same then it is a changed column
if (isset($from[$className]['columns'][$name]) && $from[$className]['columns'][$name] != $column) {
$this->_changes['changed_columns'][$info['tableName']][$name] = $column;
}
}
// Check for new foreign keys
foreach ($info['options']['foreignKeys'] as $name => $foreignKey) {
$foreignKey['name'] = $name;
// If foreign key doesn't exist in the from schema information then we need to add a index and the new fk
if ( ! isset($from[$className]['options']['foreignKeys'][$name])) {
$this->_changes['created_foreign_keys'][$info['tableName']][$name] = $foreignKey;
$indexName = Doctrine_Manager::connection()->generateUniqueIndexName($info['tableName'], $foreignKey['local']);
$this->_changes['created_indexes'][$info['tableName']][$indexName] = array('fields' => array($foreignKey['local']));
// If foreign key does exist then lets see if anything has changed with it
} else if (isset($from[$className]['options']['foreignKeys'][$name])) {
$oldForeignKey = $from[$className]['options']['foreignKeys'][$name];
$oldForeignKey['name'] = $name;
// If the foreign key has changed any then we need to drop the foreign key and readd it
if ($foreignKey !== $oldForeignKey) {
$this->_changes['dropped_foreign_keys'][$info['tableName']][$name] = $oldForeignKey;
$this->_changes['created_foreign_keys'][$info['tableName']][$name] = $foreignKey;
}
}
}
// Check for new indexes
foreach ($info['options']['indexes'] as $name => $index) {
// If index doesn't exist in the from schema information
if ( ! isset($from[$className]['options']['indexes'][$name])) {
$this->_changes['created_indexes'][$info['tableName']][$name] = $index;
}
}
}
// Loop over the from schema information and compare it to the to schema information
foreach ($from as $className => $info) {
// If the class exists in the from but not in the to then it is a dropped table
if ( ! isset($to[$className])) {
$table = array('tableName' => $info['tableName'],
'columns' => $info['columns'],
'options' => array('type' => $info['options']['type'],
'charset' => $info['options']['charset'],
'collate' => $info['options']['collate'],
'indexes' => $info['options']['indexes'],
'foreignKeys' => $info['options']['foreignKeys'],
'primary' => $info['options']['primary']));
$this->_changes['dropped_tables'][$info['tableName']] = $table;
}
// Check for removed columns
foreach ($info['columns'] as $name => $column) {
// If column exists in the from but not in the to then we need to remove it
if (isset($to[$className]) && ! isset($to[$className]['columns'][$name])) {
$this->_changes['dropped_columns'][$info['tableName']][$name] = $column;
}
}
// Check for dropped foreign keys
foreach ($info['options']['foreignKeys'] as $name => $foreignKey) {
// If the foreign key exists in the from but not in the to then we need to drop it
if ( ! isset($to[$className]['options']['foreignKeys'][$name])) {
$this->_changes['dropped_foreign_keys'][$info['tableName']][$name] = $foreignKey;
}
}
// Check for removed indexes
foreach ($info['options']['indexes'] as $name => $index) {
// If the index exists in the from but not the to then we need to remove it
if ( ! isset($to[$className]['options']['indexes'][$name])) {
$this->_changes['dropped_indexes'][$info['tableName']][$name] = $index;
}
}
}
return $this->_changes;
}
Doctrine_Migration_Diff::_buildModelInformation ( array  $models)
protected

Build all the model schema information for the passed array of models

Parameters
array$modelsArray of models to build the schema information for
Returns
array $info Array of schema information for all the passed models

Definition at line 291 of file Diff.php.

{
$info = array();
foreach ($models as $key => $model) {
$table = Doctrine_Core::getTable($model);
if ($table->getTableName() !== $this->_migration->getTableName()) {
$info[$model] = $table->getExportableFormat();
}
}
$info = $this->_cleanModelInformation($info);
return $info;
}
Doctrine_Migration_Diff::_cleanModelInformation (   $info)
protected

Clean the produced model information of any potential prefix text

Parameters
mixed$infoEither array or string to clean of prefixes
Returns
mixed $info Cleaned value which is either an array or string

Definition at line 312 of file Diff.php.

{
if (is_array($info)) {
foreach ($info as $key => $value) {
unset($info[$key]);
$key = $this->_cleanModelInformation($key);
$info[$key] = $this->_cleanModelInformation($value);
}
return $info;
} else {
$find = array(
self::$_toPrefix,
self::$_fromPrefix,
Doctrine_Inflector::tableize(self::$_toPrefix) . '_',
Doctrine_Inflector::tableize(self::$_fromPrefix) . '_',
Doctrine_Inflector::tableize(self::$_toPrefix),
Doctrine_Inflector::tableize(self::$_fromPrefix)
);
return str_replace($find, null, $info);
}
}
Doctrine_Migration_Diff::_cleanup ( )
protected

Cleanup temporary generated models after a diff is performed

Returns
void

Definition at line 406 of file Diff.php.

{
$filesToClean = array_diff($modelFiles, $this->_startingModelFiles);
foreach ($filesToClean as $file) {
if (file_exists($file)) {
unlink($file);
}
}
// clean up tmp directories
Doctrine_Lib::removeDirectories($this->_tmpPath . DIRECTORY_SEPARATOR . strtolower(self::$_fromPrefix) . '_doctrine_tmp_dirs');
Doctrine_Lib::removeDirectories($this->_tmpPath . DIRECTORY_SEPARATOR . strtolower(self::$_toPrefix) . '_doctrine_tmp_dirs');
}
Doctrine_Migration_Diff::_diff (   $from,
  $to 
)
protected

Generate a diff between the from and to schema information

Parameters
string$fromPath to set of models to migrate from
string$toPath to set of models to migrate to
Returns
array $changes

Definition at line 163 of file Diff.php.

{
// Load the from and to models
$fromModels = $this->_initializeModels($from);
$toModels = $this->_initializeModels($to);
// Build schema information for the models
$fromInfo = $this->_buildModelInformation($fromModels);
$toInfo = $this->_buildModelInformation($toModels);
// Build array of changes between the from and to information
$changes = $this->_buildChanges($fromInfo, $toInfo);
$this->_cleanup();
return $changes;
}
Doctrine_Migration_Diff::_generateModels (   $prefix,
  $item 
)
protected

Generate a set of models for the schema information source

Parameters
string$prefixPrefix to generate the models with
mixed$itemThe item to generate the models from
Returns
string $path The path where the models were generated
Exceptions
Doctrine_Migration_Exception::$e

Definition at line 369 of file Diff.php.

{
$path = $this->_tmpPath . DIRECTORY_SEPARATOR . strtolower($prefix) . '_doctrine_tmp_dirs';
$options = array(
'classPrefix' => $prefix,
'generateBaseClasses' => false
);
if (is_string($item) && file_exists($item)) {
$extension = $this->_getItemExtension($item);
if ($extension === 'yml') {
Doctrine_Core::generateModelsFromYaml($item, $path, $options);
return $path;
} else if ($extension === 'php') {
return $path;
} else {
throw new Doctrine_Migration_Exception('No php or yml files found at path: "' . $item . '"');
}
} else {
try {
Doctrine_Core::generateModelsFromDb($path, (array) $item, $options);
return $path;
} catch (Exception $e) {
throw new Doctrine_Migration_Exception('Could not generate models from connection: ' . $e->getMessage());
}
}
}
Doctrine_Migration_Diff::_getItemExtension (   $item)
protected

Get the extension of the type of file contained in a directory. Used to determine if a directory contains YAML or PHP files.

Parameters
string$item
Returns
string $extension

Definition at line 341 of file Diff.php.

{
if (is_dir($item)) {
$files = glob($item . DIRECTORY_SEPARATOR . '*');
} else {
$files = array($item);
}
$extension = null;
if (isset($files[0])) {
if (is_dir($files[0])) {
$extension = $this->_getItemExtension($files[0]);
} else {
$pathInfo = pathinfo($files[0]);
$extension = $pathInfo['extension'];
}
}
return $extension;
}
Doctrine_Migration_Diff::_initializeModels (   $path)
protected

Initialize some Doctrine models at a given path.

Parameters
string$path
Returns
array $models

Definition at line 141 of file Diff.php.

{
$modelLoading = $manager->getAttribute(Doctrine_Core::ATTR_MODEL_LOADING);
if ($modelLoading === Doctrine_Core::MODEL_LOADING_PEAR) {
} else {
}
return $models;
}
Doctrine_Migration_Diff::generateChanges ( )

Generate an array of changes found between the from and to schema information.

Returns
array $changes

Definition at line 110 of file Diff.php.

{
$this->_cleanup();
$from = $this->_generateModels(self::$_fromPrefix, $this->_from);
$to = $this->_generateModels(
Doctrine_Manager::getInstance()->getAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX) . self::$_toPrefix,
$this->_to
);
return $this->_diff($from, $to);
}
Doctrine_Migration_Diff::generateMigrationClasses ( )

Generate a migration class for the changes in this diff instance

Returns
array $changes

Definition at line 128 of file Diff.php.

{
$builder = new Doctrine_Migration_Builder($this->_migration);
return $builder->generateMigrationsFromDiff($this);
}
Doctrine_Migration_Diff::getUniqueId ( )
protected

Get unique hash id for this migration instance

Returns
string $uniqueId

Definition at line 100 of file Diff.php.

{
return md5($this->_from . $this->_to);
}
Doctrine_Migration_Diff::setTmpPath (   $tmpPath)

Set the temporary path to store the generated models for generating diffs

Parameters
string$tmpPath
Returns
void

Definition at line 87 of file Diff.php.

{
if ( ! is_dir($tmpPath)) {
mkdir($tmpPath, 0777, true);
}
$this->_tmpPath = $tmpPath;
}

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