Exemplo n.º 1
0
 /**
  * Returns the difference between the tables $table1 and $table2.
  *
  * If there are no differences this method returns the boolean false.
  *
  * @param ezcDbSchemaTable $table1
  * @param ezcDbSchemaTable $table2
  *
  * @return bool|ezcDbSchemaTableDiff
  */
 private static final function diffTable(ezcDbSchemaTable $table1, ezcDbSchemaTable $table2)
 {
     $changes = 0;
     $tableDifferences = new ezcDbSchemaTableDiff();
     /* See if all the fields in table 1 exist in table 2 */
     foreach ($table2->fields as $fieldName => $fieldDefinition) {
         if (!isset($table1->fields[$fieldName])) {
             $tableDifferences->addedFields[$fieldName] = $fieldDefinition;
             $changes++;
         }
     }
     /* See if there are any removed fields in table 2 */
     foreach ($table1->fields as $fieldName => $fieldDefinition) {
         if (!isset($table2->fields[$fieldName])) {
             $tableDifferences->removedFields[$fieldName] = true;
             $changes++;
         }
     }
     /* See if there are any changed fieldDefinitioninitions */
     foreach ($table1->fields as $fieldName => $fieldDefinition) {
         if (isset($table2->fields[$fieldName])) {
             $fieldDifferences = ezcDbSchemaComparator::diffField($fieldDefinition, $table2->fields[$fieldName]);
             if ($fieldDifferences) {
                 $tableDifferences->changedFields[$fieldName] = $fieldDifferences;
                 $changes++;
             }
         }
     }
     $table1Indexes = $table1->indexes;
     $table2Indexes = $table2->indexes;
     /* See if all the indexes in table 1 exist in table 2 */
     foreach ($table2Indexes as $indexName => $indexDefinition) {
         if (!isset($table1Indexes[$indexName])) {
             $tableDifferences->addedIndexes[$indexName] = $indexDefinition;
             $changes++;
         }
     }
     /* See if there are any removed indexes in table 2 */
     foreach ($table1Indexes as $indexName => $indexDefinition) {
         if (!isset($table2Indexes[$indexName])) {
             $tableDifferences->removedIndexes[$indexName] = true;
             $changes++;
         }
     }
     /* See if there are any changed indexDefinitions */
     foreach ($table1Indexes as $indexName => $indexDefinition) {
         if (isset($table2Indexes[$indexName])) {
             $indexDifferences = ezcDbSchemaComparator::diffIndex($indexDefinition, $table2Indexes[$indexName]);
             if ($indexDifferences) {
                 $tableDifferences->changedIndexes[$indexName] = $indexDifferences;
                 $changes++;
             }
         }
     }
     return $changes ? $tableDifferences : false;
 }