Skip to content

Commit 61e7abb

Browse files
authored
feat(database): alter table with only indices (#852)
1 parent 82f1808 commit 61e7abb

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/Tempest/Database/src/QueryStatements/AlterTableStatement.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,20 @@ public function drop(QueryStatement $statement): self
8989

9090
public function compile(DatabaseDialect $dialect): string
9191
{
92-
$alterTable = sprintf(
93-
'ALTER TABLE %s %s;',
94-
new TableName($this->tableName),
95-
arr($this->statements)
96-
->map(fn (QueryStatement $queryStatement) => str($queryStatement->compile($dialect))->trim()->replace(' ', ' '))
97-
->filter(fn (StringHelper $line) => $line->isNotEmpty())
98-
->implode(', ' . PHP_EOL . ' ')
99-
->wrap(before: PHP_EOL . ' ', after: PHP_EOL)
100-
->toString(),
101-
);
92+
if ($this->statements !== []) {
93+
$alterTable = sprintf(
94+
'ALTER TABLE %s %s;',
95+
new TableName($this->tableName),
96+
arr($this->statements)
97+
->map(fn (QueryStatement $queryStatement) => str($queryStatement->compile($dialect))->trim()->replace(' ', ' '))
98+
->filter(fn (StringHelper $line) => $line->isNotEmpty())
99+
->implode(', ' . PHP_EOL . ' ')
100+
->wrap(before: PHP_EOL . ' ', after: PHP_EOL)
101+
->toString(),
102+
);
103+
} else {
104+
$alterTable = '';
105+
}
102106

103107
if ($this->createIndexStatements !== []) {
104108
$createIndices = PHP_EOL . arr($this->createIndexStatements)

tests/Integration/Database/QueryStatements/AlterTableStatementTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ public function test_it_can_alter_a_table_definition(): void
7171
$this->assertSame('[email protected]', $user->email);
7272
}
7373

74+
public function test_alter_for_only_indexes(): void
75+
{
76+
$statement = new AlterTableStatement('table')
77+
->index('foo')
78+
->unique('bar')
79+
->compile(DatabaseDialect::SQLITE);
80+
81+
$this->assertStringNotContainsString('ALTER TABLE', $statement);
82+
}
83+
7484
private function getAlterTableMigration(): mixed
7585
{
7686
return new class () implements DatabaseMigration {

0 commit comments

Comments
 (0)