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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: updateOrInsert signature change in 11.10 #216

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace Colopl\Spanner\Query;

use Closure;
use Colopl\Spanner\Connection;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Query\Builder as BaseBuilder;
Expand Down Expand Up @@ -61,13 +62,23 @@ public function update(array $values)
/**
* @inheritDoc
*/
public function updateOrInsert(array $attributes, array $values = [])
public function updateOrInsert(array $attributes, array|callable $values = [])
{
if (! $this->where($attributes)->exists()) {
$exists = $this->where($attributes)->exists();

if ($values instanceof Closure) {
$values = $values($exists);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


if (! $exists) {
return $this->insert(array_merge($attributes, $values));
}

return (bool) $this->take(1)->update(Arr::except($values, array_keys($attributes)));
if (empty($values)) {
return true;
}

return (bool) $this->limit(1)->update(Arr::except($values, array_keys($attributes)));
}

/**
Expand Down
Loading