private function inlinePrimaryKey(MidataConstraint $primaryKey) { $assert = $this->service('assert'); $columns = $primaryKey->columns(); if (empty($columns)) { $assert->exception("Can't create primary key for constraints without columns"); } $columns = $this->implode($columns); $sql = "PRIMARY KEY ({$columns})"; return $sql; }
private function diffConstraint($table) { $assert = $this->service('assert'); $sourceTable = $this->adapter('source')->table($table); $targetTable = $this->adapter('target')->table($table); $sourceConstraints = $sourceTable->constraints(); $targetConstraints = $targetTable->constraints(); $baseDiff = $this->baseDiff($sourceConstraints, $targetConstraints); $diff = $baseDiff['diff']; $status = $baseDiff['status']; foreach ($diff as $constraint => &$diffConstraint) { if ($diffConstraint['status'] != self::NO_CHANGE) { // constraint zostal usuniete lub stworzony wiec pomijam continue; } // po obu stronach mamy constraint z taka sama nazwa wiec sprawdzam // jakie sa roznice pomiedzy nimi $sourceConstraint = $sourceTable->constraint($constraint); $targetConstraint = $targetTable->constraint($constraint); if ($sourceConstraint->isPrimaryKey() && $targetConstraint->isForeignKey() || $sourceConstraint->isForeignKey() && $targetConstraint->isPrimaryKey()) { // constrainty o takich samych nazwach sa innego typu, moze sie // pojawic jak porownujemy rozne typy bazdanych $name = $sourceConstraint->name(); $assert->exception("Incompatible type of constraint with same name {$name}"); } if ($sourceConstraint->isPrimaryKey()) { $sourceAttribute = $sourceConstraint->columns(); $targetAttribute = $targetConstraint->columns(); $changed = false; if (md5(var_export($sourceAttribute, true)) !== md5(var_export($targetAttribute, true))) { $changed = true; } if ($changed) { $diffConstraint['status'] = $status = self::CHANGED; // pojawila sie zmiana dodaje rowniez informacje o type // constrainta $diffConstraint['constraintType'] = 'primaryKey'; $diffConstraint['diff'] = array('source' => $sourceAttribute, 'target' => $targetAttribute); } } elseif ($sourceConstraint->isForeignKey()) { foreach (Constraint::allAttributes() as $attribute) { $sourceAttribute = $sourceConstraint->{$attribute}(); $targetAttribute = $targetConstraint->{$attribute}(); if ($sourceAttribute == Constraint::NOT_SUPPORTED || $targetAttribute == Constraint::NOT_SUPPORTED) { continue; } $changed = false; if ($attribute == "columns") { if (md5(var_export($sourceAttribute, true)) !== md5(var_export($targetAttribute, true))) { $changed = true; } } else { if ($sourceAttribute !== $targetAttribute) { $changed = true; } } if ($changed) { $diffConstraint['status'] = $status = self::CHANGED; $diffConstraint['constraintType'] = 'foreignKey'; $diffConstraint['diff'][$attribute] = array('source' => $sourceAttribute, 'target' => $targetAttribute); } } } else { $assert->exception("Unsupported type of constraint"); } } return array('status' => $status, 'diff' => $diff); }