Skip to content

Commit

Permalink
fix db findOrFail question and fix redis muit no release connection
Browse files Browse the repository at this point in the history
  • Loading branch information
sakuraovq committed Oct 30, 2019
1 parent 7492415 commit e5f6980
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 29 deletions.
26 changes: 17 additions & 9 deletions src/db/src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
* @method static Collection fromQuery(string $query, array $bindings = [])
* @method static static find($id, array $columns = ['*'])
* @method static Collection findMany(array $ids, array $columns = ['*'])
* @method static Builder findOrFail($id, array $columns = ['*'])
* @method static Builder findOrNew($id, array $columns = ['*'])
* @method static Builder firstOrNew(array $attributes, array $values = [])
* @method static static findOrFail($id, array $columns = ['*'])
* @method static static findOrNew($id, array $columns = ['*'])
* @method static static firstOrNew(array $attributes, array $values = [])
* @method static static firstOrCreate(array $attributes, array $values = [])
* @method static static updateOrCreate(array $attributes, array $values = [], array $counters = [])
* @method static bool updateOrInsert(array $attributes, array $values = [], array $counters = [])
Expand Down Expand Up @@ -169,7 +169,6 @@
* @method static float|int average(string $column)
* @method static void truncate()
* @method static Builder useWritePdo()
* @method static int getCountForPagination(array $columns = ['*'])
*/
abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
{
Expand Down Expand Up @@ -474,7 +473,7 @@ public function syncCounter(array $counters, array $extra = []): self
* @throws DbException
* @throws ReflectionException
*/
public function save()
public function save(): bool
{
$query = $this->newModelQuery();

Expand Down Expand Up @@ -548,7 +547,7 @@ protected function finishSave()
* @throws DbException
* @throws ReflectionException
*/
protected function performUpdate(Builder $query)
protected function performUpdate(Builder $query): bool
{
// If the updating event returns false, we will cancel the update operation so
// developers can hook Validation systems into their models and cancel this
Expand All @@ -563,11 +562,13 @@ protected function performUpdate(Builder $query)
$dirty = $this->getDirty();

if (count($dirty) > 0) {
$this->setKeysForSaveQuery($query)->update($dirty);
$result = (bool)$this->setKeysForSaveQuery($query)->update($dirty);

$this->syncChanges();

$this->fireEvent(DbEvent::MODEL_UPDATED);

return $result;
}

return true;
Expand All @@ -583,7 +584,14 @@ protected function performUpdate(Builder $query)
*/
protected function setKeysForSaveQuery(Builder $query)
{
$query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery());
$id = $this->getKeyForSaveQuery();
$keyName = $this->getKeyName();

if ($id === null) {
throw new DbException(sprintf('Save record %s value must not null', $keyName));
}

$query->where($keyName, '=', $id);

return $query;
}
Expand All @@ -610,7 +618,7 @@ protected function getKeyForSaveQuery()
* @throws DbException
* @throws ReflectionException
*/
protected function performInsert(Builder $query)
protected function performInsert(Builder $query): bool
{
if ($this->fireEvent(DbEvent::MODEL_CREATING) === false) {
return false;
Expand Down
10 changes: 10 additions & 0 deletions src/db/test/unit/Eloquent/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -867,4 +867,14 @@ public function testWhereCall()
$this->assertEquals($sql, $toSql);
}

public function testFindOrFail()
{
User::updateOrCreate(['id' => 1], ['age' => 1]);

$user = User::findOrFail(1, ['age']);

// got id fail
$this->expectException(DbException::class);
$user->update(['age' => 2]);
}
}
4 changes: 4 additions & 0 deletions src/redis/src/Connection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ public function call(callable $callback, bool $reconnect = false)
$result = $callback($this->client);
Log::profileEnd('redis.%s', __FUNCTION__);
// Release Connection

$this->release();
} catch (Throwable $e) {
if (!$reconnect && $this->reconnect()) {
Expand Down Expand Up @@ -689,6 +690,9 @@ private function multi(int $mode, callable $callback, bool $reconnect = false):
$result = $pipeline->exec();

Log::profileEnd($proKey);

// Release Connection
$this->release();
} catch (Throwable $e) {
if (!$reconnect && $this->reconnect()) {
return $this->multi($mode, $callback, true);
Expand Down
43 changes: 23 additions & 20 deletions src/redis/test/unit/Command/MultiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ class MultiTest extends TestCase
{
public function testPipeline()
{
$count = 10;
$result = Redis::pipeline(function (\Redis $redis) use ($count) {
for ($i = 0; $i < $count; $i++) {
$redis->set("key:$i", $i);
}
});
$count = 100;

$this->assertEquals(\count($result), $count);
for ($i = 0; $i < $count; $i++) {
$result = Redis::pipeline(function (\Redis $redis) use ($count) {
for ($i = 0; $i < $count; $i++) {
$redis->set("key:$i", $i);
}
});

foreach ($result as $index => $value) {
$this->assertTrue($value);
$this->assertEquals(\count($result), $count);

foreach ($result as $index => $value) {
$this->assertTrue($value);
}
}

}

public function testTransaction()
Expand All @@ -42,17 +46,16 @@ public function testTransaction()
});

/**
array(4) {
[0]=>
bool(true)
[1]=>
int(0)
[2]=>
bool(true)
[3]=>
int(1)
}
* array(4) {
* [0]=>
* bool(true)
* [1]=>
* int(0)
* [2]=>
* bool(true)
* [3]=>
* int(1)
* }
*/

$this->assertEquals(\count($result), $count * 2);
Expand Down

0 comments on commit e5f6980

Please sign in to comment.