From 1c2a1ed5a08578de82d20ab822276c0dc04a7c80 Mon Sep 17 00:00:00 2001 From: Takayasu Oyama Date: Wed, 19 Jun 2024 15:33:05 +0900 Subject: [PATCH] feat: add docs for Query\Builder::whereInUnnest (#222) feature/where-unnest --- .github/workflows/run-test.yml | 9 +++++++-- Dockerfile | 3 +-- Makefile | 3 +++ README.md | 5 +++++ compose.yaml | 2 +- phpstan.neon | 2 -- phpunit.xml | 4 +--- tests/Query/UnnestTest.php | 28 +++++++++++++++++----------- 8 files changed, 35 insertions(+), 21 deletions(-) diff --git a/.github/workflows/run-test.yml b/.github/workflows/run-test.yml index 04112c48..282a9299 100644 --- a/.github/workflows/run-test.yml +++ b/.github/workflows/run-test.yml @@ -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 diff --git a/Dockerfile b/Dockerfile index 26856379..fea48ab9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/Makefile b/Makefile index c09c1f5a..f7ded4ea 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index ddfbb12b..27f657ad 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/compose.yaml b/compose.yaml index 242d8d76..63dfe9a4 100644 --- a/compose.yaml +++ b/compose.yaml @@ -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: diff --git a/phpstan.neon b/phpstan.neon index 1a6cb9b1..614a3a04 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -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 but returns mixed\.$#' path: src/Connection.php - message: '#^Generator expects value type array, mixed given.$#' diff --git a/phpunit.xml b/phpunit.xml index bfa0e95c..612ad6c5 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -22,9 +22,7 @@ bootstrap="tests/bootstrap.php" cacheDirectory=".phpunit.cache" colors="true" - processIsolation="false" - stopOnError="true" - stopOnFailure="true"> + processIsolation="false"> ./tests ./tests diff --git a/tests/Query/UnnestTest.php b/tests/Query/UnnestTest.php index a1121296..6e6e87c2 100644 --- a/tests/Query/UnnestTest.php +++ b/tests/Query/UnnestTest.php @@ -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; @@ -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; @@ -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); + } }