Skip to content

Commit

Permalink
feat: add docs for Query\Builder::whereInUnnest (#222)
Browse files Browse the repository at this point in the history
feature/where-unnest
  • Loading branch information
taka-oyama authored Jun 19, 2024
1 parent 1c46ef7 commit 1c2a1ed
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 21 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ jobs:
uses: actions/checkout@v3
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: Run test with docker compose
run: docker-compose run test
- name: Build docker image
run: make build
- name: Run test
run: make test
- name: Show failed container logs
if: failure()
run: make logs
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ RUN pecl_mt_install() { \
&& pecl_mt_install pcov \
&& docker-php-ext-enable grpc opcache protobuf \
&& apk del .build-deps \
&& rm -rf /tmp/* \
&& mkdir -p /project/
&& rm -rf /tmp/*

WORKDIR /project
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ test: build
$(BASE_COMMAND) run test
$(BASE_COMMAND) down

logs:
$(BASE_COMMAND) logs

update:
$(BASE_COMMAND) run test composer update
$(BASE_COMMAND) down
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ Please note that the following are not required, but are strongly recommended fo
### SQL Mode
Currently only supports Spanner running GoogleSQL (PostgreSQL mode is not supported).

### Query
- [Binding more than 950 parameters in a single query will result in an error](https://cloud.google.com/spanner/quotas#query-limits)
by the server. You may by-pass this limitation by using `Query\Builder::whereInUnnest(...)` method to pass the values
as an array and unnest them on the server side instead of using query parameters.

### Eloquent
If you use interleaved keys, you MUST define them in the `interleaveKeys` property, or else you won't be able to save.
For more detailed instructions, see `Colopl\Spanner\Tests\Eloquent\ModelTest`.
Expand Down
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ services:
test:
build:
context: .
command: /bin/sh -c "composer install --no-progress && vendor/bin/phpstan --no-progress --memory-limit=-1 && vendor/bin/phpunit"
command: /bin/sh -c "composer install --no-progress && vendor/bin/phpstan --no-progress --memory-limit=-1 && composer test"
volumes:
- .:/project
depends_on:
Expand Down
2 changes: 0 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ parameters:
- message: '#^Parameter \#1 \$start of method Illuminate\\Database\\Connection::getElapsedTime\(\) expects int, float given\.$#'
path: src/Connection.php
count: 2
- message: "#^Parameter \\#1 \\$table of method Illuminate\\\\Database\\\\Query\\\\Builder\\:\\:from\\(\\) expects Closure\\|Illuminate\\\\Database\\\\Eloquent\\\\Builder\\|Illuminate\\\\Database\\\\Query\\\\Builder\\|string, Closure\\|Illuminate\\\\Contracts\\\\Database\\\\Query\\\\Expression\\|Illuminate\\\\Database\\\\Query\\\\Builder\\|string given\\.$#"
path: src/Connection.php
- message: '#^Method Colopl\\Spanner\\Connection::selectWithOptions\(\) should return array<int, array> but returns mixed\.$#'
path: src/Connection.php
- message: '#^Generator expects value type array, mixed given.$#'
Expand Down
4 changes: 1 addition & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
bootstrap="tests/bootstrap.php"
cacheDirectory=".phpunit.cache"
colors="true"
processIsolation="false"
stopOnError="true"
stopOnFailure="true">
processIsolation="false">
<testsuite name="all">
<directory suffix="Test.php">./tests</directory>
<directory suffix="TestLast.php">./tests</directory>
Expand Down
28 changes: 17 additions & 11 deletions tests/Query/UnnestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,11 @@

namespace Colopl\Spanner\Tests\Query;

use Colopl\Spanner\Tests\Eloquent\User;
use Colopl\Spanner\Tests\TestCase;

class UnnestTest extends TestCase
{
protected function createTestUser(): User
{
$user = new User();
$user->userId = $this->generateUuid();
$user->name = 'test user on EloquentTest';
return $user;
}

public function testUnnesting(): void
public function test_whereInUnnest(): void
{
$conn = $this->getDefaultConnection();
$tableName = self::TABLE_NAME_TEST;
Expand All @@ -54,7 +45,7 @@ public function testUnnesting(): void
$this->assertSame($ids->all(), $results->all());
}

public function testUnnestingEmpty(): void
public function test_whereInUnnest__with_empty_values(): void
{
$conn = $this->getDefaultConnection();
$tableName = self::TABLE_NAME_TEST;
Expand All @@ -66,4 +57,19 @@ public function testUnnestingEmpty(): void
$this->assertSame('select * from `Test` where 0 = 1', $sql);
$this->assertSame([], $results->all());
}

public function test_whereInUnnest__with_more_than_950_parameters(): void
{
$conn = $this->getDefaultConnection();
$tableName = self::TABLE_NAME_USER;
$qb = $conn->table($tableName);
$id1 = $this->generateUuid();
$id2 = $this->generateUuid();
$dummyIds = array_map($this->generateUuid(...), range(0, 950));

$qb->insert([['userId' => $id1, 'name' => 't1'], ['userId' => $id2, 'name' => 't2']]);
$given = $qb->whereInUnnest('userId', [$id1, $id2, ...$dummyIds])->pluck('userId')->sort()->values()->all();
$expected = collect([$id1, $id2])->sort()->values()->all();
$this->assertSame($expected, $given);
}
}

0 comments on commit 1c2a1ed

Please sign in to comment.