Exemplo n.º 1
0
 public function saveColumn(Column $column)
 {
     if ($column->isSynced()) {
         return $this;
     }
     $syncedCol = $column->getTable()->getColumn($column->getName())->load();
     if ($column->equals($syncedCol, false)) {
         return $this;
     }
     $sql = $this->getColumnSql($column);
     $name = $this->quoteName($column->getDatabase(), $column->getTable());
     if ($syncedCol->isUnique() && !$column->isUnique()) {
         $this->dropUniqueIndex($syncedCol);
     }
     $this->query("ALTER TABLE {$name} MODIFY {$sql}");
     /* Drop/Add UNIQUE if needed */
     if (!$syncedCol->isUnique() && $column->isUnique()) {
         $this->addUniqueIndex($column);
     }
     /* Drop PRIMARY if needed (It gets added through getColumnSql() and the PRIMARY KEY addon) */
     if ($syncedCol->isPrimary() && !$column->isPrimary()) {
         $this->query("ALTER TABLE {$name} DROP PRIMARY KEY");
     }
     /* Add INDEX if needed */
     if (!$syncedCol->isIndex() && $column->isIndex()) {
         $this->addIndex($column);
     }
     /* Drop/Add CONSTRAINTS if needed */
     $syncedRef = $syncedCol->getReference();
     $colRef = $column->getReference();
     $refChanged = $colRef && $syncedRef && !$colRef->equals($syncedRef);
     if ($syncedRef && !$colRef || $colRef && !$syncedRef || $refChanged) {
         if ($syncedRef && !$colRef || $refChanged) {
             $this->dropConstraint($syncedCol);
         }
         if ($colRef && !$syncedRef || $refChanged) {
             $this->addConstraint($column);
         }
     }
     /* Drop INDEX if needed */
     if ($syncedCol->isIndex() && !$column->isIndex()) {
         $this->dropIndex($column);
     }
     return $this;
 }