Skip to content

Commit

Permalink
update: drop user_id for owner morphs
Browse files Browse the repository at this point in the history
  • Loading branch information
rbondoc96 committed Nov 12, 2024
1 parent 0651f0d commit 6dfea2a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
29 changes: 29 additions & 0 deletions database/migrations/2024_11_12_000006_drop_user_id_for_owner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('audit_changes', function (Blueprint $table) {
$table->morphs('owner');
$table->dropColumn('user_id');
});
}

public function down(): void
{
Schema::table('audit_changes', function (Blueprint $table) {
$table
->foreign('user_id')
->references(config('activity-logger.user.foreign_key', 'id'))
->on(config('activity-logger.user.table', 'users'))
->onDelete('RESTRICT');
$table->dropColumn('owner_id');
$table->dropColumn('owner_type');
});
}
};
4 changes: 2 additions & 2 deletions src/Commands/AuditModelResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function handle(): void
AuditChange::query()->where('processed', false)->chunkById(200, function ($items) {
/** @var AuditChange $item */
foreach ($items as $item) {
$id = AuditResolver::findUserId($item->entity);
$id = AuditResolver::findOwner($item->entity)?->getKey();
$item->processed = true;
$item->user_id = $id;
$item->saveOrFail();
Expand All @@ -27,7 +27,7 @@ public function handle(): void
AuditModel::query()->where('processed', false)->chunkById(200, function ($items) {
/** @var AuditModel $item */
foreach ($items as $item) {
$id = AuditResolver::findUserId($item->entity);
$id = AuditResolver::findOwner($item->entity)?->getKey();
$item->processed = true;
$item->user_id = $id;
$item->saveOrFail();
Expand Down
8 changes: 4 additions & 4 deletions src/Helpers/AuditResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@

class AuditResolver
{
public static function findUserId(?Model $model): ?int
public static function findOwner(?Model $model): ?Model
{
if (empty($model)) {
return null;
}

$id = $model->trackableUserResolver();
$owner = $model->trackableOwnerResolver();

if (empty($id)) {
if (empty($owner)) {
return null;
}

return $id;
return $owner;
}
}
12 changes: 6 additions & 6 deletions src/Models/AuditChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

namespace Sourcetoad\Logger\Models;

use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Sourcetoad\Logger\Logger;
use Sourcetoad\Logger\Traits\Immutable;

/**
Expand All @@ -17,11 +15,12 @@
* @property int $activity_id
* @property int $entity_type
* @property int $entity_id
* @property int|null $user_id
* @property int $owner_id
* @property string $owner_type
* @property int|null $key_id
* @property bool $processed
* @property-read AuditKey $key
* @property-read User|null $user
* @property-read Model|null $owner
* @property-read Model $entity
*/
class AuditChange extends BaseModel
Expand Down Expand Up @@ -50,9 +49,10 @@ class AuditChange extends BaseModel
// Relations
//--------------------------------------------------------------------------------------------------------------

public function user(): BelongsTo
/** @return MorphTo<Model, self> */
public function owner(): MorphTo
{
return $this->belongsTo(Logger::$userModel);
return $this->morphTo('owner');
}

public function key(): BelongsTo
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/Trackable.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public static function bootTrackable(): void
});
}

public function trackableUserResolver(): ?int
public function trackableOwnerResolver(): ?Model
{
throw new \BadMethodCallException('This method (trackableUserResolver) needs ' .
throw new \BadMethodCallException('This method (trackableOwnerResolver) needs ' .
'to be implemented in the implemented class: ' . static::class);
}
}

0 comments on commit 6dfea2a

Please sign in to comment.