diff --git a/src/Tempest/Database/src/QueryStatements/AlterTableStatement.php b/src/Tempest/Database/src/QueryStatements/AlterTableStatement.php index 6e9744f25..96cbce3a2 100644 --- a/src/Tempest/Database/src/QueryStatements/AlterTableStatement.php +++ b/src/Tempest/Database/src/QueryStatements/AlterTableStatement.php @@ -89,16 +89,20 @@ public function drop(QueryStatement $statement): self public function compile(DatabaseDialect $dialect): string { - $alterTable = sprintf( - 'ALTER TABLE %s %s;', - new TableName($this->tableName), - arr($this->statements) - ->map(fn (QueryStatement $queryStatement) => str($queryStatement->compile($dialect))->trim()->replace(' ', ' ')) - ->filter(fn (StringHelper $line) => $line->isNotEmpty()) - ->implode(', ' . PHP_EOL . ' ') - ->wrap(before: PHP_EOL . ' ', after: PHP_EOL) - ->toString(), - ); + if ($this->statements !== []) { + $alterTable = sprintf( + 'ALTER TABLE %s %s;', + new TableName($this->tableName), + arr($this->statements) + ->map(fn (QueryStatement $queryStatement) => str($queryStatement->compile($dialect))->trim()->replace(' ', ' ')) + ->filter(fn (StringHelper $line) => $line->isNotEmpty()) + ->implode(', ' . PHP_EOL . ' ') + ->wrap(before: PHP_EOL . ' ', after: PHP_EOL) + ->toString(), + ); + } else { + $alterTable = ''; + } if ($this->createIndexStatements !== []) { $createIndices = PHP_EOL . arr($this->createIndexStatements) diff --git a/tests/Integration/Database/QueryStatements/AlterTableStatementTest.php b/tests/Integration/Database/QueryStatements/AlterTableStatementTest.php index 74247c975..68292e709 100644 --- a/tests/Integration/Database/QueryStatements/AlterTableStatementTest.php +++ b/tests/Integration/Database/QueryStatements/AlterTableStatementTest.php @@ -71,6 +71,16 @@ public function test_it_can_alter_a_table_definition(): void $this->assertSame('test@example.com', $user->email); } + public function test_alter_for_only_indexes(): void + { + $statement = new AlterTableStatement('table') + ->index('foo') + ->unique('bar') + ->compile(DatabaseDialect::SQLITE); + + $this->assertStringNotContainsString('ALTER TABLE', $statement); + } + private function getAlterTableMigration(): mixed { return new class () implements DatabaseMigration {