DBDiff\DB\Schema\TableSchema::getDiff PHP Method

getDiff() public method

public getDiff ( $table )
    public function getDiff($table)
    {
        Logger::info("Now calculating schema diff for table `{$table}`");
        $diffSequence = [];
        $sourceSchema = $this->getSchema('source', $table);
        $targetSchema = $this->getSchema('target', $table);
        // Engine
        $sourceEngine = $sourceSchema['engine'];
        $targetEngine = $targetSchema['engine'];
        if ($sourceEngine != $targetEngine) {
            $diffSequence[] = new AlterTableEngine($table, $sourceEngine, $targetEngine);
        }
        // Collation
        $sourceCollation = $sourceSchema['collation'];
        $targetCollation = $targetSchema['collation'];
        if ($sourceCollation != $targetCollation) {
            $diffSequence[] = new AlterTableCollation($table, $sourceCollation, $targetCollation);
        }
        // Columns
        $sourceColumns = $sourceSchema['columns'];
        $targetColumns = $targetSchema['columns'];
        $differ = new MapDiffer();
        $diffs = $differ->doDiff($targetColumns, $sourceColumns);
        foreach ($diffs as $column => $diff) {
            if ($diff instanceof \Diff\DiffOp\DiffOpRemove) {
                $diffSequence[] = new AlterTableDropColumn($table, $column, $diff);
            } else {
                if ($diff instanceof \Diff\DiffOp\DiffOpChange) {
                    $diffSequence[] = new AlterTableChangeColumn($table, $column, $diff);
                } else {
                    if ($diff instanceof \Diff\DiffOp\DiffOpAdd) {
                        $diffSequence[] = new AlterTableAddColumn($table, $column, $diff);
                    }
                }
            }
        }
        // Keys
        $sourceKeys = $sourceSchema['keys'];
        $targetKeys = $targetSchema['keys'];
        $differ = new MapDiffer();
        $diffs = $differ->doDiff($targetKeys, $sourceKeys);
        foreach ($diffs as $key => $diff) {
            if ($diff instanceof \Diff\DiffOp\DiffOpRemove) {
                $diffSequence[] = new AlterTableDropKey($table, $key, $diff);
            } else {
                if ($diff instanceof \Diff\DiffOp\DiffOpChange) {
                    $diffSequence[] = new AlterTableChangeKey($table, $key, $diff);
                } else {
                    if ($diff instanceof \Diff\DiffOp\DiffOpAdd) {
                        $diffSequence[] = new AlterTableAddKey($table, $key, $diff);
                    }
                }
            }
        }
        // Constraints
        $sourceConstraints = $sourceSchema['constraints'];
        $targetConstraints = $targetSchema['constraints'];
        $differ = new MapDiffer();
        $diffs = $differ->doDiff($targetConstraints, $sourceConstraints);
        foreach ($diffs as $name => $diff) {
            if ($diff instanceof \Diff\DiffOp\DiffOpRemove) {
                $diffSequence[] = new AlterTableDropConstraint($table, $name, $diff);
            } else {
                if ($diff instanceof \Diff\DiffOp\DiffOpChange) {
                    $diffSequence[] = new AlterTableChangeConstraint($table, $name, $diff);
                } else {
                    if ($diff instanceof \Diff\DiffOp\DiffOpAdd) {
                        $diffSequence[] = new AlterTableAddConstraint($table, $name, $diff);
                    }
                }
            }
        }
        return $diffSequence;
    }

Usage Example

 public function getDiff($params)
 {
     // Connect and test accessibility
     $this->manager->connect($params);
     $this->manager->testResources($params);
     // Schema diff
     $schemaDiff = [];
     if ($params->type !== 'data') {
         if ($params->input['kind'] === 'db') {
             $dbSchema = new DBSchema($this->manager);
             $schemaDiff = $dbSchema->getDiff();
         } else {
             $tableSchema = new TableSchema($this->manager);
             $schemaDiff = $tableSchema->getDiff($params->input['source']['table']);
         }
     }
     // Data diff
     $dataDiff = [];
     if ($params->type !== 'schema') {
         if ($params->input['kind'] === 'db') {
             $dbData = new DBData($this->manager);
             $dataDiff = $dbData->getDiff();
         } else {
             $tableData = new TableData($this->manager);
             $dataDiff = $tableData->getDiff($params->input['source']['table']);
         }
     }
     return ['schema' => $schemaDiff, 'data' => $dataDiff];
 }
All Usage Examples Of DBDiff\DB\Schema\TableSchema::getDiff