| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Migration.php 1080 2007-02-10 18:17:08Z jwage $ |
| 4 | * |
| 5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 16 | * |
| 17 | * This software consists of voluntary contributions made by many individuals |
| 18 | * and is licensed under the LGPL. For more information, see |
| 19 | * <http://www.phpdoctrine.org>. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * Doctrine_Migration |
| 24 | * |
| 25 | * this class represents a database view |
| 26 | * |
| 27 | * @package Doctrine |
| 28 | * @subpackage Migration |
| 29 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 30 | * @link www.phpdoctrine.org |
| 31 | * @since 1.0 |
| 32 | * @version $Revision: 1080 $ |
| 33 | * @author Jonathan H. Wage <jwage@mac.com> |
| 34 | */ |
| 35 | class Doctrine_Migration |
| 36 | { |
| 37 | protected $_changes = array('created_tables' => array(), |
| 38 | 'renamed_tables' => array(), |
| 39 | 'created_constraints' => array(), |
| 40 | 'dropped_fks' => array(), |
| 41 | 'created_fks' => array(), |
| 42 | 'dropped_constraints' => array(), |
| 43 | 'removed_indexes' => array(), |
| 44 | 'dropped_tables' => array(), |
| 45 | 'added_columns' => array(), |
| 46 | 'renamed_columns' => array(), |
| 47 | 'changed_columns' => array(), |
| 48 | 'removed_columns' => array(), |
| 49 | 'added_indexes' => array()), |
| 50 | $_migrationTableName = 'migration_version', |
| 51 | $_migrationClassesDirectory = array(), |
| 52 | $_migrationClasses = array(), |
| 53 | $_loadedMigrations = array(); |
| 54 | |
| 55 | /** |
| 56 | * construct |
| 57 | * |
| 58 | * Specify the path to the directory with the migration classes. |
| 59 | * The classes will be loaded and the migration table will be created if it does not already exist |
| 60 | * |
| 61 | * @param string $directory |
| 62 | * @return void |
| 63 | */ |
| 64 | public function __construct($directory = null) |
| 65 | { |
| 66 | if ($directory != null) { |
| 67 | $this->_migrationClassesDirectory = $directory; |
| 68 | |
| 69 | $this->loadMigrationClasses(); |
| 70 | |
| 71 | $this->createMigrationTable(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * getTableName |
| 77 | * |
| 78 | * @return void |
| 79 | */ |
| 80 | public function getTableName() |
| 81 | { |
| 82 | return $this->_migrationTableName; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * setTableName |
| 87 | * |
| 88 | * @param string $tableName |
| 89 | * @return void |
| 90 | */ |
| 91 | public function setTableName($tableName) |
| 92 | { |
| 93 | $this->_migrationTableName = Doctrine_Manager::connection() |
| 94 | ->formatter->getTableName($tableName); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * createMigrationTable |
| 99 | * |
| 100 | * Creates the migration table used to store the current version |
| 101 | * |
| 102 | * @return void |
| 103 | */ |
| 104 | protected function createMigrationTable() |
| 105 | { |
| 106 | $conn = Doctrine_Manager::connection(); |
| 107 | |
| 108 | try { |
| 109 | $conn->export->createTable($this->_migrationTableName, array('version' => array('type' => 'integer', 'size' => 11))); |
| 110 | |
| 111 | return true; |
| 112 | } catch(Exception $e) { |
| 113 | return false; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * loadMigrationClassesFromDirectory |
| 120 | * |
| 121 | * refactored out from loadMigrationClasses |
| 122 | * $param array An array of classes |
| 123 | * @return void |
| 124 | */ |
| 125 | public function loadMigrationClassesFromDirectory() |
| 126 | { |
| 127 | $classes = get_declared_classes(); |
| 128 | |
| 129 | foreach ((array) $this->_migrationClassesDirectory as $dir) { |
| 130 | $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), |
| 131 | RecursiveIteratorIterator::LEAVES_ONLY); |
| 132 | |
| 133 | foreach ($it as $file) { |
| 134 | $e = explode('.', $file->getFileName()); |
| 135 | if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) { |
| 136 | if ( ! in_array($file->getFileName(), $this->_loadedMigrations)) { |
| 137 | require_once($file->getPathName()); |
| 138 | |
| 139 | $requiredClass = array_diff(get_declared_classes(), $classes); |
| 140 | $requiredClass = end($requiredClass); |
| 141 | |
| 142 | if ($requiredClass) { |
| 143 | $this->_loadedMigrations[$requiredClass] = $file->getFileName(); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * loadMigrationClasses |
| 153 | * |
| 154 | * Loads the migration classes for the directory specified by the constructor |
| 155 | * |
| 156 | * @return void |
| 157 | */ |
| 158 | protected function loadMigrationClasses() |
| 159 | { |
| 160 | if ($this->_migrationClassesDirectory !== null) { |
| 161 | $this->loadMigrationClassesFromDirectory(); |
| 162 | } |
| 163 | |
| 164 | $parent = new ReflectionClass('Doctrine_Migration'); |
| 165 | |
| 166 | foreach ($this->_loadedMigrations as $name => $fileName) { |
| 167 | $class = new ReflectionClass($name); |
| 168 | |
| 169 | while ($class->isSubclassOf($parent)) { |
| 170 | |
| 171 | $class = $class->getParentClass(); |
| 172 | if ($class === false) { |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if ($class === false) { |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | $e = explode('_', $fileName); |
| 182 | $classMigrationNum = (int) $e[0]; |
| 183 | |
| 184 | $this->_migrationClasses[$classMigrationNum] = array('className' => $name, 'fileName' => $fileName); |
| 185 | } |
| 186 | |
| 187 | return $this->_migrationClasses; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * getMigrationClasses |
| 192 | * |
| 193 | * @return void |
| 194 | */ |
| 195 | public function getMigrationClasses() |
| 196 | { |
| 197 | return $this->_migrationClasses; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * setCurrentVersion |
| 202 | * |
| 203 | * Sets the current version in the migration table |
| 204 | * |
| 205 | * @param string $number |
| 206 | * @return void |
| 207 | */ |
| 208 | protected function setCurrentVersion($number) |
| 209 | { |
| 210 | $conn = Doctrine_Manager::connection(); |
| 211 | |
| 212 | if ($this->hasMigrated()) { |
| 213 | $conn->exec("UPDATE " . $this->_migrationTableName . " SET version = $number"); |
| 214 | } else { |
| 215 | $conn->exec("INSERT INTO " . $this->_migrationTableName . " (version) VALUES ($number)"); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * getCurrentVersion |
| 221 | * |
| 222 | * Get the current version of the database |
| 223 | * |
| 224 | * @return void |
| 225 | */ |
| 226 | public function getCurrentVersion() |
| 227 | { |
| 228 | $conn = Doctrine_Manager::connection(); |
| 229 | |
| 230 | $result = $conn->fetchColumn("SELECT version FROM " . $this->_migrationTableName); |
| 231 | |
| 232 | return isset($result[0]) ? $result[0]:0; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * hasMigrated |
| 237 | * |
| 238 | * Returns true/false for whether or not this database has been migrated in the past |
| 239 | * |
| 240 | * @return void |
| 241 | */ |
| 242 | public function hasMigrated() |
| 243 | { |
| 244 | $conn = Doctrine_Manager::connection(); |
| 245 | |
| 246 | $result = $conn->fetchColumn("SELECT version FROM " . $this->_migrationTableName); |
| 247 | |
| 248 | return isset($result[0]) ? true:false; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * getLatestVersion |
| 253 | * |
| 254 | * Gets the latest possible version from the loaded migration classes |
| 255 | * |
| 256 | * @return void |
| 257 | */ |
| 258 | public function getLatestVersion() |
| 259 | { |
| 260 | $this->loadMigrationClasses(); |
| 261 | |
| 262 | $versions = array(); |
| 263 | foreach (array_keys($this->_migrationClasses) as $classMigrationNum) { |
| 264 | $versions[$classMigrationNum] = $classMigrationNum; |
| 265 | } |
| 266 | |
| 267 | rsort($versions); |
| 268 | |
| 269 | return isset($versions[0]) ? $versions[0]:0; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * getNextVersion |
| 274 | * |
| 275 | * @return integer $nextVersion |
| 276 | */ |
| 277 | public function getNextVersion() |
| 278 | { |
| 279 | return $this->getLatestVersion() + 1; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * getMigrationClass |
| 284 | * |
| 285 | * Get instance of migration class for $num |
| 286 | * |
| 287 | * @param string $num |
| 288 | * @return void |
| 289 | */ |
| 290 | protected function getMigrationClass($num) |
| 291 | { |
| 292 | foreach ($this->_migrationClasses as $classMigrationNum => $info) { |
| 293 | $className = $info['className']; |
| 294 | |
| 295 | if ($classMigrationNum == $num) { |
| 296 | return new $className(); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | throw new Doctrine_Migration_Exception('Could not find migration class for migration step: '.$num); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * doMigrateStep |
| 305 | * |
| 306 | * Perform migration directory for the specified version. Loads migration classes and performs the migration then processes the changes |
| 307 | * |
| 308 | * @param string $direction |
| 309 | * @param string $num |
| 310 | * @return void |
| 311 | */ |
| 312 | protected function doMigrateStep($direction, $num) |
| 313 | { |
| 314 | $migrate = $this->getMigrationClass($num); |
| 315 | |
| 316 | $migrate->doMigrate($direction); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * doMigrate |
| 321 | * |
| 322 | * Perform migration for a migration class. Executes the up or down method then processes the changes |
| 323 | * |
| 324 | * @param string $direction |
| 325 | * @return void |
| 326 | */ |
| 327 | protected function doMigrate($direction) |
| 328 | { |
| 329 | $method = 'pre'.$direction; |
| 330 | $this->$method(); |
| 331 | |
| 332 | if (method_exists($this, $direction)) { |
| 333 | $this->$direction(); |
| 334 | |
| 335 | foreach ($this->_changes as $type => $changes) { |
| 336 | $process = new Doctrine_Migration_Process(); |
| 337 | $funcName = 'process' . Doctrine::classify($type); |
| 338 | |
| 339 | if ( ! empty($changes)) { |
| 340 | $process->$funcName($changes); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | $method = 'post'.$direction; |
| 346 | $this->$method(); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * migrate |
| 351 | * |
| 352 | * Perform a migration chain by specifying the $from and $to. |
| 353 | * If you do not specify a $from or $to then it will attempt to migrate from the current version to the latest version |
| 354 | * |
| 355 | * @param string $from |
| 356 | * @param string $to |
| 357 | * @return void |
| 358 | */ |
| 359 | public function migrate($to = null) |
| 360 | { |
| 361 | $from = $this->getCurrentVersion(); |
| 362 | |
| 363 | // If nothing specified then lets assume we are migrating from the current version to the latest version |
| 364 | if ($to === null) { |
| 365 | $to = $this->getLatestVersion(); |
| 366 | } |
| 367 | |
| 368 | if ($from == $to) { |
| 369 | throw new Doctrine_Migration_Exception('Already at version # ' . $to); |
| 370 | } |
| 371 | |
| 372 | $direction = $from > $to ? 'down':'up'; |
| 373 | |
| 374 | if ($direction === 'up') { |
| 375 | for ($i = $from + 1; $i <= $to; $i++) { |
| 376 | $this->doMigrateStep($direction, $i); |
| 377 | } |
| 378 | } else { |
| 379 | for ($i = $from; $i > $to; $i--) { |
| 380 | $this->doMigrateStep($direction, $i); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | $this->setCurrentVersion($to); |
| 385 | |
| 386 | return $to; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * addChange |
| 391 | * |
| 392 | * @param string $type |
| 393 | * @param string $array |
| 394 | * @return void |
| 395 | */ |
| 396 | protected function addChange($type, array $change = array()) |
| 397 | { |
| 398 | $this->_changes[$type][] = $change; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * createTable |
| 403 | * |
| 404 | * @param string $tableName |
| 405 | * @param string $array |
| 406 | * @param string $array |
| 407 | * @return void |
| 408 | */ |
| 409 | public function createTable($tableName, array $fields = array(), array $options = array()) |
| 410 | { |
| 411 | $options = get_defined_vars(); |
| 412 | |
| 413 | $this->addChange('created_tables', $options); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * dropTable |
| 418 | * |
| 419 | * @param string $tableName |
| 420 | * @return void |
| 421 | */ |
| 422 | public function dropTable($tableName) |
| 423 | { |
| 424 | $options = get_defined_vars(); |
| 425 | |
| 426 | $this->addChange('dropped_tables', $options); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * renameTable |
| 431 | * |
| 432 | * @param string $oldTableName |
| 433 | * @param string $newTableName |
| 434 | * @return void |
| 435 | */ |
| 436 | public function renameTable($oldTableName, $newTableName) |
| 437 | { |
| 438 | $options = get_defined_vars(); |
| 439 | |
| 440 | $this->addChange('renamed_tables', $options); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * createConstraint |
| 445 | * |
| 446 | * @param string $tableName |
| 447 | * @param string $constraintName |
| 448 | * @return void |
| 449 | */ |
| 450 | public function createConstraint($tableName, $constraintName, array $definition) |
| 451 | { |
| 452 | $options = get_defined_vars(); |
| 453 | |
| 454 | $this->addChange('created_constraints', $options); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * dropConstraint |
| 459 | * |
| 460 | * @param string $tableName |
| 461 | * @param string $constraintName |
| 462 | * @return void |
| 463 | */ |
| 464 | public function dropConstraint($tableName, $constraintName, $primary = false) |
| 465 | { |
| 466 | $options = get_defined_vars(); |
| 467 | |
| 468 | $this->addChange('dropped_constraints', $options); |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * createForeignKey |
| 473 | * |
| 474 | * @param string $tableName |
| 475 | * @param string $constraintName |
| 476 | * @return void |
| 477 | */ |
| 478 | public function createForeignKey($tableName, array $definition) |
| 479 | { |
| 480 | $options = get_defined_vars(); |
| 481 | |
| 482 | $this->addChange('created_fks', $options); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * dropForeignKey |
| 487 | * |
| 488 | * @param string $tableName |
| 489 | * @param string $constraintName |
| 490 | * @return void |
| 491 | */ |
| 492 | public function dropForeignKey($tableName, $fkName) |
| 493 | { |
| 494 | $options = get_defined_vars(); |
| 495 | |
| 496 | $this->addChange('dropped_fks', $options); |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * addColumn |
| 501 | * |
| 502 | * @param string $tableName |
| 503 | * @param string $columnName |
| 504 | * @param string $type |
| 505 | * @param string $array |
| 506 | * @return void |
| 507 | */ |
| 508 | public function addColumn($tableName, $columnName, $type, array $options = array()) |
| 509 | { |
| 510 | $options = get_defined_vars(); |
| 511 | |
| 512 | $this->addChange('added_columns', $options); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * renameColumn |
| 517 | * |
| 518 | * @param string $tableName |
| 519 | * @param string $oldColumnName |
| 520 | * @param string $newColumnName |
| 521 | * @return void |
| 522 | */ |
| 523 | public function renameColumn($tableName, $oldColumnName, $newColumnName) |
| 524 | { |
| 525 | $options = get_defined_vars(); |
| 526 | |
| 527 | $this->addChange('renamed_columns', $options); |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * renameColumn |
| 532 | * |
| 533 | * @param string $tableName |
| 534 | * @param string $columnName |
| 535 | * @param string $type |
| 536 | * @param string $array |
| 537 | * @return void |
| 538 | */ |
| 539 | public function changeColumn($tableName, $columnName, $type, array $options = array()) |
| 540 | { |
| 541 | $options = get_defined_vars(); |
| 542 | |
| 543 | $this->addChange('changed_columns', $options); |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * removeColumn |
| 548 | * |
| 549 | * @param string $tableName |
| 550 | * @param string $columnName |
| 551 | * @return void |
| 552 | */ |
| 553 | public function removeColumn($tableName, $columnName) |
| 554 | { |
| 555 | $options = get_defined_vars(); |
| 556 | |
| 557 | $this->addChange('removed_columns', $options); |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * addIndex |
| 562 | * |
| 563 | * @param string $tableName |
| 564 | * @param string $indexName |
| 565 | * @param string $array |
| 566 | * @return void |
| 567 | */ |
| 568 | public function addIndex($tableName, $indexName, array $definition) |
| 569 | { |
| 570 | $options = get_defined_vars(); |
| 571 | |
| 572 | $this->addChange('added_indexes', $options); |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * removeIndex |
| 577 | * |
| 578 | * @param string $tableName |
| 579 | * @param string $indexName |
| 580 | * @return void |
| 581 | */ |
| 582 | public function removeIndex($tableName, $indexName) |
| 583 | { |
| 584 | $options = get_defined_vars(); |
| 585 | |
| 586 | $this->addChange('removed_indexes', $options); |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * preUp |
| 591 | * |
| 592 | * @return void |
| 593 | */ |
| 594 | public function preUp() |
| 595 | { |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * postUp |
| 601 | * |
| 602 | * @return void |
| 603 | */ |
| 604 | public function postUp() |
| 605 | { |
| 606 | return; |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * preDown |
| 611 | * |
| 612 | * @return void |
| 613 | */ |
| 614 | public function preDown() |
| 615 | { |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | /** |
| 620 | * postDown |
| 621 | * |
| 622 | * @return void |
| 623 | */ |
| 624 | public function postDown() |
| 625 | { |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | } |