From 21576cf2695ade955f3db26b3ea49f3eec8cc7c6 Mon Sep 17 00:00:00 2001 From: Yusron Arif Date: Wed, 11 Oct 2023 17:00:33 +0700 Subject: [PATCH 1/2] fix without perform_by and timestamptz, softdeletetz --- .../Eloquent/Concerns/HasTimestamps.php | 12 ++- src/Database/Eloquent/Model.php | 15 +--- src/Database/Eloquent/SoftDeletes.php | 35 +++++--- src/Database/Schema/Blueprint.php | 80 ++++++++++++------- 4 files changed, 86 insertions(+), 56 deletions(-) diff --git a/src/Database/Eloquent/Concerns/HasTimestamps.php b/src/Database/Eloquent/Concerns/HasTimestamps.php index b2362bd..8b2954f 100644 --- a/src/Database/Eloquent/Concerns/HasTimestamps.php +++ b/src/Database/Eloquent/Concerns/HasTimestamps.php @@ -19,9 +19,11 @@ trait HasTimestamps */ public function setCreatedAt($value): static { - $this->setPerformedBy(); $this->{$this->getCreatedAtColumn()} = $value; - $this->{$this->getCreatedByColumn()} = $this->performBy; + if (config('koffinate.core.model.use_perform_by')) { + $this->setPerformedBy(); + $this->{$this->getCreatedByColumn()} = $this->performBy; + } return $this; } @@ -35,9 +37,11 @@ public function setCreatedAt($value): static */ public function setUpdatedAt($value): static { - $this->setPerformedBy(); $this->{$this->getUpdatedAtColumn()} = $value; - $this->{$this->getUpdatedByColumn()} = $this->performBy; + if (config('koffinate.core.model.use_perform_by')) { + $this->setPerformedBy(); + $this->{$this->getUpdatedByColumn()} = $this->performBy; + } return $this; } diff --git a/src/Database/Eloquent/Model.php b/src/Database/Eloquent/Model.php index 6292b6b..23f7148 100644 --- a/src/Database/Eloquent/Model.php +++ b/src/Database/Eloquent/Model.php @@ -37,16 +37,6 @@ class Model extends BaseModel */ protected bool $useTryOnEnumCast = true; - /** - * The attributes that should be hidden for arrays. - * - * @var array - */ - protected $hidden = [ - 'created_at', 'updated_at', 'deleted_at', 'restore_at', - 'created_by', 'updated_by', 'deleted_by', 'restore_by', - ]; - /** * Create a new Eloquent model instance. * @@ -182,7 +172,10 @@ protected function performerAsPlain(?string $performer = null): Fluent */ protected function setPerformedBy(): void { - if (auth()->user() && empty($this->performBy) && config('koffinate.core.model.use_perform_by')) { + // reset performer + $this->performBy = null; + + if (auth()->check() && config('koffinate.core.model.use_perform_by') && empty($this->performBy)) { $user = auth()->user(); if ($user instanceof User) { if ($this->performerMode == 'users') { diff --git a/src/Database/Eloquent/SoftDeletes.php b/src/Database/Eloquent/SoftDeletes.php index 57c1811..5924963 100644 --- a/src/Database/Eloquent/SoftDeletes.php +++ b/src/Database/Eloquent/SoftDeletes.php @@ -17,27 +17,34 @@ protected function runSoftDelete(): void { $query = $this->setKeysForSaveQuery($this->newModelQuery()); - $this->setPerformedBy(); - $time = $this->freshTimestamp(); - $columns = [ - $this->getDeletedAtColumn() => $this->fromDateTime($time), - $this->getDeletedByColumn() => $this->performBy, - ]; + $columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)]; $this->{$this->getDeletedAtColumn()} = $time; - $this->{$this->getDeletedByColumn()} = $this->performBy; + + if (config('koffinate.core.model.use_perform_by')) { + $this->setPerformedBy(); + $columns[$this->getDeletedByColumn()] = $this->performBy; + $this->{$this->getDeletedByColumn()} = $this->performBy; + } if ($this->timestamps && ! is_null($this->getUpdatedAtColumn())) { $this->{$this->getUpdatedAtColumn()} = $time; - $this->{$this->getUpdatedByColumn()} = $this->performBy; $columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time); - $columns[$this->getUpdatedByColumn()] = $this->performBy; + + if (config('koffinate.core.model.use_perform_by')) { + $this->{$this->getUpdatedByColumn()} = $this->performBy; + $columns[$this->getUpdatedByColumn()] = $this->performBy; + } } $query->update($columns); + + $this->syncOriginalAttributes(array_keys($columns)); + + $this->fireModelEvent('trashed', false); } /** @@ -54,12 +61,14 @@ public function restore(): ?bool return false; } - $this->setPerformedBy(); - $this->{$this->getDeletedAtColumn()} = null; - $this->{$this->getDeletedByColumn()} = null; $this->{$this->getRestoreAtColumn()} = $this->freshTimestamp(); - $this->{$this->getRestoreByColumn()} = $this->performBy; + + if (config('koffinate.core.model.use_perform_by')) { + $this->setPerformedBy(); + $this->{$this->getDeletedByColumn()} = null; + $this->{$this->getRestoreByColumn()} = $this->performBy; + } // Once we have saved the model, we will fire the "restored" event so this // developer will do anything they need to after a restore operation is diff --git a/src/Database/Schema/Blueprint.php b/src/Database/Schema/Blueprint.php index 8e2a9b4..f3da62f 100644 --- a/src/Database/Schema/Blueprint.php +++ b/src/Database/Schema/Blueprint.php @@ -48,23 +48,26 @@ public function __construct($table, Closure $callback = null, $prefix = '') */ public function timestamps($precision = 0): void { - $foreignType = in_array($this->userKeyType, ['int', 'integer']) ? 'foreignId' : 'foreignUuid'; - $this->timestamp('created_at', $precision)->nullable(); - if ($this->performerMode == 'users') { - $this->{$foreignType}('created_by')->nullable() - ->constrained($this->tableUser)->onUpdate('cascade')->onDelete('restrict'); - } else { - $this->string('created_by', 100)->nullable(); - } + $this->makePerformerColumn('created_by'); $this->timestamp('updated_at', $precision)->nullable(); - if ($this->performerMode == 'users') { - $this->{$foreignType}('updated_by')->nullable() - ->constrained($this->tableUser)->onUpdate('cascade')->onDelete('restrict'); - } else { - $this->string('updated_by', 100)->nullable(); - } + $this->makePerformerColumn('updated_by'); + } + + /** + * Add creation and update timestampTz columns to the table. + * + * @param int|null $precision + * @return void + */ + public function timestampsTz($precision = 0): void + { + $this->timestampTz('created_at', $precision)->nullable(); + $this->makePerformerColumn('created_by'); + + $this->timestampTz('updated_at', $precision)->nullable(); + $this->makePerformerColumn('updated_by'); } /** @@ -77,27 +80,48 @@ public function timestamps($precision = 0): void */ public function softDeletes($column = 'deleted_at', $precision = 0): ColumnDefinition { - $foreignType = in_array($this->userKeyType, ['int', 'integer']) ? 'foreignId' : 'foreignUuid'; - + $deletedByColumn = str($column)->replaceMatches('/_at$/i', '')->append('_by')->toString(); $deletedColum = $this->timestamp($column, $precision)->nullable(); - if ($this->performerMode == 'users') { - $this->{$foreignType}('deleted_by')->nullable() - ->constrained($this->tableUser)->onUpdate('cascade')->onDelete('restrict'); - } else { - $this->string('deleted_by', 100)->nullable(); - } + $this->makePerformerColumn($deletedByColumn); $this->timestamp('restore_at', $precision)->nullable(); - if ($this->performerMode == 'users') { - $this->{$foreignType}('restore_by')->nullable() - ->constrained($this->tableUser)->onUpdate('cascade')->onDelete('restrict'); - } else { - $this->string('restore_by', 100)->nullable(); - } + $this->makePerformerColumn('restore_by'); + + return $deletedColum; + } + + /** + * Add a "deleted at" timestampTz for the table. + * + * @param string $column + * @param int|null $precision + * @return \Illuminate\Database\Schema\ColumnDefinition + */ + public function softDeletesTz($column = 'deleted_at', $precision = 0): ColumnDefinition + { + $deletedByColumn = str($column)->replaceMatches('/_at$/i', '')->append('_by')->toString(); + $deletedColum = $this->timestampTz($column, $precision)->nullable(); + $this->makePerformerColumn($deletedByColumn); + + $this->timestampTz('restore_at', $precision)->nullable(); + $this->makePerformerColumn('restore_by'); return $deletedColum; } + private function makePerformerColumn(string $column): void + { + if (config('koffinate.core.model.use_perform_by')) { + if ($this->performerMode == 'users') { + $foreignType = in_array($this->userKeyType, ['int', 'integer']) ? 'foreignId' : 'foreignUuid'; + $this->{$foreignType}($column)->nullable() + ->constrained($this->tableUser)->onUpdate('cascade')->onDelete('restrict'); + } else { + $this->string($column, 100)->nullable(); + } + } + } + /** * Add the proper columns for a polymorphic table. * From 87f23a9e48b8a334c80261d0af9fb1619657cb10 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 11 Oct 2023 10:03:00 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- src/Database/Schema/Blueprint.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Database/Schema/Blueprint.php b/src/Database/Schema/Blueprint.php index f3da62f..ed9bbc9 100644 --- a/src/Database/Schema/Blueprint.php +++ b/src/Database/Schema/Blueprint.php @@ -59,6 +59,7 @@ public function timestamps($precision = 0): void * Add creation and update timestampTz columns to the table. * * @param int|null $precision + * * @return void */ public function timestampsTz($precision = 0): void @@ -95,6 +96,7 @@ public function softDeletes($column = 'deleted_at', $precision = 0): ColumnDefin * * @param string $column * @param int|null $precision + * * @return \Illuminate\Database\Schema\ColumnDefinition */ public function softDeletesTz($column = 'deleted_at', $precision = 0): ColumnDefinition