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: Allow pretending for DDL statements #170

Merged
merged 4 commits into from
Jan 25, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Added
- `Schema\Grammar::compileDropForeign` to allow dropping foreign key constraints (#163)
- `Schema\Builder::dropAllTables` works properly, dropping foreign keys, indexes, then tables in order of interleaving (#161)
- Support for inserting and selecting array of DateTime/Numeric objects (#168)
- Allow pretending for DDL statements (#170)

Changed
- `Query\Builder::lock()` no longer throw an error and will be ignored instead (#156)
Expand Down
9 changes: 6 additions & 3 deletions src/Concerns/ManagesDataDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ public function runDdlBatch(array $statements): mixed
}

$start = microtime(true);
$result = [];

$result = $this->waitForOperation(
$this->getSpannerDatabase()->updateDdlBatch($statements),
);
if (!$this->pretending()) {
$result = $this->waitForOperation(
$this->getSpannerDatabase()->updateDdlBatch($statements),
);
}

foreach ($statements as $statement) {
$this->logQuery($statement, [], $this->getElapsedTime($start));
Expand Down
22 changes: 22 additions & 0 deletions tests/Concerns/ManagesDataDefinitionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,30 @@ public function test_runDdlBatch(): void
$this->assertSame([], $result);
$this->assertSame($statement, $conn->getQueryLog()[0]['query']);
$this->assertCount(1, $conn->getQueryLog());
Event::assertDispatchedTimes(QueryExecuted::class, 1);
$this->assertContains($newTable, array_map(fn ($d) => $d['name'], $conn->getSchemaBuilder()->getTables()));
}

public function test_runDdlBatch_within_pretend(): void
{
$conn = $this->getDefaultConnection();
$conn->setEventDispatcher(Event::fake([QueryExecuted::class]));
$conn->enableQueryLog();

$newTable = $this->generateTableName('runDdlBatch');
$statement = "create table {$newTable} (id int64) primary key (id)";

$result = null;
$conn->pretend(function (Connection $conn) use (&$result, $statement) {
$result = $conn->runDdlBatch([$statement]);
});

$this->assertSame([], $result);
$this->assertSame([['query' => $statement, 'bindings' => [], 'time' => 0.0]], $conn->getQueryLog());

Event::assertDispatchedTimes(QueryExecuted::class, 1);

$this->assertFalse($conn->getSchemaBuilder()->hasTable($newTable));
}

public function test_runDdlBatch_with_empty_statement(): void
Expand Down