SchemaTool.php 31.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
/*
 *  $Id$
 *
 * 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.doctrine-project.org>.
 */

namespace Doctrine\ORM\Tools;

24
use Doctrine\DBAL\Types\Type,
25 26
    Doctrine\ORM\EntityManager,
    Doctrine\ORM\Internal\CommitOrderCalculator;
27 28

/**
29
 * The SchemaTool is a tool to create/drop/update database schemas based on
30 31
 * <tt>ClassMetadata</tt> class descriptors.
 *
32 33 34 35 36 37 38
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link    www.doctrine-project.org
 * @since   2.0
 * @version $Revision$
 * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
 * @author  Jonathan Wage <jonwage@gmail.com>
 * @author  Roman Borschel <roman@code-factory.org>
39
 * @author  Benjamin Eberlei <kontakt@beberlei.de>
40 41 42
 */
class SchemaTool
{
43 44 45
    /**
     * @var \Doctrine\ORM\EntityManager
     */
46
    private $_em;
47 48 49 50

    /**
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
     */
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    private $_platform;

    /**
     * Initializes a new SchemaTool instance that uses the connection of the
     * provided EntityManager.
     *
     * @param Doctrine\ORM\EntityManager $em
     */
    public function __construct(EntityManager $em)
    {
        $this->_em = $em;
        $this->_platform = $em->getConnection()->getDatabasePlatform();
    }

    /**
     * Creates the database schema for the given array of ClassMetadata instances.
     *
     * @param array $classes
     */
    public function createSchema(array $classes)
    {
        $createSchemaSql = $this->getCreateSchemaSql($classes);
        $conn = $this->_em->getConnection();
74
        
75 76 77 78 79 80
        foreach ($createSchemaSql as $sql) {
            $conn->execute($sql);
        }
    }

    /**
81 82
     * Gets the list of DDL statements that are required to create the database schema for
     * the given list of ClassMetadata instances.
83 84
     *
     * @param array $classes
85
     * @return array $sql The SQL statements needed to create the schema for the classes.
86 87 88
     */
    public function getCreateSchemaSql(array $classes)
    {
89 90 91 92
        $sql = array(); // All SQL statements
        $processedClasses = array(); // Reminder for processed classes, used for hierarchies
        $foreignKeyConstraints = array(); // FK SQL statements. Appended to $sql at the end.
        $sequences = array(); // Sequence SQL statements. Appended to $sql at the end.
93 94

        foreach ($classes as $class) {
95
            if (isset($processedClasses[$class->name]) || $class->isMappedSuperclass) {
96
                continue;
97 98
            }

99
            $options = array(); // table options
100
            $columns = array(); // table columns
101
            
102
            if ($class->isInheritanceTypeSingleTable()) {
103 104 105
                $columns = $this->_gatherColumns($class, $options);
                $this->_gatherRelationsSql($class, $sql, $columns, $foreignKeyConstraints);
                
106 107 108 109 110
                // Add the discriminator column
                $discrColumnDef = $this->_getDiscriminatorColumnDefinition($class);
                $columns[$discrColumnDef['name']] = $discrColumnDef;

                // Aggregate all the information from all classes in the hierarchy
111
                foreach ($class->parentClasses as $parentClassName) {
112 113 114
                    // Parent class information is already contained in this class
                    $processedClasses[$parentClassName] = true;
                }
115
                
116
                foreach ($class->subClasses as $subClassName) {
117 118 119
                    $subClass = $this->_em->getClassMetadata($subClassName);
                    $columns = array_merge($columns, $this->_gatherColumns($subClass, $options));
                    $this->_gatherRelationsSql($subClass, $sql, $columns, $foreignKeyConstraints);
120 121 122
                    $processedClasses[$subClassName] = true;
                }
            } else if ($class->isInheritanceTypeJoined()) {
123 124 125
                // Add all non-inherited fields as columns
                foreach ($class->fieldMappings as $fieldName => $mapping) {
                    if ( ! isset($mapping['inherited'])) {
romanb's avatar
romanb committed
126
                        $columnName = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
127
                        $columns[$columnName] = $this->_gatherColumn($class, $mapping, $options);
128 129 130 131 132 133 134 135 136 137 138 139 140
                    }
                }

                $this->_gatherRelationsSql($class, $sql, $columns, $foreignKeyConstraints);

                // Add the discriminator column only to the root table
                if ($class->name == $class->rootEntityName) {
                    $discrColumnDef = $this->_getDiscriminatorColumnDefinition($class);
                    $columns[$discrColumnDef['name']] = $discrColumnDef;
                } else {
                    // Add an ID FK column to child tables
                    $idMapping = $class->fieldMappings[$class->identifier[0]];
                    $idColumn = $this->_gatherColumn($class, $idMapping, $options);
141
                    
142
                    unset($idColumn['autoincrement']);
143
                    
144
                    $columns[$idColumn['name']] = $idColumn;
145
                    
146 147
                    // Add a FK constraint on the ID column
                    $constraint = array();
148 149 150 151
                    $constraint['tableName'] = $class->getQuotedTableName($this->_platform);
                    $constraint['foreignTable'] = $this->_em->getClassMetadata($class->rootEntityName)->getQuotedTableName($this->_platform);
                    $constraint['local'] = array($idColumn['name']);
                    $constraint['foreign'] = array($idColumn['name']);
152 153 154
                    $constraint['onDelete'] = 'CASCADE';
                    $foreignKeyConstraints[] = $constraint;
                }
155
            } else if ($class->isInheritanceTypeTablePerClass()) {
156
                throw DoctrineException::notSupported();
157 158 159
            } else {
                $columns = $this->_gatherColumns($class, $options);
                $this->_gatherRelationsSql($class, $sql, $columns, $foreignKeyConstraints);
160
            }
161 162 163 164
            
            if (isset($class->primaryTable['indexes'])) {
                $options['indexes'] = $class->primaryTable['indexes'];
            }
165
            
166 167 168
            if (isset($class->primaryTable['uniqueConstraints'])) {
                $options['uniqueConstraints'] = $class->primaryTable['uniqueConstraints'];
            }
169

170
            $sql = array_merge($sql, $this->_platform->getCreateTableSql(
171 172
                $class->getQuotedTableName($this->_platform), $columns, $options)
            );
173

174
            $processedClasses[$class->name] = true;
175

176 177
            // TODO if we're reusing the sequence previously defined (in another model),
            // it should not attempt to create a new sequence.
178
            if ($class->isIdGeneratorSequence() && $class->name == $class->rootEntityName) {
179 180 181 182 183 184 185
                $seqDef = $class->getSequenceGeneratorDefinition();
                $sequences[] = $this->_platform->getCreateSequenceSql(
                    $seqDef['sequenceName'],
                    $seqDef['initialValue'],
                    $seqDef['allocationSize']
                );
            }
186 187
        }

188
        // Append the foreign key constraints SQL
189 190
        if ($this->_platform->supportsForeignKeyConstraints()) {
            foreach ($foreignKeyConstraints as $fkConstraint) {
191
                $sql = array_merge($sql, (array) $this->_platform->getCreateForeignKeySql($fkConstraint['tableName'], $fkConstraint));
192 193 194
            }
        }

195 196 197
        // Append the sequence SQL
        $sql = array_merge($sql, $sequences);

198 199 200
        return $sql;
    }

201 202 203 204 205 206 207 208
    /**
     * Gets a portable column definition as required by the DBAL for the discriminator
     * column of a class.
     * 
     * @param ClassMetadata $class
     * @return array The portable column definition of the discriminator column as required by
     *              the DBAL.
     */
209 210
    private function _getDiscriminatorColumnDefinition($class)
    {
211
        $discrColumn = $class->discriminatorColumn;
212
        
213
        return array(
214
            'name' => $class->getQuotedDiscriminatorColumnName($this->_platform),
215 216 217 218 219 220
            'type' => Type::getType($discrColumn['type']),
            'length' => $discrColumn['length'],
            'notnull' => true
        );
    }

221
    /**
222 223
     * Gathers the column definitions as required by the DBAL of all field mappings
     * found in the given class.
224 225
     *
     * @param ClassMetadata $class
226 227 228
     * @param array $options The table options/constraints where any additional options/constraints
     *              that are required by columns should be appended.
     * @return array The list of portable column definitions as required by the DBAL.
229
     */
230
    private function _gatherColumns($class, array &$options)
231 232
    {
        $columns = array();
233
        
234
        foreach ($class->fieldMappings as $fieldName => $mapping) {
235 236
            $column = $this->_gatherColumn($class, $mapping, $options);
            $columns[$column['name']] = $column;
237
        }
238
        
239 240
        return $columns;
    }
241 242 243 244 245 246 247 248 249 250
    
    /**
     * Creates a column definition as required by the DBAL from an ORM field mapping definition.
     * 
     * @param ClassMetadata $class The class that owns the field mapping.
     * @param array $mapping The field mapping.
     * @param array $options The table options/constraints where any additional options/constraints
     *          required by the column should be appended.
     * @return array The portable column definition as required by the DBAL.
     */
251 252 253
    private function _gatherColumn($class, array $mapping, array &$options)
    {
        $column = array();
254
        $column['name'] = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
255
        $column['type'] = Type::getType($mapping['type']);
256
        $column['length'] = isset($mapping['length']) ? $mapping['length'] : null;
257
        $column['notnull'] = isset($mapping['nullable']) ? ! $mapping['nullable'] : true;
258
        $column['unique'] = isset($mapping['unique']) ? $mapping['unique'] : false;
259
        $column['version'] = $class->isVersioned && $class->versionField == $mapping['fieldName'] ? true : false;
260

261 262 263 264
        if(strtolower($column['type']) == 'string' && $column['length'] === null) {
            $column['length'] = 255;
        }

265 266 267
        if (isset($mapping['precision'])) {
            $column['precision'] = $mapping['precision'];
        }
268
        
269 270 271
        if (isset($mapping['scale'])) {
            $column['scale'] = $mapping['scale'];
        }
272
        
273 274 275
        if (isset($mapping['default'])) {
            $column['default'] = $mapping['default'];
        }
276
        
277 278 279
        if ($class->isIdentifier($mapping['fieldName'])) {
            $column['primary'] = true;
            $options['primary'][] = $mapping['columnName'];
280

281 282 283 284 285 286 287 288
            if ($class->isIdGeneratorIdentity()) {
                $column['autoincrement'] = true;
            }
        }

        return $column;
    }

289 290 291 292 293 294 295 296 297 298 299 300
    /**
     * Gathers the SQL for properly setting up the relations of the given class.
     * This includes the SQL for foreign key constraints and join tables.
     * 
     * @param ClassMetadata $class
     * @param array $sql The sequence of SQL statements where any new statements should be appended.
     * @param array $columns The list of columns in the class's primary table where any additional
     *          columns required by relations should be appended.
     * @param array $constraints The constraints of the table where any additional constraints
     *          required by relations should be appended.
     * @return void
     */
301 302
    private function _gatherRelationsSql($class, array &$sql, array &$columns, array &$constraints)
    {
303 304 305 306
        foreach ($class->associationMappings as $fieldName => $mapping) {
            if (isset($class->inheritedAssociationFields[$fieldName])) {
                continue;
            }
romanb's avatar
romanb committed
307

308
            $foreignClass = $this->_em->getClassMetadata($mapping->targetEntityName);
309
            
310
            if ($mapping->isOneToOne() && $mapping->isOwningSide) {
311
                $constraint = array();
312 313
                $constraint['tableName'] = $class->getQuotedTableName($this->_platform);
                $constraint['foreignTable'] = $foreignClass->getQuotedTableName($this->_platform);
314 315
                $constraint['local'] = array();
                $constraint['foreign'] = array();
316
                
317 318
                foreach ($mapping->getJoinColumns() as $joinColumn) {
                    $column = array();
319
                    $column['name'] = $mapping->getQuotedJoinColumnName($joinColumn['name'], $this->_platform);
320
                    $column['type'] = Type::getType($foreignClass->getTypeOfColumn($joinColumn['referencedColumnName']));
321 322
                    $columns[$column['name']] = $column;
                    $constraint['local'][] = $column['name'];
323
                    $constraint['foreign'][] = $joinColumn['referencedColumnName'];
324
                    
325 326 327
                    if (isset($joinColumn['onUpdate'])) {
                        $constraint['onUpdate'] = $joinColumn['onUpdate'];
                    }
328
                    
329 330 331
                    if (isset($joinColumn['onDelete'])) {
                        $constraint['onDelete'] = $joinColumn['onDelete'];
                    }
332
                }
333
                
334
                $constraints[] = $constraint;
335
            } else if ($mapping->isOneToMany() && $mapping->isOwningSide) {
336
                //... create join table, one-many through join table supported later
337
                throw DoctrineException::notSupported();
338
            } else if ($mapping->isManyToMany() && $mapping->isOwningSide) {
339 340 341 342
                // create join table
                $joinTableColumns = array();
                $joinTableOptions = array();
                $joinTable = $mapping->getJoinTable();
343 344
                
                // Build first FK constraint (relation table => source table)
345
                $constraint1 = array(
346 347
                    'tableName' => $mapping->getQuotedJoinTableName($this->_platform),
                    'foreignTable' => $class->getQuotedTableName($this->_platform),
348 349 350
                    'local' => array(),
                    'foreign' => array()
                );
351
                
352 353 354 355
                foreach ($joinTable['joinColumns'] as $joinColumn) {
                    $column = array();
                    $column['primary'] = true;
                    $joinTableOptions['primary'][] = $joinColumn['name'];
356
                    $column['name'] = $mapping->getQuotedJoinColumnName($joinColumn['name'], $this->_platform);
357
                    $column['type'] = Type::getType($class->getTypeOfColumn($joinColumn['referencedColumnName']));
358 359
                    $joinTableColumns[$column['name']] = $column;
                    $constraint1['local'][] = $column['name'];
360
                    $constraint1['foreign'][] = $joinColumn['referencedColumnName'];
361
                    
362 363 364
                    if (isset($joinColumn['onUpdate'])) {
                        $constraint1['onUpdate'] = $joinColumn['onUpdate'];
                    }
365
                    
366 367 368
                    if (isset($joinColumn['onDelete'])) {
                        $constraint1['onDelete'] = $joinColumn['onDelete'];
                    }
369
                }
370
                
371
                $constraints[] = $constraint1;
372 373
                
                // Build second FK constraint (relation table => target table)
374
                $constraint2 = array();
375 376
                $constraint2['tableName'] = $mapping->getQuotedJoinTableName($this->_platform);
                $constraint2['foreignTable'] = $foreignClass->getQuotedTableName($this->_platform);
377 378
                $constraint2['local'] = array();
                $constraint2['foreign'] = array();
379
                
380 381 382 383 384
                foreach ($joinTable['inverseJoinColumns'] as $inverseJoinColumn) {
                    $column = array();
                    $column['primary'] = true;
                    $joinTableOptions['primary'][] = $inverseJoinColumn['name'];
                    $column['name'] = $inverseJoinColumn['name'];
385 386
                    $column['type'] = Type::getType($this->_em->getClassMetadata($mapping->getTargetEntityName())
                            ->getTypeOfColumn($inverseJoinColumn['referencedColumnName']));
387 388 389
                    $joinTableColumns[$inverseJoinColumn['name']] = $column;
                    $constraint2['local'][] = $inverseJoinColumn['name'];
                    $constraint2['foreign'][] = $inverseJoinColumn['referencedColumnName'];
390
                    
391 392 393
                    if (isset($inverseJoinColumn['onUpdate'])) {
                        $constraint2['onUpdate'] = $inverseJoinColumn['onUpdate'];
                    }
394
                    
395 396 397
                    if (isset($joinColumn['onDelete'])) {
                        $constraint2['onDelete'] = $inverseJoinColumn['onDelete'];
                    }
398
                }
399
                
400
                $constraints[] = $constraint2;
401 402
                
                // Get the SQL for creating the join table and merge it with the others
403
                $sql = array_merge($sql, $this->_platform->getCreateTableSql(
404 405
                    $mapping->getQuotedJoinTableName($this->_platform), $joinTableColumns, $joinTableOptions)
                );
406 407 408
            }
        }
    }
409 410 411 412 413 414 415
    
    /**
     * Drops the database schema for the given classes.
     * 
     * @param array $classes
     * @return void
     */
416 417
    public function dropSchema(array $classes)
    {
418 419
        $dropSchemaSql = $this->getDropSchemaSql($classes);
        $conn = $this->_em->getConnection();
420
        
421 422 423
        foreach ($dropSchemaSql as $sql) {
            $conn->execute($sql);
        }
424
    }
425 426 427 428 429 430 431
    
    /**
     * Gets the SQL needed to drop the database schema for the given classes.
     * 
     * @param array $classes
     * @return array
     */
432 433
    public function getDropSchemaSql(array $classes)
    {
434 435
        $sql = array();
        
436
        $commitOrder = $this->_getCommitOrder($classes);
437 438 439 440 441 442
        $associationTables = $this->_getAssociationTables($commitOrder);
        
        // Drop association tables first
        foreach ($associationTables as $associationTable) {
            $sql[] = $this->_platform->getDropTableSql($associationTable);
        }
443

444 445 446
        // Drop tables in reverse commit order
        for ($i = count($commitOrder) - 1; $i >= 0; --$i) {
            $class = $commitOrder[$i];
447
            
jwage's avatar
jwage committed
448 449
            if (($class->isInheritanceTypeSingleTable() && $class->name != $class->rootEntityName)
                || $class->isMappedSuperclass) {
450 451
                continue;
            }
452
            
453 454 455
            $sql[] = $this->_platform->getDropTableSql($class->getTableName());
        }
        
456 457
        //TODO: Drop other schema elements, like sequences etc.
        
458
        return $sql;
459
    }
460
    
461 462 463 464 465 466 467
    /**
     * Updates the database schema of the given classes by comparing the ClassMetadata
     * instances to the current database schema that is inspected.
     * 
     * @param array $classes
     * @return void
     */
468 469
    public function updateSchema(array $classes)
    {
470 471
        $updateSchemaSql = $this->getUpdateSchemaSql($classes);
        $conn = $this->_em->getConnection();
472
        
473 474 475
        foreach ($updateSchemaSql as $sql) {
            $conn->execute($sql);
        }
476 477
    }
    
478 479 480 481 482 483 484
    /**
     * Gets the sequence of SQL statements that need to be performed in order
     * to bring the given class mappings in-synch with the relational schema.
     * 
     * @param array $classes The classes to consider.
     * @return array The sequence of SQL statements.
     */
485 486
    public function getUpdateSchemaSql(array $classes)
    {
487 488 489 490 491 492 493 494 495 496
        $sql = array();
        $conn = $this->_em->getConnection();
        $sm = $conn->getSchemaManager();
        
        $tables = $sm->listTables();
        $newClasses = array();
        
        foreach ($classes as $class) {
            $tableName = $class->getTableName();
            $tableExists = false;
497
            
498 499 500
            foreach ($tables as $index => $table) {
                if ($tableName == $table) {
                    $tableExists = true;
501
                    
502 503 504 505
                    unset($tables[$index]);
                    break;
                }
            }
506
            
507 508 509 510
            if ( ! $tableExists) {
                $newClasses[] = $class;
            } else {
                $newFields = array();
511 512
                $updateFields = array();
                $dropIndexes = array();
513 514 515
                $newJoinColumns = array();
                $currentColumns = $sm->listTableColumns($tableName);
                                
516
                foreach ($class->fieldMappings as $fieldName => $fieldMapping) {
517
                    $exists = false;
518
                    
519 520 521
                    foreach ($currentColumns as $index => $column) {
                        if ($column['name'] == $fieldMapping['columnName']) {
                            // Column exists, check for changes
522 523
                            $columnInfo = $column;
                            $columnChanged = false;
jwage's avatar
jwage committed
524
                                                        
525
                            // 1. check for nullability change
526 527 528 529 530 531 532 533 534 535 536
                            $columnInfo['notnull'] = ( ! isset($columnInfo['notnull'])) 
                                ? false : $columnInfo['notnull'];
                            $notnull = ! $class->isNullable($fieldName);
                            
                            if ($columnInfo['notnull'] != $notnull) {
                                $columnInfo['notnull'] = $notnull;
                                $columnChanged = true;
                            }
                            
                            unset($notnull);
                            
537
                            // 2. check for uniqueness change
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
                            $columnInfo['unique'] = ( ! isset($columnInfo['unique'])) 
                                ? false : $columnInfo['unique'];
                            $unique = $class->isUniqueField($fieldName);
                            
                            if ($columnInfo['unique'] != $unique) {
                                // We need to call a special DROP INDEX if it was defined
                                if ($columnInfo['unique']) {
                                    $dropIndexes[] = $column['name'];
                                }
                                
                                $columnInfo['unique'] = $unique;
                                $columnChanged = true;
                            }
                            
                            unset($unique);
                            
                            // 3. check for type change
                            $type = Type::getType($fieldMapping['type']);
                            
                            if ($columnInfo['type'] != $type) {
                                $columnInfo['type'] = $type;
                                $columnChanged = true;
                            }
                            
                            unset($type);
                            
564 565
                            // 4. check for scale and precision change
                            if (strtolower($columnInfo['type']) == 'decimal') {
566 567 568 569 570 571 572 573
                                /*// Doesn't work yet, see DDC-89
                                if($columnInfo['length'] != $fieldMapping['precision'] ||
                                   $columnInfo['scale'] != $fieldMapping['scale']) {

                                    $columnInfo['length'] = $fieldMapping['precision'];
                                    $columnInfo['scale'] = $fieldMapping['scale'];
                                    $columnChanged = true;
                                }*/
574 575 576 577 578 579 580
                            }
                            // 5. check for length change of strings
                            elseif(strtolower($fieldMapping['type']) == 'string') {
                                if(!isset($fieldMapping['length']) || $fieldMapping['length'] === null) {
                                    $fieldMapping['length'] = 255;
                                }

581 582 583 584 585
                                if($columnInfo['length'] != $fieldMapping['length']) {
                                    $columnInfo['length'] = $fieldMapping['length'];
                                    $columnChanged = true;
                                }
                            }
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
                            
                            // 6. check for flexible and fixed length
                            $fieldMapping['fixed'] = ( ! isset($fieldMapping['fixed'])) 
                                ? false : $fieldMapping['fixed'];
                                
                            if ($columnInfo['fixed'] != $fieldMapping['fixed']) {
                                $columnInfo['fixed'] = $fieldMapping['fixed'];
                                $columnChanged = true;
                            }
                            
                            // Only add to column changed list if it was actually changed
                            if ($columnChanged) {
                                $updateFields[] = $columnInfo;
                            }
                            
601 602 603 604 605
                            unset($currentColumns[$index]);
                            $exists = true;
                            break;
                        }
                    }
606
                    
607 608 609 610 611 612 613 614 615
                    if ( ! $exists) {
                        $newFields[] = $fieldMapping;
                    }
                }
                
                foreach ($class->associationMappings as $assoc) {
                    if ($assoc->isOwningSide && $assoc->isOneToOne()) {
                        foreach ($assoc->targetToSourceKeyColumns as $targetColumn => $sourceColumn) {
                            $exists = false;
616
                            
617 618 619 620 621 622 623 624 625 626 627
                            foreach ($currentColumns as $index => $column) {
                                if ($column['name'] == $sourceColumn) {
                                    // Column exists, check for changes
                                    
                                    // 1. check for nullability change
                                    
                                    unset($currentColumns[$index]);
                                    $exists = true;
                                    break;
                                }
                            }
628
                            
629 630 631 632 633 634 635 636 637 638
                            if ( ! $exists) {
                                $newJoinColumns[$sourceColumn] = array(
                                    'name' => $sourceColumn,
                                    'type' => 'integer' //FIXME!!!
                                );
                            }
                        }
                    }
                }
                
639 640 641 642 643 644 645 646
                // Drop indexes
                if ($dropIndexes) {
                    foreach ($dropIndexes as $dropIndex) {
                        $sql[] = $this->_platform->getDropIndexSql($tableName, $dropIndex);
                    }
                }
                
                // Create new columns
647 648
                if ($newFields || $newJoinColumns) {
                    $changes = array();
649
                    
650 651 652 653
                    foreach ($newFields as $newField) {
                        $options = array();
                        $changes['add'][$newField['columnName']] = $this->_gatherColumn($class, $newField, $options);
                    }
654
                    
655
                    foreach ($newJoinColumns as $name => $joinColumn) {
jwage's avatar
jwage committed
656
                        $joinColumn['type'] = Type::getType($joinColumn['type']);
657 658
                        $changes['add'][$name] = $joinColumn;
                    }
659
                    $sql = array_merge($sql, $this->_platform->getAlterTableSql($tableName, $changes));
660 661
                }
                
662 663 664 665 666 667 668 669 670 671 672
                // Update existent columns
                if ($updateFields) {
                    $changes = array();
                    
                    foreach ($updateFields as $updateField) {
                        // Now we pick the Type instance
                        $changes['change'][$updateField['name']] = array(
                            'definition' => $updateField
                        );
                    }
                    
673
                    $sql = array_merge($sql, $this->_platform->getAlterTableSql($tableName, $changes));
674 675
                }
                
676 677 678
                // Drop any remaining columns
                if ($currentColumns) {
                    $changes = array();
679
                    
680 681 682 683
                    foreach ($currentColumns as $column) {
                        $options = array();
                        $changes['remove'][$column['name']] = $column;
                    }
684
                    
685
                    $sql = array_merge($sql, $this->_platform->getAlterTableSql($tableName, $changes));
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
                }
            }
        }
        
        if ($newClasses) {
            $sql = array_merge($this->getCreateSchemaSql($newClasses), $sql);
        }
        
        // Drop any remaining tables (Probably not a good idea, because the given class list
        // may not be complete!)
        /*if ($tables) {
            foreach ($tables as $table) {
                $sql[] = $this->_platform->getDropTableSql($table);
            }
        }*/
        
        return $sql;
703
    }
704 705 706 707 708 709 710 711
    
    private function _getCommitOrder(array $classes)
    {
        $calc = new CommitOrderCalculator;

        // Calculate dependencies
        foreach ($classes as $class) {
            $calc->addClass($class);
712
            
713 714 715
            foreach ($class->associationMappings as $assoc) {
                if ($assoc->isOwningSide) {
                    $targetClass = $this->_em->getClassMetadata($assoc->targetEntityName);
716
                    
717 718 719
                    if ( ! $calc->hasClass($targetClass->name)) {
                        $calc->addClass($targetClass);
                    }
720
                    
721 722 723 724 725 726 727 728
                    // add dependency ($targetClass before $class)
                    $calc->addDependency($targetClass, $class);
                }
            }
        }

        return $calc->getCommitOrder();
    }
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
    
    private function _getAssociationTables(array $classes)
    {
        $associationTables = array();
        
        foreach ($classes as $class) {
            foreach ($class->associationMappings as $assoc) {
                if ($assoc->isOwningSide && $assoc->isManyToMany()) {
                    $associationTables[] = $assoc->joinTable['name'];
                }
            }
        }
        
        return $associationTables;
    }
744
}