/** * Overrides the implementation from DefaultPlatform * * @author Niklas Närhinen <*****@*****.**> * @return string * @see DefaultPlatform::getModifyColumnDDL */ public function getModifyColumnDDL(ColumnDiff $columnDiff) { $ret = ''; $changedProperties = $columnDiff->getChangedProperties(); $toColumn = $columnDiff->getToColumn(); $table = $toColumn->getTable(); $colName = $this->quoteIdentifier($toColumn->getName()); $pattern = "\nALTER TABLE %s ALTER COLUMN %s;\n"; foreach ($changedProperties as $key => $property) { switch ($key) { case 'defaultValueType': continue; case 'size': case 'type': case 'scale': $sqlType = $toColumn->getDomain()->getSqlType(); if ($toColumn->isAutoIncrement() && $table && $table->getIdMethodParameters() == null) { $sqlType = $toColumn->getType() === PropelTypes::BIGINT ? 'bigserial' : 'serial'; } if ($this->hasSize($sqlType)) { $sqlType .= $toColumn->getDomain()->printSize(); } $ret .= sprintf($pattern, $this->quoteIdentifier($table->getName()), $colName . ' TYPE ' . $sqlType); break; case '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)); } break; case 'notNull': $notNull = " DROP NOT NULL"; if ($property[1]) { $notNull = " SET NOT NULL"; } $ret .= sprintf($pattern, $this->quoteIdentifier($table->getName()), $colName . $notNull); break; } } return $ret; }
/** * 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; }