Example #1
0
 public function render()
 {
     $diffGenerator = new DiffGenerator();
     $diffGenerator->source($this->source())->target($this->target());
     $raport = $diffGenerator->diff();
     foreach ($raport['tables'] as $table => $diff) {
         if ($diff['status'] == DiffGenerator::NO_CHANGE) {
             continue;
         }
         $this->append("");
         switch ($diff['status']) {
             case DiffGenerator::DROPED:
                 $this->printDroped($table);
                 break;
             case DiffGenerator::CREATED:
                 $this->printCreated($table);
                 break;
             case DiffGenerator::CHANGED:
                 $this->printChangedTable($table, $diff);
                 break;
         }
     }
     if (!empty($raport['views'])) {
         foreach ($raport['views'] as $view => $diff) {
             if ($diff['status'] == DiffGenerator::NO_CHANGE) {
                 continue;
             }
             $this->append("");
             switch ($diff['status']) {
                 case DiffGenerator::DROPED:
                     $this->printDroped($view);
                     break;
                 case DiffGenerator::CREATED:
                     $this->printCreated($view);
                     break;
                 case DiffGenerator::CHANGED:
                     $this->printChangedView($view, $diff);
                     break;
             }
         }
     }
     parent::render();
 }
Example #2
0
 /**
  * Checks the differences between source and target, and generate proper
  * sql to sync.
  *
  * @return string
  */
 public function render()
 {
     $assert = $this->service('assert');
     $diffGenerator = new DiffGenerator();
     $diffGenerator->source($this->source())->target($this->target());
     $diffRaport = $diffGenerator->diff();
     // na postawie raportu tworze odpowiednie zapytania sql
     $targetAdapter = $this->adapter('target');
     $this->buffor = new Buffor();
     $this->removeForeignKey($diffRaport['tables']);
     foreach ($diffRaport['tables'] as $table => $diff) {
         if ($diff['status'] == DiffGenerator::NO_CHANGE) {
             // nic sie nie zmienilo w tabeli
             continue;
         }
         // mamy zmiane w tabeli
         $targetTable = $targetAdapter->table($table);
         $status = $diff['status'];
         $tableBuilder = $this->ddlBuilder(MidataDDL::TABLE);
         switch ($status) {
             case DiffGenerator::CREATED:
                 //$this->buffor->append(1, $tableBuilder->create($targetTable));
                 $this->buffor->append("create-table", $this->createTable($targetTable));
                 break;
             case DiffGenerator::DROPED:
                 $this->buffor->append("drop-table", $tableBuilder->drop($targetTable));
                 break;
             case DiffGenerator::CHANGED:
                 $this->processDiff($diff['columns']['diff'], 'column', $targetTable);
                 $this->processDiff($diff['triggers']['diff'], 'trigger', $targetTable);
                 $this->processDiff($diff['indexes']['diff'], 'index', $targetTable);
                 // dla kazdej tabeli gdzie pojawila sie zmiana usuniete zostaly
                 // klucze obce powiazane z ta tabela, dlatego przetwarzanie
                 // constraintow jest troche inne niz pozostalych obiektow
                 $this->processConstraintDiff($diff['constraints']['diff'], $targetTable);
                 break;
             default:
                 $assert->exception("Some not supported status '{$status}'");
                 break;
         }
     }
     foreach ($diffRaport['views'] as $view => $diff) {
         if ($diff['status'] == DiffGenerator::NO_CHANGE) {
             continue;
         }
         $targetView = $targetAdapter->view($view);
         $status = $diff['status'];
         $viewBuilder = $this->ddlBuilder(MidataDDL::VIEW);
         switch ($status) {
             case DiffGenerator::CREATED:
                 $this->buffor->append('create-view', $viewBuilder->create($targetView));
                 break;
             case DiffGenerator::DROPED:
                 $this->buffor->append("drop-view", $viewBuilder->drop($targetView));
                 break;
             case DiffGenerator::CHANGED:
                 $this->buffor->append("alter-view", $viewBuilder->alter($targetView));
                 break;
             default:
                 $assert->exception("Some not supported status '{$status}'");
                 break;
         }
     }
     $this->append($this->buffor->get());
     parent::render();
 }