sqlftw icon indicating copy to clipboard operation
sqlftw copied to clipboard

Serialization issue when copying AlterTableCommand

Open krystof018 opened this issue 2 months ago • 0 comments

Hello, I came across bug when creating new instance of AlterTableCommand from old one.

$actions = getMeNewActions($originalCommand);

 $newCommand = new AlterTableCommand(
    $originalCommand->getTable(),
    $actions,
    $originalCommand->getAlterOptions(),
    $originalCommand->getOptionsList(), // this causes the problem
    $originalCommand->getPartitioning(),
);

Problem is with AlterTableCommand::getOptionsList().

public function getOptionsList(): TableOptionsList
{
    return $this->tableOptions ?? new TableOptionsList([]);
}

This would not be problematic itself if serialization of AlterTableCommand did not depend on null check:

// in AlterTableCommand::serialize() line 127
if ($this->tableOptions !== null) {
    if (!$this->actions->isEmpty()) {
        $result .= ',';
    }
    $result .= "\n" . $formatter->indent . $this->tableOptions->serialize($formatter, ",\n", ' ');
}

The logic should probably check if the TableOptionsList is empty so the comma + \n + indent there is not added without reason since empty TableOptionsList serializes to ''.

krystof018 avatar Oct 09 '25 13:10 krystof018