Skip to content

Commit

Permalink
feat: Allow pretending for DDL statements (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
taka-oyama authored Jan 25, 2024
1 parent 8ebf9f8 commit 017e198
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
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

0 comments on commit 017e198

Please sign in to comment.