Skip to content

Commit

Permalink
feat(database): alter table with only indices (#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt authored Dec 13, 2024
1 parent 82f1808 commit 61e7abb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/Tempest/Database/src/QueryStatements/AlterTableStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ public function test_it_can_alter_a_table_definition(): void
$this->assertSame('[email protected]', $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 {
Expand Down

0 comments on commit 61e7abb

Please sign in to comment.