Skip to content

Commit 3417fea

Browse files
authored
Merge pull request #1997 from vol4onok/bugfix-broken-alter-table-hevengo-corp
Added test case
2 parents 2da1345 + d7b87fd commit 3417fea

File tree

7 files changed

+36
-6
lines changed

7 files changed

+36
-6
lines changed

src/Propel/Generator/Platform/Util/AlterTableStatementMerger.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Propel\Generator\Platform\Util;
1010

1111
use Propel\Generator\Model\Table;
12+
use Propel\Generator\Util\SqlParser;
1213

1314
/**
1415
* Merges several ALTER TABLE statements when creating migrations.
@@ -66,7 +67,10 @@ public function __construct(Table $table)
6667
*/
6768
public function mergeStatements(string $sql): string
6869
{
69-
$statements = explode(';', $sql);
70+
$sqlParser = new SqlParser();
71+
$sqlParser->setSQL($sql);
72+
$statements = $sqlParser->explodeIntoStatements();
73+
7074
$blocks = [];
7175
$currentBlock = [];
7276

src/Propel/Generator/Util/SqlParser.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,37 @@ public function explodeIntoStatements(): array
232232
public function getNextStatement(): string
233233
{
234234
$isAfterBackslash = false;
235+
$isCommentLine = false;
235236
$isInString = false;
236237
$stringQuotes = '';
237238
$parsedString = '';
238239
$lowercaseString = ''; // helper variable for performance sake
239240
while ($this->pos <= $this->len) {
240241
$char = $this->sql[$this->pos] ?? '';
242+
243+
$newLine = !isset($this->sql[$this->pos - 1]) || $this->sql[$this->pos - 1] === PHP_EOL;
244+
245+
// Skip comments
246+
if ($isCommentLine === true) {
247+
$this->pos++;
248+
if ($char === PHP_EOL) {
249+
$isCommentLine = false; // end of comments line
250+
}
251+
252+
continue;
253+
}
241254
// check flags for strings or escaper
242255
switch ($char) {
256+
case '#':
257+
// detect comment line
258+
if ($newLine === true && $isCommentLine === false) {
259+
$this->pos++;
260+
$isCommentLine = true;
261+
262+
continue 2;
263+
}
264+
265+
break;
243266
case '\\':
244267
$isAfterBackslash = true;
245268

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

299-
return trim($parsedString);
323+
return $parsedTrimmedString ?: $parsedString; // to check empty line to avoid stop parsing
300324
}
301325
// avoid using strtolower on the whole parsed string every time new character is added
302326
// there is also no point in adding characters which are in the string

tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function testGetModifyTableDDL($tableDiff)
131131
132132
CHANGE `bar` `bar1` INTEGER,
133133
134-
CHANGE `baz` `baz` VARCHAR(12),
134+
CHANGE `baz` `baz` VARCHAR(12) DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt',
135135
136136
ADD `baz3` TEXT AFTER `baz`;
137137

tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function testGetModifyTableDDL($tableDiff)
139139
140140
CHANGE `bar` `bar1` INTEGER,
141141
142-
CHANGE `baz` `baz` VARCHAR(12),
142+
CHANGE `baz` `baz` VARCHAR(12) DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt',
143143
144144
ADD `baz3` TEXT AFTER `baz`;
145145

tests/Propel/Tests/Generator/Platform/OraclePlatformMigrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function testGetModifyTableDDL($tableDiff)
105105
106106
MODIFY
107107
(
108-
baz NVARCHAR2(12)
108+
baz NVARCHAR2(12) DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt'
109109
),
110110
111111
ADD

tests/Propel/Tests/Generator/Platform/PgsqlPlatformMigrationTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public function testGetModifyTableDDL($tableDiff)
9999
100100
ALTER TABLE "foo"
101101
102+
ALTER COLUMN "baz" SET DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt',
103+
102104
ALTER COLUMN "baz" DROP NOT NULL,
103105
104106
ADD "baz3" TEXT;

tests/Propel/Tests/Generator/Platform/PlatformMigrationTestProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function providerForTestGetModifyTableDDL()
107107
<table name="foo">
108108
<column name="id" primaryKey="true" type="INTEGER" autoIncrement="true"/>
109109
<column name="bar1" type="INTEGER"/>
110-
<column name="baz" type="VARCHAR" size="12" required="false"/>
110+
<column name="baz" type="VARCHAR" size="12" required="false" defaultValue="pdf;jpg;png;doc;docx;xls;xlsx;txt"/>
111111
<column name="baz3" type="LONGVARCHAR"/>
112112
<foreign-key name="foo1_fk_1" foreignTable="foo2">
113113
<reference local="bar1" foreign="bar"/>

0 commit comments

Comments
 (0)