Skip to content

Commit bcf6f82

Browse files
committed
Fix table options and alter options serialisation
1 parent adc4e87 commit bcf6f82

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

sources/Sql/Ddl/Table/AlterTableCommand.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public function __construct(
6161
AlterTableOption::checkValue($option);
6262
}
6363
}
64+
if ($tableOptions === []) {
65+
$tableOptions = null;
66+
}
6467

6568
$this->name = $name;
6669
$this->actions = is_array($actions) ? new AlterActionsList($actions) : $actions;
@@ -131,15 +134,16 @@ public function serialize(Formatter $formatter): string
131134

132135
$result .= $this->actions->serialize($formatter);
133136

134-
if ($this->tableOptions !== null && !$this->actions->isEmpty()) {
135-
$result .= ',';
136-
}
137-
138-
if ($this->tableOptions !== null && !$this->tableOptions->isEmpty()) {
137+
if ($this->tableOptions !== null) {
138+
if (!$this->actions->isEmpty()) {
139+
$result .= ',';
140+
}
139141
$result .= "\n" . $formatter->indent . $this->tableOptions->serialize($formatter, ",\n", ' ');
140142
}
141143

142-
$result = rtrim($result, ',');
144+
if (($this->tableOptions !== null || !$this->actions->isEmpty()) && $this->alterOptions !== []) {
145+
$result .= ',';
146+
}
143147

144148
foreach ($this->alterOptions as $option => $value) {
145149
if ($option === AlterTableOption::ONLINE) {
@@ -154,6 +158,8 @@ public function serialize(Formatter $formatter): string
154158
}
155159
}
156160

161+
$result = rtrim($result, ',');
162+
157163
if ($this->partitioning !== null) {
158164
$result .= "\n" . $this->partitioning->serialize($formatter);
159165
}

tests/Parser/Ddl/TableCommandsParser.other.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ use SqlFtw\Tests\Assert;
77
require __DIR__ . '/../../bootstrap.php';
88

99

10+
// combination of actions, table options and alter options
11+
Assert::parseSerialize("ALTER TABLE tbl1 ADD COLUMN col1 INT");
12+
Assert::parseSerialize("ALTER TABLE tbl1 ADD COLUMN col1 INT, ALGORITHM INSTANT");
13+
Assert::parseSerialize("ALTER TABLE tbl1 ADD COLUMN col1 INT, ENGINE InnoDB");
14+
Assert::parseSerialize("ALTER TABLE tbl1 ADD COLUMN col1 INT, ENGINE InnoDB, ALGORITHM INSTANT");
15+
Assert::parseSerialize("ALTER TABLE tbl1 ENGINE InnoDB");
16+
Assert::parseSerialize("ALTER TABLE tbl1 ENGINE InnoDB, ALGORITHM INSTANT");
17+
Assert::parseSerialize("ALTER TABLE tbl1 ALGORITHM INSTANT");
18+
19+
1020
// DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ... [RESTRICT | CASCADE]
1121
Assert::parseSerialize("DROP TABLE tbl1");
1222
Assert::parseSerialize("DROP TEMPORARY TABLE tbl1");

0 commit comments

Comments
 (0)