Skip to content

Commit

Permalink
Merge pull request #4354 from vanilla/fix/renameColumn
Browse files Browse the repository at this point in the history
Fix renameColumn() so it works for the first time ever
  • Loading branch information
DaazKu committed Aug 25, 2016
2 parents 04e8c35 + df12fe9 commit 7e0e1a8
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions library/database/class.mysqlstructure.php
Expand Up @@ -153,7 +153,7 @@ public function renameColumn($OldName, $NewName, $TableName = '') {
// Rename the column
// The syntax for renaming a column is:
// ALTER TABLE tablename CHANGE COLUMN oldname newname originaldefinition;
if (!$this->executeQuery('alter table `'.$OldPrefix.$this->_TableName.'` change column `'.$OldName.'` `'.$NewName.'` '.$this->_defineColumn($OldColumn))) {
if (!$this->executeQuery('alter table `'.$OldPrefix.$this->_TableName.'` change column '.$this->_defineColumn($OldColumn, $NewName))) {
throw new Exception(sprintf(t('Failed to rename table `%1$s` to `%2$s`.'), $OldName, $NewName));
}

Expand Down Expand Up @@ -672,8 +672,9 @@ protected function _modify($Explicit = false) {
*
*
* @param stdClass $column
* @param string $newColumnName For rename action only.
*/
protected function _defineColumn($column) {
protected function _defineColumn($column, $newColumnName = null) {
$column = clone $column;

$typeAliases = [
Expand Down Expand Up @@ -719,7 +720,15 @@ protected function _defineColumn($column) {
throw new Exception(sprintf(t('The specified data type (%1$s) is not accepted for the MySQL database.'), $column->Type));
}

$Return = "`{$column->Name}` {$column->Type}";
$Return = "`{$column->Name}` ";

// The CHANGE COLUMN syntax requires this ordering.
// @see $this->renameColumn().
if (is_string($newColumnName)) {
$Return .= "`{$newColumnName}` ";
}

$Return .= "{$column->Type}";

$LengthTypes = $this->types('length');
if ($column->Length != '' && in_array($column->Type, $LengthTypes)) {
Expand Down

0 comments on commit 7e0e1a8

Please sign in to comment.