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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw exception when column id is not existed #5239

Merged
merged 3 commits into from
Dec 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG-3.0.md
Expand Up @@ -121,6 +121,7 @@ composer analyse
- [#5091](https://github.com/hyperf/hyperf/pull/5091) Move `Jsonable` and `Xmlable` to `contract` from `utils`.
- [#5092](https://github.com/hyperf/hyperf/pull/5092) Move `MessageBag` and `MessageProvider` to `contract` from `utils`.
- [#5204](https://github.com/hyperf/hyperf/pull/5204) Transform the type of param `$server` in `Hyperf\WebSocketServer\Server::deferOnOpen()` to `mixed`.
- [#5239](https://github.com/hyperf/hyperf/pull/5239) Throw exception when using `chunkById` but the column is not existed.

## Swow Supported

Expand Down Expand Up @@ -172,4 +173,4 @@ composer analyse
- [#5199](https://github.com/hyperf/hyperf/pull/5199) Fixed bug that `RedisSentinel` can't support empty password.
- [#5221](https://github.com/hyperf/hyperf/pull/5221) Fixed bug that `PGSqlSwooleConnection::affectingStatement()` can't work when the `sql` is wrong.
- [#5223](https://github.com/hyperf/hyperf/pull/5223) Fixed bug that `KeepaliveConnection::isTimeout()` can't work when using swow.
- [#5228](https://github.com/hyperf/hyperf/issues/5228) Fixed bug that proxy class will be generated failed when using parameters who allow null in constructor.
- [#5229](https://github.com/hyperf/hyperf/pull/5229) Fixed bug that proxy class will be generated failed when using parameters who allow null in constructor.
4 changes: 4 additions & 0 deletions src/database/src/Query/Builder.php
Expand Up @@ -1994,6 +1994,10 @@ public function chunkById($count, callable $callback, $column = 'id', $alias = n
$lastResult = $results->last();
$lastId = is_array($lastResult) ? $lastResult[$alias] : $lastResult->{$alias};

if ($lastId === null) {
throw new RuntimeException("The chunkById operation was aborted because the [{$alias}] column is not present in the query result.");
}

unset($results);
} while ($countResults == $count);

Expand Down
30 changes: 30 additions & 0 deletions src/database/tests/ModelRealBuilderTest.php
Expand Up @@ -12,6 +12,7 @@
namespace HyperfTest\Database;

use Carbon\Carbon;
use Hyperf\Context\Context;
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Contract\PaginatorInterface;
use Hyperf\Database\Connection;
Expand Down Expand Up @@ -42,6 +43,7 @@
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use RuntimeException;

/**
* @internal
Expand Down Expand Up @@ -384,6 +386,34 @@ public function testSimplePaginate()
}
}

public function testChunkById()
{
$container = $this->getContainer();
$container->shouldReceive('make')->with(PaginatorInterface::class, Mockery::any())->andReturnUsing(function ($_, $args) {
return new Paginator(...array_values($args));
});
$container->shouldReceive('get')->with(Db::class)->andReturn(new Db($container));
Context::set($key = 'chunk.by.id.' . uniqid(), 0);
Db::table('user')->chunkById(2, function ($data) use ($key) {
$id = $data->first()->id;
$this->assertNotSame($id, Context::get($key));
Context::set($key, $id);
});
}

public function testChunkByIdButNotFound()
{
$container = $this->getContainer();
$container->shouldReceive('make')->with(PaginatorInterface::class, Mockery::any())->andReturnUsing(function ($_, $args) {
return new Paginator(...array_values($args));
});
$container->shouldReceive('get')->with(Db::class)->andReturn(new Db($container));

$this->expectException(RuntimeException::class);

Db::table('user')->chunkById(1, fn () => 1, 'created_at');
}

public function testPaginationCountQuery()
{
$container = $this->getContainer();
Expand Down