/**
  * Builds the DDL SQL to modify a column
  *
  * @return string
  */
 public function getModifyColumnDDL(ColumnDiff $columnDiff)
 {
     return $this->getChangeColumnDDL($columnDiff->getFromColumn(), $columnDiff->getToColumn());
 }
Exemple #2
0
 /**
  * Overrides the implementation from DefaultPlatform
  *
  * @author     Niklas Närhinen <*****@*****.**>
  * @return string
  * @see DefaultPlatform::getModifyColumnDDL
  */
 public function getModifyColumnDDL(ColumnDiff $columnDiff)
 {
     $ret = '';
     $changedProperties = $columnDiff->getChangedProperties();
     $fromColumn = $columnDiff->getFromColumn();
     $toColumn = clone $columnDiff->getToColumn();
     $fromTable = $fromColumn->getTable();
     $table = $toColumn->getTable();
     $colName = $this->quoteIdentifier($toColumn->getName());
     $pattern = "\nALTER TABLE %s ALTER COLUMN %s;\n";
     if (isset($changedProperties['autoIncrement'])) {
         $tableName = $table->getName();
         $colPlainName = $toColumn->getName();
         $seqName = "{$tableName}_{$colPlainName}_seq";
         if ($toColumn->isAutoIncrement() && $table && $table->getIdMethodParameters() == null) {
             $defaultValue = "nextval('{$seqName}'::regclass)";
             $toColumn->setDefaultValue($defaultValue);
             $changedProperties['defaultValueValue'] = [null, $defaultValue];
             //add sequence
             if (!$fromTable->getDatabase()->hasSequence($seqName)) {
                 $this->createOrDropSequences .= sprintf("\nCREATE SEQUENCE %s;\n", $seqName);
                 $fromTable->getDatabase()->addSequence($seqName);
             }
         }
         if (!$toColumn->isAutoIncrement() && $fromColumn->isAutoIncrement()) {
             $changedProperties['defaultValueValue'] = [$fromColumn->getDefaultValueString(), null];
             $toColumn->setDefaultValue(null);
             //remove sequence
             if ($fromTable->getDatabase()->hasSequence($seqName)) {
                 $this->createOrDropSequences .= sprintf("\nDROP SEQUENCE %s CASCADE;\n", $seqName);
                 $fromTable->getDatabase()->removeSequence($seqName);
             }
         }
     }
     if (isset($changedProperties['size']) || isset($changedProperties['type']) || isset($changedProperties['scale'])) {
         $sqlType = $toColumn->getDomain()->getSqlType();
         if ($this->hasSize($sqlType) && $toColumn->isDefaultSqlType($this)) {
             if ($this->isNumber($sqlType)) {
                 if ('NUMERIC' === strtoupper($sqlType)) {
                     $sqlType .= $toColumn->getSizeDefinition();
                 }
             } else {
                 $sqlType .= $toColumn->getSizeDefinition();
             }
         }
         if ($using = $this->getUsingCast($fromColumn, $toColumn)) {
             $sqlType .= $using;
         }
         $ret .= sprintf($pattern, $this->quoteIdentifier($table->getName()), $colName . ' TYPE ' . $sqlType);
     }
     if (isset($changedProperties['defaultValueValue'])) {
         $property = $changedProperties['defaultValueValue'];
         if ($property[0] !== null && $property[1] === null) {
             $ret .= sprintf($pattern, $this->quoteIdentifier($table->getName()), $colName . ' DROP DEFAULT');
         } else {
             $ret .= sprintf($pattern, $this->quoteIdentifier($table->getName()), $colName . ' SET ' . $this->getColumnDefaultValueDDL($toColumn));
         }
     }
     if (isset($changedProperties['notNull'])) {
         $property = $changedProperties['notNull'];
         $notNull = ' DROP NOT NULL';
         if ($property[1]) {
             $notNull = ' SET NOT NULL';
         }
         $ret .= sprintf($pattern, $this->quoteIdentifier($table->getName()), $colName . $notNull);
     }
     return $ret;
 }