Skip to content

Commit

Permalink
Migration: Missing "AFTER"/"FIRST" when adding columns (MySQL)
Browse files Browse the repository at this point in the history
- Copied code from Propel 1
- Changed methods, so the statements can get merged
  • Loading branch information
IceShack committed Mar 1, 2015
1 parent 56cecb5 commit 24538e6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 36 deletions.
51 changes: 51 additions & 0 deletions src/Propel/Generator/Platform/MysqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,57 @@ public function getModifyColumnsDDL($columnDiffs)
return $ret;
}

/**
* Builds the DDL SQL to add a a column
*
* @param Column $column
*
* @return string
*/
public function getAddColumnDDL(Column $column)
{
$pattern = "
ALTER TABLE %s ADD %s %s;
";
$tableColumns = $column->getTable()->getColumns();

//default to add to top if the before-column cannot be found
$insertPositionDDL = "FIRST";
foreach ($tableColumns as $i => $tableColumn) {
//we found the column, use the column before it, if its not the first
if ($tableColumn->getName() == $column->getName()) {
//we have a column that is not the first column
if ($i > 0) {
$insertPositionDDL = "AFTER " . $this->quoteIdentifier($tableColumns[$i - 1]->getName());
}
break;
}
}

return sprintf($pattern,
$this->quoteIdentifier($column->getTable()->getName()),
$this->getColumnDDL($column),
$insertPositionDDL
);
}

/**
* Builds the DDL SQL to add a list of columns
*
* @param Column[] $columns
*
* @return string
*/
public function getAddColumnsDDL($columns)
{
$lines = '';
foreach ($columns as $column) {
$lines .= $this->getAddColumnDDL($column);
}

return $lines;
}

/**
* @see Platform::supportsSchemas()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ public function testRenameTableDDL($databaseDiff)
CHANGE `baz` `baz` VARCHAR(12),
ADD
(
`baz3` TEXT
);
ADD `baz3` TEXT AFTER `baz`;
CREATE TABLE `foo5`
(
Expand Down Expand Up @@ -133,10 +130,7 @@ public function testGetModifyTableDDL($tableDiff)
CHANGE `baz` `baz` VARCHAR(12),
ADD
(
`baz3` TEXT
);
ADD `baz3` TEXT AFTER `baz`;
CREATE INDEX `bar_fk` ON `foo` (`bar1`);
Expand All @@ -155,10 +149,7 @@ public function testGetModifyTableColumnsDDL($tableDiff)
ALTER TABLE `foo` CHANGE `baz` `baz` VARCHAR(12);
ALTER TABLE `foo` ADD
(
`baz3` TEXT
);
ALTER TABLE `foo` ADD `baz3` TEXT AFTER `baz`;
";
$this->assertEquals($expected, $this->getPlatform()->getModifyTableColumnsDDL($tableDiff));
}
Expand Down Expand Up @@ -276,7 +267,7 @@ public function testGetModifyColumnsDDL($columnDiffs)
public function testGetAddColumnDDL($column)
{
$expected = "
ALTER TABLE `foo` ADD `bar` INTEGER;
ALTER TABLE `foo` ADD `bar` INTEGER AFTER `id`;
";
$this->assertEquals($expected, $this->getPlatform()->getAddColumnDDL($column));
}
Expand All @@ -287,11 +278,9 @@ public function testGetAddColumnDDL($column)
public function testGetAddColumnsDDL($columns)
{
$expected = "
ALTER TABLE `foo` ADD
(
`bar1` INTEGER,
`bar2` DOUBLE(3,2) DEFAULT -1 NOT NULL
);
ALTER TABLE `foo` ADD `bar1` INTEGER AFTER `id`;
ALTER TABLE `foo` ADD `bar2` DOUBLE(3,2) DEFAULT -1 NOT NULL AFTER `bar1`;
";
$this->assertEquals($expected, $this->getPlatform()->getAddColumnsDDL($columns));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ public function testRenameTableDDL($databaseDiff)
CHANGE `baz` `baz` VARCHAR(12),
ADD
(
`baz3` TEXT
);
ADD `baz3` TEXT AFTER `baz`;
CREATE TABLE `foo5`
(
Expand Down Expand Up @@ -138,10 +135,7 @@ public function testGetModifyTableDDL($tableDiff)
CHANGE `baz` `baz` VARCHAR(12),
ADD
(
`baz3` TEXT
);
ADD `baz3` TEXT AFTER `baz`;
CREATE INDEX `bar_fk` ON `foo` (`bar1`);
Expand All @@ -164,10 +158,7 @@ public function testGetModifyTableColumnsDDL($tableDiff)
ALTER TABLE `foo` CHANGE `baz` `baz` VARCHAR(12);
ALTER TABLE `foo` ADD
(
`baz3` TEXT
);
ALTER TABLE `foo` ADD `baz3` TEXT AFTER `baz`;
";
$this->assertEquals($expected, $this->getPlatform()->getModifyTableColumnsDDL($tableDiff));
}
Expand Down Expand Up @@ -303,7 +294,7 @@ public function testGetModifyColumnsDDL($columnDiffs)
public function testGetAddColumnDDL($column)
{
$expected = "
ALTER TABLE `foo` ADD `bar` INTEGER;
ALTER TABLE `foo` ADD `bar` INTEGER AFTER `id`;
";
$this->assertEquals($expected, $this->getPlatform()->getAddColumnDDL($column));
}
Expand All @@ -314,11 +305,9 @@ public function testGetAddColumnDDL($column)
public function testGetAddColumnsDDL($columns)
{
$expected = "
ALTER TABLE `foo` ADD
(
`bar1` INTEGER,
`bar2` DOUBLE(3,2) DEFAULT -1 NOT NULL
);
ALTER TABLE `foo` ADD `bar1` INTEGER AFTER `id`;
ALTER TABLE `foo` ADD `bar2` DOUBLE(3,2) DEFAULT -1 NOT NULL AFTER `bar1`;
";
$this->assertEquals($expected, $this->getPlatform()->getAddColumnsDDL($columns));
}
Expand Down

0 comments on commit 24538e6

Please sign in to comment.