Skip to content

Commit

Permalink
Merge pull request #1997 from vol4onok/bugfix-broken-alter-table-heve…
Browse files Browse the repository at this point in the history
…ngo-corp

Added test case
  • Loading branch information
gechetspr authored Apr 1, 2024
2 parents 2da1345 + d7b87fd commit 3417fea
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Propel\Generator\Platform\Util;

use Propel\Generator\Model\Table;
use Propel\Generator\Util\SqlParser;

/**
* Merges several ALTER TABLE statements when creating migrations.
Expand Down Expand Up @@ -66,7 +67,10 @@ public function __construct(Table $table)
*/
public function mergeStatements(string $sql): string
{
$statements = explode(';', $sql);
$sqlParser = new SqlParser();
$sqlParser->setSQL($sql);
$statements = $sqlParser->explodeIntoStatements();

$blocks = [];
$currentBlock = [];

Expand Down
26 changes: 25 additions & 1 deletion src/Propel/Generator/Util/SqlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,37 @@ public function explodeIntoStatements(): array
public function getNextStatement(): string
{
$isAfterBackslash = false;
$isCommentLine = false;
$isInString = false;
$stringQuotes = '';
$parsedString = '';
$lowercaseString = ''; // helper variable for performance sake
while ($this->pos <= $this->len) {
$char = $this->sql[$this->pos] ?? '';

$newLine = !isset($this->sql[$this->pos - 1]) || $this->sql[$this->pos - 1] === PHP_EOL;

// Skip comments
if ($isCommentLine === true) {
$this->pos++;
if ($char === PHP_EOL) {
$isCommentLine = false; // end of comments line
}

continue;
}
// check flags for strings or escaper
switch ($char) {
case '#':
// detect comment line
if ($newLine === true && $isCommentLine === false) {
$this->pos++;
$isCommentLine = true;

continue 2;
}

break;
case '\\':
$isAfterBackslash = true;

Expand Down Expand Up @@ -295,8 +318,9 @@ public function getNextStatement(): string
// check for end of statement
if ($char . $nextChars == $this->delimiter) {
$this->pos += $i; // increase position
$parsedTrimmedString = trim($parsedString);

return trim($parsedString);
return $parsedTrimmedString ?: $parsedString; // to check empty line to avoid stop parsing
}
// avoid using strtolower on the whole parsed string every time new character is added
// there is also no point in adding characters which are in the string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function testGetModifyTableDDL($tableDiff)
CHANGE `bar` `bar1` INTEGER,
CHANGE `baz` `baz` VARCHAR(12),
CHANGE `baz` `baz` VARCHAR(12) DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt',
ADD `baz3` TEXT AFTER `baz`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function testGetModifyTableDDL($tableDiff)
CHANGE `bar` `bar1` INTEGER,
CHANGE `baz` `baz` VARCHAR(12),
CHANGE `baz` `baz` VARCHAR(12) DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt',
ADD `baz3` TEXT AFTER `baz`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function testGetModifyTableDDL($tableDiff)
MODIFY
(
baz NVARCHAR2(12)
baz NVARCHAR2(12) DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt'
),
ADD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public function testGetModifyTableDDL($tableDiff)
ALTER TABLE "foo"
ALTER COLUMN "baz" SET DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt',
ALTER COLUMN "baz" DROP NOT NULL,
ADD "baz3" TEXT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function providerForTestGetModifyTableDDL()
<table name="foo">
<column name="id" primaryKey="true" type="INTEGER" autoIncrement="true"/>
<column name="bar1" type="INTEGER"/>
<column name="baz" type="VARCHAR" size="12" required="false"/>
<column name="baz" type="VARCHAR" size="12" required="false" defaultValue="pdf;jpg;png;doc;docx;xls;xlsx;txt"/>
<column name="baz3" type="LONGVARCHAR"/>
<foreign-key name="foo1_fk_1" foreignTable="foo2">
<reference local="bar1" foreign="bar"/>
Expand Down

0 comments on commit 3417fea

Please sign in to comment.