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

getDiff() public method

public getDiff ( )
    function getDiff()
    {
        $params = ParamsFactory::get();
        $diffs = [];
        // Collation
        $dbName = $this->manager->getDB('target')->getDatabaseName();
        $sourceCollation = $this->getDBVariable('source', 'collation_database');
        $targetCollation = $this->getDBVariable('target', 'collation_database');
        if ($sourceCollation !== $targetCollation) {
            $diffs[] = new SetDBCollation($dbName, $sourceCollation, $targetCollation);
        }
        // Charset
        $sourceCharset = $this->getDBVariable('source', 'character_set_database');
        $targetCharset = $this->getDBVariable('target', 'character_set_database');
        if ($sourceCharset !== $targetCharset) {
            $diffs[] = new SetDBCharset($dbName, $sourceCharset, $targetCharset);
        }
        // Tables
        $tableSchema = new TableSchema($this->manager);
        $sourceTables = $this->manager->getTables('source');
        $targetTables = $this->manager->getTables('target');
        if (isset($params->tablesToIgnore)) {
            $sourceTables = array_diff($sourceTables, $params->tablesToIgnore);
            $targetTables = array_diff($targetTables, $params->tablesToIgnore);
        }
        $addedTables = array_diff($sourceTables, $targetTables);
        foreach ($addedTables as $table) {
            $diffs[] = new AddTable($table, $this->manager->getDB('source'));
        }
        $commonTables = array_intersect($sourceTables, $targetTables);
        foreach ($commonTables as $table) {
            $tableDiff = $tableSchema->getDiff($table);
            $diffs = array_merge($diffs, $tableDiff);
        }
        $deletedTables = array_diff($targetTables, $sourceTables);
        foreach ($deletedTables as $table) {
            $diffs[] = new DropTable($table, $this->manager->getDB('target'));
        }
        return $diffs;
    }

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];
 }