LazyRecord\Schema\Comparator::compare PHP Метод

compare() публичный Метод

compare two schemas.
public compare ( SchemaInterface $before, SchemaInterface $after )
$before SchemaInterface schema before
$after SchemaInterface new schema
    public function compare(SchemaInterface $before, SchemaInterface $after)
    {
        $diff = array();
        $beforeColumns = $before ? $before->getColumns() : array();
        $afterColumns = $after ? $after->getColumns() : array();
        $columnKeys = array_unique(array_merge(array_keys($beforeColumns), array_keys($afterColumns)));
        foreach ($columnKeys as $key) {
            // If schema and db has the same column, we then compare the column definitions
            if (isset($beforeColumns[$key]) && isset($afterColumns[$key])) {
                $bc = $beforeColumns[$key];
                $ac = $afterColumns[$key];
                $afterc = $afterColumns[$key];
                $d = new ColumnDiff($key, 'M', $bc, $ac);
                // Compare the type info
                if (strtolower($bc->type) !== strtolower($ac->type)) {
                    $d->appendDetail(new AttributeDiff('type', strtolower($bc->buildTypeName($this->driver)), strtolower($ac->buildTypeName($this->driver))));
                }
                if ($bc->length !== $ac->length) {
                    $d->appendDetail(new AttributeDiff('length', $bc->buildTypeName($this->driver), $ac->buildTypeName($this->driver)));
                }
                if ($bc->decimals !== $ac->decimals) {
                    $d->appendDetail(new AttributeDiff('decimals', $bc->buildTypeName($this->driver), $ac->buildTypeName($this->driver)));
                }
                if ($bc->primary !== $ac->primary) {
                    $d->appendDetail(new AttributeDiff('primary', $bc->primary, $ac->primary));
                }
                // we only compare unsigned when:
                //   driver is MySQL or the column is not a primary key
                if ($this->driver instanceof MySQLDriver) {
                    if (!$ac->primary && !$bc->primary) {
                        if ($bc->unsigned != $ac->unsigned) {
                            $d->appendDetail(new AttributeDiff('unsigned', $bc->unsigned, $ac->unsigned));
                        }
                    }
                }
                if ($bc->notNull != $ac->notNull) {
                    $d->appendDetail(new AttributeDiff('notNull', $bc->notNull, $ac->notNull));
                }
                // They are the same column, let's compare these attributes
                $attributes = array('default');
                foreach ($attributes as $n) {
                    // Closure are meaningless
                    $aval = $ac->{$n};
                    $bval = $bc->{$n};
                    if ($aval instanceof Closure || $bval instanceof Closure) {
                        continue;
                    }
                    if ($aval === null && $bval === null || $aval === false && $bval === false) {
                        continue;
                    }
                    if (is_array($aval)) {
                        $aval = new Raw($aval[0]);
                    }
                    if (is_array($bval)) {
                        $bval = new Raw($bval[0]);
                    }
                    if ($aval instanceof Raw && $bval instanceof Raw && $aval->compare($bval) != 0) {
                        $d->appendDetail(new AttributeDiff($n, $aval, $bval));
                    } elseif (is_scalar($aval) && is_scalar($bval) && $aval != $bval) {
                        $d->appendDetail(new AttributeDiff($n, $aval, $bval));
                    }
                }
                if (count($d->details) > 0) {
                    $diff[] = $d;
                }
            } elseif (isset($beforeColumns[$key]) && !isset($afterColumns[$key])) {
                // flag: -
                $diff[] = new ColumnDiff($key, 'D', $beforeColumns[$key], null);
            } elseif (isset($afterColumns[$key]) && !isset($beforeColumns[$key])) {
                // flag: +
                $diff[] = new ColumnDiff($key, 'A', null, $afterColumns[$key]);
            }
        }
        return $diff;
    }

Usage Example

Пример #1
0
 public function execute()
 {
     $formatter = new \CLIFramework\Formatter();
     $options = $this->options;
     $logger = $this->logger;
     $connectionManager = \LazyRecord\ConnectionManager::getInstance();
     $dsId = $this->getCurrentDataSourceId();
     $conn = $connectionManager->getConnection($dsId);
     $driver = $connectionManager->getQueryDriver($dsId);
     $this->logger->info('Performing Comparison...');
     $parser = TableParser::create($driver, $conn);
     $existingTables = $parser->getTables();
     $tableSchemas = $parser->getDeclareSchemaMap();
     $found = false;
     $comparator = new Comparator();
     foreach ($tableSchemas as $table => $currentSchema) {
         $this->logger->debug("Checking table {$table}");
         $ref = new ReflectionObject($currentSchema);
         $filepath = $ref->getFilename();
         $filepath = substr($filepath, strlen(getcwd()) + 1);
         if (in_array($table, $existingTables)) {
             $before = $parser->reverseTableSchema($table);
             $diffs = $comparator->compare($before, $currentSchema);
             if (count($diffs)) {
                 $found = true;
                 $printer = new ComparatorConsolePrinter($diffs);
                 $printer->beforeName = $table . ":data source [{$dsId}]";
                 $printer->afterName = $table . ':' . $filepath;
                 $printer->output();
             }
         } else {
             $msg = sprintf("+ table %-20s %s", "'" . $table . "'", $filepath);
             echo $formatter->format($msg, 'green'), "\n";
             $a = isset($tableSchemas[$table]) ? $tableSchemas[$table] : null;
             $diff = $comparator->compare(new DeclareSchema(), $currentSchema);
             foreach ($diff as $diffItem) {
                 echo "  ", $diffItem->toColumnAttrsString(), "\n";
             }
             $found = true;
         }
     }
     if (!$found) {
         $this->logger->info("No diff found");
     }
 }
All Usage Examples Of LazyRecord\Schema\Comparator::compare