Ejemplo n.º 1
0
 /**
  * Returns a SchemaDiff object containing the differences between the schemas $fromSchema and $toSchema.
  *
  * The returned differences are returned in such a way that they contain the
  * operations to change the schema stored in $fromSchema to the schema that is
  * stored in $toSchema.
  *
  * @param Schema $fromSchema
  * @param Schema $toSchema
  *
  * @return SchemaDiff
  */
 public function compare(Schema $fromSchema, Schema $toSchema)
 {
     $diff = new SchemaDiff();
     $foreignKeysToTable = array();
     foreach ($toSchema->getTables() as $table) {
         $tableName = $table->getShortestName($toSchema->getName());
         if (!$fromSchema->hasTable($tableName)) {
             $diff->newTables[$tableName] = $toSchema->getTable($tableName);
         } else {
             $tableDifferences = $this->diffTable($fromSchema->getTable($tableName), $toSchema->getTable($tableName));
             if ($tableDifferences !== false) {
                 $diff->changedTables[$tableName] = $tableDifferences;
             }
         }
     }
     /* Check if there are tables removed */
     foreach ($fromSchema->getTables() as $table) {
         $tableName = $table->getShortestName($fromSchema->getName());
         $table = $fromSchema->getTable($tableName);
         if (!$toSchema->hasTable($tableName)) {
             $diff->removedTables[$tableName] = $table;
         }
         // also remember all foreign keys that point to a specific table
         foreach ($table->getForeignKeys() as $foreignKey) {
             $foreignTable = strtolower($foreignKey->getForeignTableName());
             if (!isset($foreignKeysToTable[$foreignTable])) {
                 $foreignKeysToTable[$foreignTable] = array();
             }
             $foreignKeysToTable[$foreignTable][] = $foreignKey;
         }
     }
     foreach ($diff->removedTables as $tableName => $table) {
         if (isset($foreignKeysToTable[$tableName])) {
             $diff->orphanedForeignKeys = array_merge($diff->orphanedForeignKeys, $foreignKeysToTable[$tableName]);
         }
     }
     foreach ($toSchema->getSequences() as $sequence) {
         $sequenceName = $sequence->getShortestName($toSchema->getName());
         if (!$fromSchema->hasSequence($sequenceName)) {
             $diff->newSequences[] = $sequence;
         } else {
             if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) {
                 $diff->changedSequences[] = $toSchema->getSequence($sequenceName);
             }
         }
     }
     foreach ($fromSchema->getSequences() as $sequence) {
         if ($this->isAutoIncrementSequenceInSchema($toSchema, $sequence)) {
             continue;
         }
         $sequenceName = $sequence->getShortestName($fromSchema->getName());
         if (!$toSchema->hasSequence($sequenceName)) {
             $diff->removedSequences[] = $sequence;
         }
     }
     return $diff;
 }
Ejemplo n.º 2
0
 /**
  * Returns a SchemaDiff object containing the differences between the schemas $fromSchema and $toSchema.
  *
  * The returned diferences are returned in such a way that they contain the
  * operations to change the schema stored in $fromSchema to the schema that is
  * stored in $toSchema.
  *
  * @param Schema $fromSchema
  * @param Schema $toSchema
  *
  * @return SchemaDiff
  */
 public function compare(Schema $fromSchema, Schema $toSchema)
 {
     if ($fromSchema->hasExplicitForeignKeyIndexes() && !$toSchema->hasExplicitForeignKeyIndexes()) {
         $toSchema->visit(new \Doctrine\DBAL\Schema\Visitor\FixSchema(true));
     }
     $diff = new SchemaDiff();
     $foreignKeysToTable = array();
     foreach ($toSchema->getTables() as $tableName => $table) {
         if (!$fromSchema->hasTable($tableName)) {
             $diff->newTables[$tableName] = $table;
         } else {
             $tableDifferences = $this->diffTable($fromSchema->getTable($tableName), $table);
             if ($tableDifferences !== false) {
                 $diff->changedTables[$tableName] = $tableDifferences;
             }
         }
     }
     /* Check if there are tables removed */
     foreach ($fromSchema->getTables() as $tableName => $table) {
         if (!$toSchema->hasTable($tableName)) {
             $diff->removedTables[$tableName] = $table;
         }
         // also remember all foreign keys that point to a specific table
         foreach ($table->getForeignKeys() as $foreignKey) {
             $foreignTable = strtolower($foreignKey->getForeignTableName());
             if (!isset($foreignKeysToTable[$foreignTable])) {
                 $foreignKeysToTable[$foreignTable] = array();
             }
             $foreignKeysToTable[$foreignTable][] = $foreignKey;
         }
     }
     foreach ($diff->removedTables as $tableName => $table) {
         if (isset($foreignKeysToTable[$tableName])) {
             $diff->orphanedForeignKeys = array_merge($diff->orphanedForeignKeys, $foreignKeysToTable[$tableName]);
         }
     }
     foreach ($toSchema->getSequences() as $sequenceName => $sequence) {
         if (!$fromSchema->hasSequence($sequenceName)) {
             $diff->newSequences[] = $sequence;
         } else {
             if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) {
                 $diff->changedSequences[] = $fromSchema->getSequence($sequenceName);
             }
         }
     }
     foreach ($fromSchema->getSequences() as $sequenceName => $sequence) {
         if (!$toSchema->hasSequence($sequenceName)) {
             $diff->removedSequences[] = $sequence;
         }
     }
     return $diff;
 }
Ejemplo n.º 3
0
 /**
  * Returns a SchemaDiff object containing the differences between the schemas $fromSchema and $toSchema.
  *
  * The returned differences are returned in such a way that they contain the
  * operations to change the schema stored in $fromSchema to the schema that is
  * stored in $toSchema.
  *
  * @param \Doctrine\DBAL\Schema\Schema $fromSchema
  * @param \Doctrine\DBAL\Schema\Schema $toSchema
  *
  * @return \Doctrine\DBAL\Schema\SchemaDiff
  */
 public function compare(Schema $fromSchema, Schema $toSchema)
 {
     $diff = new SchemaDiff();
     $diff->fromSchema = $fromSchema;
     $foreignKeysToTable = array();
     foreach ($toSchema->getTables() as $table) {
         $tableName = $table->getShortestName($toSchema->getName());
         if (!$fromSchema->hasTable($tableName)) {
             $diff->newTables[$tableName] = $toSchema->getTable($tableName);
         } else {
             $tableDifferences = $this->diffTable($fromSchema->getTable($tableName), $toSchema->getTable($tableName));
             if ($tableDifferences !== false) {
                 $diff->changedTables[$tableName] = $tableDifferences;
             }
         }
     }
     /* Check if there are tables removed */
     foreach ($fromSchema->getTables() as $table) {
         $tableName = $table->getShortestName($fromSchema->getName());
         $table = $fromSchema->getTable($tableName);
         if (!$toSchema->hasTable($tableName)) {
             $diff->removedTables[$tableName] = $table;
         }
         // also remember all foreign keys that point to a specific table
         foreach ($table->getForeignKeys() as $foreignKey) {
             $foreignTable = strtolower($foreignKey->getForeignTableName());
             if (!isset($foreignKeysToTable[$foreignTable])) {
                 $foreignKeysToTable[$foreignTable] = array();
             }
             $foreignKeysToTable[$foreignTable][] = $foreignKey;
         }
     }
     foreach ($diff->removedTables as $tableName => $table) {
         if (isset($foreignKeysToTable[$tableName])) {
             $diff->orphanedForeignKeys = array_merge($diff->orphanedForeignKeys, $foreignKeysToTable[$tableName]);
             // deleting duplicated foreign keys present on both on the orphanedForeignKey
             // and the removedForeignKeys from changedTables
             foreach ($foreignKeysToTable[$tableName] as $foreignKey) {
                 // strtolower the table name to make if compatible with getShortestName
                 $localTableName = strtolower($foreignKey->getLocalTableName());
                 if (isset($diff->changedTables[$localTableName])) {
                     foreach ($diff->changedTables[$localTableName]->removedForeignKeys as $key => $removedForeignKey) {
                         unset($diff->changedTables[$localTableName]->removedForeignKeys[$key]);
                     }
                 }
             }
         }
     }
     foreach ($toSchema->getSequences() as $sequence) {
         $sequenceName = $sequence->getShortestName($toSchema->getName());
         if (!$fromSchema->hasSequence($sequenceName)) {
             $diff->newSequences[] = $sequence;
         } else {
             if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) {
                 $diff->changedSequences[] = $toSchema->getSequence($sequenceName);
             }
         }
     }
     foreach ($fromSchema->getSequences() as $sequence) {
         if ($this->isAutoIncrementSequenceInSchema($toSchema, $sequence)) {
             continue;
         }
         $sequenceName = $sequence->getShortestName($fromSchema->getName());
         if (!$toSchema->hasSequence($sequenceName)) {
             $diff->removedSequences[] = $sequence;
         }
     }
     return $diff;
 }