Skip to content

Commit

Permalink
Fix table options and alter options serialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
paranoiq committed Feb 20, 2023
1 parent adc4e87 commit bcf6f82
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
18 changes: 12 additions & 6 deletions sources/Sql/Ddl/Table/AlterTableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public function __construct(
AlterTableOption::checkValue($option);
}
}
if ($tableOptions === []) {
$tableOptions = null;
}

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

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

if ($this->tableOptions !== null && !$this->actions->isEmpty()) {
$result .= ',';
}

if ($this->tableOptions !== null && !$this->tableOptions->isEmpty()) {
if ($this->tableOptions !== null) {
if (!$this->actions->isEmpty()) {
$result .= ',';
}
$result .= "\n" . $formatter->indent . $this->tableOptions->serialize($formatter, ",\n", ' ');
}

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

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

$result = rtrim($result, ',');

if ($this->partitioning !== null) {
$result .= "\n" . $this->partitioning->serialize($formatter);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Parser/Ddl/TableCommandsParser.other.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ use SqlFtw\Tests\Assert;
require __DIR__ . '/../../bootstrap.php';


// combination of actions, table options and alter options
Assert::parseSerialize("ALTER TABLE tbl1 ADD COLUMN col1 INT");
Assert::parseSerialize("ALTER TABLE tbl1 ADD COLUMN col1 INT, ALGORITHM INSTANT");
Assert::parseSerialize("ALTER TABLE tbl1 ADD COLUMN col1 INT, ENGINE InnoDB");
Assert::parseSerialize("ALTER TABLE tbl1 ADD COLUMN col1 INT, ENGINE InnoDB, ALGORITHM INSTANT");
Assert::parseSerialize("ALTER TABLE tbl1 ENGINE InnoDB");
Assert::parseSerialize("ALTER TABLE tbl1 ENGINE InnoDB, ALGORITHM INSTANT");
Assert::parseSerialize("ALTER TABLE tbl1 ALGORITHM INSTANT");


// DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ... [RESTRICT | CASCADE]
Assert::parseSerialize("DROP TABLE tbl1");
Assert::parseSerialize("DROP TEMPORARY TABLE tbl1");
Expand Down

0 comments on commit bcf6f82

Please sign in to comment.