. */ /** * Doctrine_Migration * * this class represents a database view * * @author Jonathan H. Wage * @package Doctrine * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @category Object Relational Mapping * @link www.phpdoctrine.com * @since 1.0 * @version $Revision: 1080 $ */ class Doctrine_Migration { public $changes = array('created_tables' => array(), 'dropped_tables' => array(), 'renamed_tables' => array(), 'added_columns' => array(), 'renamed_columns' => array(), 'changed_columns' => array(), 'removed_columns' => array(), 'added_indexes' => array(), 'removed_indexes' => array()); static public function migration($directory, $from, $to) { if ($from === $to OR $from === 0) { throw new Doctrine_Migration_Exception('You specified an invalid migration path. The from and to cannot be the same and from cannot be zero.'); } $direction = $from > $to ? 'down':'up'; $fromPath = $directory.DIRECTORY_SEPARATOR; $toPath = $directory.DIRECTORY_SEPARATOR; if ($direction === 'up') { for ($i = $from + 1; $i <= $to; $i++) { self::doDirectionStep($directory, $direction, $i); } } else { for ($i = $from; $i > $to; $i--) { self::doDirectionStep($directory, $direction, $i); } } } public static function doDirectionStep($directory, $direction, $num) { $className = 'Migration'.$num; $fileName = $className.'.class.php'; $filePath = $directory.DIRECTORY_SEPARATOR.$fileName; if (file_exists($filePath)) { require_once($filePath); if (class_exists($className)) { $migrate = new $className(); $migrate->migrate($direction); } } } public function migrate($direction) { if (method_exists($this, $direction)) { $this->$direction(); $this->processChanges(); } } public function processChanges() { foreach ($this->changes as $type => $changes) { $process = new Doctrine_Migration_Process(); $funcName = 'process'.Doctrine::classify($type); $process->$funcName($changes); } } public function addChange($type, array $change = array()) { $this->changes[$type][] = $change; } public function createTable($tableName, array $fields = array(), array $options = array()) { $options = get_defined_vars(); $this->addChange('created_tables', $options); } public function dropTable($tableName) { $options = get_defined_vars(); $this->addChange('dropped_tables', $options); } public function renameTable($oldTableName, $newTableName) { $options = get_defined_vars(); $this->addChange('renamed_tables', $options); } public function addColumn($tableName, $columnName, $type, $options = array()) { $options = get_defined_vars(); $this->addChange('added_columns', $options); } public function renameColumn($tableName, $oldColumnName, $newColumnName) { $options = get_defined_vars(); $this->addChange('renamed_columns', $options); } public function changeColumn($tableName, $columnName, $type, array $options = array()) { $options = get_defined_vars(); $this->addChange('changed_columns', $options); } public function removeColumn($tableName, $columnName) { $options = get_defined_vars(); $this->addChange('removed_columns', $options); } public function addIndex($tableName, $indexName, array $options = array()) { $options = get_defined_vars(); $this->addChange('added_indexes', $options); } public function removeIndex($tableName, $indexName) { $options = get_defined_vars(); $this->addChange('removed_indexes', $options); } }