break;
case 'alter':
- $queries .= "CHANGE COLUMN `{$columnName}` {$this->buildColumnDefinition($data['oldColumnName'], $data['data'])},";
+ $newColumnName = $columnName;
+ if (isset($data['oldColumnName'])) {
+ /**
+ * @deprecated 5.4 `oldColumnName` was an incorrect name for the index
+ * that is kept for backwards compatibility for now
+ */
+ $newColumnName = $data['oldColumnName'];
+ }
+ else if (isset($data['newColumnName'])) {
+ $newColumnName = $data['newColumnName'];
+ }
+
+ $queries .= "CHANGE COLUMN `{$columnName}` {$this->buildColumnDefinition($newColumnName, $data['data'])},";
break;
case 'drop':
$columnData[$alteredColumn->getName()] = [
'action' => 'alter',
'data' => $alteredColumn->getData(),
- 'oldColumnName' => $alteredColumn->getName()
+ 'newColumnName' => $alteredColumn->getNewName() ?? $alteredColumn->getName()
];
}
}
}
+ if ($newColumn->getNewName()) {
+ return true;
+ }
+
// default type has to be checked explicitly for `null` to properly detect changing
// from no default value (`null`) and to an empty string as default value (and vice
// versa)
*/
protected $name;
+ /**
+ * new name of the database table column
+ * @var ?string
+ */
+ protected $newName;
+
/**
* is `true` if the values of the column may not be `null`
* @var bool
return $this->defaultValue;
}
+ /**
+ * @inheritDoc
+ * @since 5.4
+ */
+ public function getNewName(): ?string {
+ return $this->newName;
+ }
+
/**
* @inheritDoc
*/
return $this;
}
+ /**
+ * @inheritDoc
+ * @since 5.4
+ */
+ public function renameTo(string $newName): self {
+ if ($newName === $this->getName()) {
+ throw new \InvalidArgumentException("'{$newName}' is the current name of the column.");
+ }
+
+ $this->newName = $newName;
+
+ return $this;
+ }
+
/**
* Checks if the given default value is valid.
*
*/
public function getName();
+ /**
+ * Returns the new name of the column or `null` if the column's name is unchanged.
+ *
+ * @since 5.4
+ */
+ public function getNewName(): ?string;
+
/**
* Returns the type of the column.
*
*/
public function notNull($notNull = true);
+ /**
+ * Sets the new name of the column and returns the column.
+ *
+ * @since 5.4
+ */
+ public function renameTo(string $newName): self;
+
/**
* Returns `true` if the column will be dropped.
*