Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(database): alter table with only indices #852

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading