1
0
mirror of synced 2024-12-13 22:56:04 +03:00
doctrine2/lib/Doctrine/Migration.php

628 lines
16 KiB
PHP
Raw Normal View History

2007-09-19 00:15:17 +04:00
<?php
/*
* $Id: Migration.php 1080 2007-02-10 18:17:08Z jwage $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.org>.
2007-09-19 00:15:17 +04:00
*/
2007-09-19 00:15:17 +04:00
/**
* Doctrine_Migration
*
* @package Doctrine
* @subpackage Migration
2007-09-19 00:15:17 +04:00
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.org
2007-09-19 00:15:17 +04:00
* @since 1.0
* @version $Revision: 1080 $
* @author Jonathan H. Wage <jonwage@gmail.com>
2007-09-19 00:15:17 +04:00
*/
class Doctrine_Migration
{
2007-10-26 08:13:29 +04:00
protected $_changes = array('created_tables' => array(),
'renamed_tables' => array(),
'created_constraints' => array(),
'dropped_fks' => array(),
'created_fks' => array(),
'dropped_constraints' => array(),
'removed_indexes' => array(),
'dropped_tables' => array(),
'added_columns' => array(),
'renamed_columns' => array(),
'changed_columns' => array(),
'removed_columns' => array(),
'added_indexes' => array()),
$_migrationTableName = 'migration_version',
$_migrationClassesDirectory = array(),
$_migrationClasses = array(),
$_loadedMigrations = array();
/**
* construct
*
* 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
*
* @param string $directory
* @return void
*/
2007-09-21 00:24:38 +04:00
public function __construct($directory = null)
{
if ($directory != null) {
2007-10-26 08:13:29 +04:00
$this->_migrationClassesDirectory = $directory;
2008-01-23 03:43:03 +03:00
$this->_loadMigrationClasses();
$this->_createMigrationTable();
2007-09-21 00:24:38 +04:00
}
}
/**
* getTableName
*
* @return void
*/
public function getTableName()
{
return $this->_migrationTableName;
}
/**
* setTableName
*
* @param string $tableName
* @return void
*/
public function setTableName($tableName)
{
$this->_migrationTableName = Doctrine_Manager::connection()
->formatter->getTableName($tableName);
}
/**
* createMigrationTable
*
* Creates the migration table used to store the current version
*
* @return void
*/
2008-01-23 03:43:03 +03:00
protected function _createMigrationTable()
{
$conn = Doctrine_Manager::connection();
2008-01-23 03:43:03 +03:00
try {
2007-10-26 08:13:29 +04:00
$conn->export->createTable($this->_migrationTableName, array('version' => array('type' => 'integer', 'size' => 11)));
2008-01-23 03:43:03 +03:00
return true;
} catch(Exception $e) {
return false;
}
}
/**
* loadMigrationClassesFromDirectory
*
* refactored out from loadMigrationClasses
* $param array An array of classes
* @return void
*/
public function loadMigrationClassesFromDirectory($classes){
foreach ((array) $this->_migrationClassesDirectory as $dir) {
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($it as $file) {
$e = explode('.', $file->getFileName());
if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) {
if ( ! in_array($file->getFileName(), $this->_loadedMigrations)) {
require_once($file->getPathName());
$requiredClass = array_diff(get_declared_classes(), $classes);
$requiredClass = end($requiredClass);
if ($requiredClass) {
$this->_loadedMigrations[$requiredClass] = $file->getFileName();
}
}
}
}
}
}
/**
* loadMigrationClasses
*
* Loads the migration classes for the directory specified by the constructor
*
* @return void
*/
2008-01-23 03:43:03 +03:00
protected function _loadMigrationClasses()
2007-09-21 00:24:38 +04:00
{
2007-10-26 08:13:29 +04:00
if ($this->_migrationClasses) {
return $this->_migrationClasses;
}
2008-01-23 03:43:03 +03:00
2007-09-21 00:24:38 +04:00
$classes = get_declared_classes();
2008-01-23 03:43:03 +03:00
2007-10-26 08:13:29 +04:00
if ($this->_migrationClassesDirectory !== null) {
2008-01-23 04:00:45 +03:00
$this->loadMigrationClassesFromDirectory($classes);
2007-09-21 00:24:38 +04:00
}
2007-09-21 00:24:38 +04:00
$parent = new ReflectionClass('Doctrine_Migration');
2008-01-23 03:43:03 +03:00
2007-10-26 08:13:29 +04:00
foreach ($this->_loadedMigrations as $name => $fileName) {
2007-09-21 00:24:38 +04:00
$class = new ReflectionClass($name);
2008-01-23 03:43:03 +03:00
while ($class->isSubclassOf($parent)) {
2007-09-21 00:24:38 +04:00
$class = $class->getParentClass();
if ($class === false) {
break;
}
}
2008-01-23 03:43:03 +03:00
2007-09-21 00:24:38 +04:00
if ($class === false) {
continue;
}
2008-01-23 03:43:03 +03:00
$e = explode('_', $fileName);
$classMigrationNum = (int) $e[0];
2008-01-23 03:43:03 +03:00
2007-10-26 08:13:29 +04:00
$this->_migrationClasses[$classMigrationNum] = array('className' => $name, 'fileName' => $fileName);
2007-09-21 00:24:38 +04:00
}
2008-01-23 03:43:03 +03:00
2007-10-26 08:13:29 +04:00
return $this->_migrationClasses;
}
/**
* getMigrationClasses
*
* @return void
*/
public function getMigrationClasses()
{
2007-10-26 08:13:29 +04:00
return $this->_migrationClasses;
2007-09-21 00:24:38 +04:00
}
/**
* setCurrentVersion
*
* Sets the current version in the migration table
*
* @param string $number
* @return void
*/
2008-01-23 03:43:03 +03:00
protected function _setCurrentVersion($number)
2007-09-19 23:33:00 +04:00
{
$conn = Doctrine_Manager::connection();
2008-01-23 03:43:03 +03:00
if ($this->hasMigrated()) {
2007-10-26 08:13:29 +04:00
$conn->exec("UPDATE " . $this->_migrationTableName . " SET version = $number");
2007-09-19 23:33:00 +04:00
} else {
2007-10-26 08:13:29 +04:00
$conn->exec("INSERT INTO " . $this->_migrationTableName . " (version) VALUES ($number)");
2007-09-19 23:33:00 +04:00
}
}
/**
* getCurrentVersion
*
* Get the current version of the database
*
* @return void
*/
2007-10-08 20:04:46 +04:00
public function getCurrentVersion()
2007-09-19 23:33:00 +04:00
{
$conn = Doctrine_Manager::connection();
2008-01-23 03:43:03 +03:00
2007-10-26 08:13:29 +04:00
$result = $conn->fetchColumn("SELECT version FROM " . $this->_migrationTableName);
2008-01-23 03:43:03 +03:00
return isset($result[0]) ? $result[0]:0;
2007-09-19 23:33:00 +04:00
}
/**
* hasMigrated
*
* Returns true/false for whether or not this database has been migrated in the past
*
* @return void
*/
2007-10-08 20:04:46 +04:00
public function hasMigrated()
2007-09-19 00:15:17 +04:00
{
$conn = Doctrine_Manager::connection();
2008-01-23 03:43:03 +03:00
2007-10-26 08:13:29 +04:00
$result = $conn->fetchColumn("SELECT version FROM " . $this->_migrationTableName);
2008-01-23 03:43:03 +03:00
return isset($result[0]) ? true:false;
}
/**
* getLatestVersion
*
* Gets the latest possible version from the loaded migration classes
*
* @return void
*/
2007-10-08 20:04:46 +04:00
public function getLatestVersion()
{
2008-01-23 03:43:03 +03:00
$this->_loadMigrationClasses();
$versions = array();
2007-10-26 08:13:29 +04:00
foreach (array_keys($this->_migrationClasses) as $classMigrationNum) {
$versions[$classMigrationNum] = $classMigrationNum;
2007-09-19 00:15:17 +04:00
}
2008-01-23 03:43:03 +03:00
rsort($versions);
2008-01-23 03:43:03 +03:00
return isset($versions[0]) ? $versions[0]:0;
}
/**
* getNextVersion
*
* @return integer $nextVersion
*/
public function getNextVersion()
{
return $this->getLatestVersion() + 1;
2007-09-19 00:15:17 +04:00
}
/**
* getMigrationClass
*
* Get instance of migration class for $num
*
* @param string $num
* @return void
*/
2008-01-23 03:43:03 +03:00
protected function _getMigrationClass($num)
2007-09-19 00:15:17 +04:00
{
2007-10-26 08:13:29 +04:00
foreach ($this->_migrationClasses as $classMigrationNum => $info) {
2007-10-16 00:39:44 +04:00
$className = $info['className'];
2008-01-23 03:43:03 +03:00
2007-10-16 00:39:44 +04:00
if ($classMigrationNum == $num) {
2007-09-21 00:24:38 +04:00
return new $className();
}
2007-09-19 23:33:00 +04:00
}
2008-01-23 03:43:03 +03:00
2007-09-21 00:24:38 +04:00
throw new Doctrine_Migration_Exception('Could not find migration class for migration step: '.$num);
2007-09-19 23:33:00 +04:00
}
/**
* doMigrateStep
*
* Perform migration directory for the specified version. Loads migration classes and performs the migration then processes the changes
*
* @param string $direction
* @param string $num
* @return void
*/
2008-01-23 03:43:03 +03:00
protected function _doMigrateStep($direction, $num)
2007-09-19 23:33:00 +04:00
{
2008-01-23 03:43:03 +03:00
$migrate = $this->_getMigrationClass($num);
$migrate->_doMigrate($direction);
2007-09-19 00:15:17 +04:00
}
/**
* doMigrate
*
* Perform migration for a migration class. Executes the up or down method then processes the changes
*
* @param string $direction
* @return void
*/
2008-01-23 03:43:03 +03:00
protected function _doMigrate($direction)
2007-09-19 00:15:17 +04:00
{
2008-02-17 07:53:51 +03:00
$method = 'pre'.$direction;
$this->$method();
if ( method_exists($this, $direction)) {
$this->$direction();
foreach ($this->_changes as $type => $changes) {
if ( ! empty($changes)) {
$funcName = 'process' . Doctrine::classify($type);
$process = new Doctrine_Migration_Process();
$process->$funcName($changes);
}
}
2007-09-19 00:15:17 +04:00
}
2008-02-17 07:53:51 +03:00
$method = 'post'.$direction;
$this->$method();
2007-09-19 00:15:17 +04:00
}
/**
* migrate
*
* Perform a migration chain by specifying the $from and $to.
* If you do not specify a $from or $to then it will attempt to migrate from the current version to the latest version
*
* @param string $from
* @param string $to
* @return void
*/
public function migrate($to = null)
2007-09-19 00:15:17 +04:00
{
$from = $this->getCurrentVersion();
2008-01-23 03:43:03 +03:00
// If nothing specified then lets assume we are migrating from the current version to the latest version
if ($to === null) {
$to = $this->getLatestVersion();
}
2008-01-23 03:43:03 +03:00
2007-10-18 20:36:55 +04:00
if ($from == $to) {
throw new Doctrine_Migration_Exception('Already at version # ' . $to);
2007-09-19 00:15:17 +04:00
}
2008-01-23 03:43:03 +03:00
$direction = $from > $to ? 'down':'up';
2008-01-23 03:43:03 +03:00
if ($direction === 'up') {
for ($i = $from + 1; $i <= $to; $i++) {
2008-01-23 03:43:03 +03:00
$this->_doMigrateStep($direction, $i);
}
} else {
for ($i = $from; $i > $to; $i--) {
2008-01-23 03:43:03 +03:00
$this->_doMigrateStep($direction, $i);
}
}
2008-01-23 03:43:03 +03:00
$this->_setCurrentVersion($to);
return $to;
2007-09-19 00:15:17 +04:00
}
/**
* addChange
*
* @param string $type
* @param string $array
* @return void
*/
2008-01-23 03:43:03 +03:00
protected function _addChange($type, array $change = array())
2007-09-19 00:15:17 +04:00
{
2007-10-26 08:13:29 +04:00
$this->_changes[$type][] = $change;
2007-09-19 00:15:17 +04:00
}
/**
* createTable
*
* @param string $tableName
* @param string $array
* @param string $array
* @return void
*/
2007-09-19 00:15:17 +04:00
public function createTable($tableName, array $fields = array(), array $options = array())
{
$options = get_defined_vars();
2008-01-23 03:43:03 +03:00
$this->_addChange('created_tables', $options);
2007-09-19 00:15:17 +04:00
}
/**
* dropTable
*
* @param string $tableName
* @return void
*/
2007-09-19 00:15:17 +04:00
public function dropTable($tableName)
{
$options = get_defined_vars();
2008-01-23 03:43:03 +03:00
$this->_addChange('dropped_tables', $options);
2007-09-19 00:15:17 +04:00
}
/**
* renameTable
*
* @param string $oldTableName
* @param string $newTableName
* @return void
*/
2007-09-19 00:15:17 +04:00
public function renameTable($oldTableName, $newTableName)
{
$options = get_defined_vars();
2008-01-23 03:43:03 +03:00
$this->_addChange('renamed_tables', $options);
2007-09-19 00:15:17 +04:00
}
/**
* createConstraint
*
* @param string $tableName
* @param string $constraintName
* @return void
*/
public function createConstraint($tableName, $constraintName, array $definition)
{
$options = get_defined_vars();
2008-01-23 03:43:03 +03:00
$this->_addChange('created_constraints', $options);
}
/**
* dropConstraint
*
* @param string $tableName
* @param string $constraintName
* @return void
*/
2007-10-17 22:49:02 +04:00
public function dropConstraint($tableName, $constraintName, $primary = false)
{
$options = get_defined_vars();
2008-01-23 03:43:03 +03:00
$this->_addChange('dropped_constraints', $options);
}
/**
* createForeignKey
*
* @param string $tableName
* @param string $constraintName
* @return void
*/
public function createForeignKey($tableName, array $definition)
{
$options = get_defined_vars();
2008-01-23 03:43:03 +03:00
$this->_addChange('created_fks', $options);
}
/**
* dropForeignKey
*
* @param string $tableName
* @param string $constraintName
* @return void
*/
public function dropForeignKey($tableName, $fkName)
{
$options = get_defined_vars();
2008-01-23 03:43:03 +03:00
$this->_addChange('dropped_fks', $options);
}
/**
* addColumn
*
* @param string $tableName
* @param string $columnName
* @param string $type
* @param string $array
* @return void
*/
2007-09-19 20:26:28 +04:00
public function addColumn($tableName, $columnName, $type, array $options = array())
2007-09-19 00:15:17 +04:00
{
$options = get_defined_vars();
2008-01-23 03:43:03 +03:00
$this->_addChange('added_columns', $options);
2007-09-19 00:15:17 +04:00
}
/**
* renameColumn
*
* @param string $tableName
* @param string $oldColumnName
* @param string $newColumnName
* @return void
*/
2007-09-19 00:15:17 +04:00
public function renameColumn($tableName, $oldColumnName, $newColumnName)
{
$options = get_defined_vars();
2008-01-23 03:43:03 +03:00
$this->_addChange('renamed_columns', $options);
2007-09-19 00:15:17 +04:00
}
/**
* renameColumn
*
* @param string $tableName
* @param string $columnName
* @param string $type
* @param string $array
* @return void
*/
2007-09-19 00:15:17 +04:00
public function changeColumn($tableName, $columnName, $type, array $options = array())
{
$options = get_defined_vars();
2008-01-23 03:43:03 +03:00
$this->_addChange('changed_columns', $options);
2007-09-19 00:15:17 +04:00
}
/**
* removeColumn
*
* @param string $tableName
* @param string $columnName
* @return void
*/
2007-09-19 00:15:17 +04:00
public function removeColumn($tableName, $columnName)
{
$options = get_defined_vars();
2008-01-23 03:43:03 +03:00
$this->_addChange('removed_columns', $options);
2007-09-19 00:15:17 +04:00
}
/**
* addIndex
*
* @param string $tableName
* @param string $indexName
* @param string $array
* @return void
*/
2007-10-19 17:16:22 +04:00
public function addIndex($tableName, $indexName, array $definition)
2007-09-19 00:15:17 +04:00
{
$options = get_defined_vars();
2008-01-23 03:43:03 +03:00
$this->_addChange('added_indexes', $options);
2007-09-19 00:15:17 +04:00
}
/**
* removeIndex
*
* @param string $tableName
* @param string $indexName
* @return void
*/
2007-09-19 00:15:17 +04:00
public function removeIndex($tableName, $indexName)
{
$options = get_defined_vars();
2008-01-23 03:43:03 +03:00
$this->_addChange('removed_indexes', $options);
2007-09-19 00:15:17 +04:00
}
2008-02-17 07:53:51 +03:00
/**
* preUp
*
* @return void
*/
public function preUp()
{
return;
}
/**
* postUp
*
* @return void
*/
public function postUp()
{
return;
}
/**
* preDown
*
* @return void
*/
public function preDown()
{
return;
}
/**
* postDown
*
* @return void
*/
public function postDown()
{
return;
}
}