From 0f0690235206af4640bc572577601a42960e33ff Mon Sep 17 00:00:00 2001 From: Adamczyk Piotr Date: Sat, 9 Dec 2023 13:20:18 +0100 Subject: [PATCH 1/3] feat: remove redundant eloquent queries with no geometry changes --- src/Database/Eloquent/HasPostgisColumns.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Database/Eloquent/HasPostgisColumns.php b/src/Database/Eloquent/HasPostgisColumns.php index 5d98f58..e39af44 100644 --- a/src/Database/Eloquent/HasPostgisColumns.php +++ b/src/Database/Eloquent/HasPostgisColumns.php @@ -9,11 +9,16 @@ use Clickbar\Magellan\IO\Generator\BaseGenerator; use Clickbar\Magellan\IO\Generator\WKT\WKTGenerator; use Clickbar\Magellan\IO\Parser\WKB\WKBParser; +use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; +use Illuminate\Database\Eloquent\Concerns\HasAttributes; use Illuminate\Support\Arr; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Config; +/** + * @mixin Model + */ trait HasPostgisColumns { public function getPostgisTypeAndSrid(string $key) @@ -141,6 +146,22 @@ public function setRawAttributes(array $attributes, $sync = false) return parent::setRawAttributes($attributes, $sync); } + public function originalIsEquivalent($key) + { + $this->assertPostgisColumnsNotEmpty(); + + // Not a postgis column + if (array_key_exists($key, $this->postgisColumns) === false) { + return parent::originalIsEquivalent($key); + } + + $attribute = Arr::get($this->attributes, $key); + $original = Arr::get($this->original, $key); + + // Compare already cast objects + return $attribute == $original; + } + protected function assertPostgisColumnsNotEmpty() { if (! property_exists($this, 'postgisColumns')) { From 11773ca08de0e805b5e1c7b6584dc53880b01ec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kr=C3=B6ll?= Date: Wed, 27 Dec 2023 10:41:29 +0100 Subject: [PATCH 2/3] chore: Update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 555edf0..4f77710 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Fixed +- Fixed model attribute equality checks for geometries, reduces unnecessary database updates (thanks @adamczykpiotr) + ## [1.4.0](https://github.com/clickbar/laravel-magellan/tree/1.4.0) - 2023-07-02 ### Improved From 982a729ceffc5a2fd889b36547e6076ff34104dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kr=C3=B6ll?= Date: Wed, 27 Dec 2023 10:42:18 +0100 Subject: [PATCH 3/3] chore: Format using pint --- src/Database/Eloquent/HasPostgisColumns.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Database/Eloquent/HasPostgisColumns.php b/src/Database/Eloquent/HasPostgisColumns.php index e39af44..1e72e92 100644 --- a/src/Database/Eloquent/HasPostgisColumns.php +++ b/src/Database/Eloquent/HasPostgisColumns.php @@ -9,9 +9,8 @@ use Clickbar\Magellan\IO\Generator\BaseGenerator; use Clickbar\Magellan\IO\Generator\WKT\WKTGenerator; use Clickbar\Magellan\IO\Parser\WKB\WKBParser; -use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; -use Illuminate\Database\Eloquent\Concerns\HasAttributes; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Config;