Skip to content

Commit

Permalink
add uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Feb 23, 2024
1 parent 4ca9ec3 commit a16264d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
42 changes: 42 additions & 0 deletions database/migrations/add_uuid_to_conversations_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use Finller\Conversation\Conversation;
use Illuminate\Support\Str;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('conversations', function (Blueprint $table) {
$table->uuid()->nullable();
});

Conversation::all()->each(function (Conversation $conversation) {
$conversation->uuid = (string) Str::uuid();
$conversation->saveQuietly([
'touch' => false
]);
});


Schema::table('conversations', function (Blueprint $table) {
$table->uuid()->nullable(false)->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('conversations', function (Blueprint $table) {
$table->dropColumn('uuid');
});
}
};
13 changes: 9 additions & 4 deletions src/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Str;

/**
* @property int $id
* @property string $uuid
* @property Collection $users
* @property ?int $owner_id
* @property ?User $owner
Expand All @@ -30,17 +32,20 @@ class Conversation extends Model
{
use HasFactory;

protected $fillable = [
'owner_id',
'metadata',
];
protected $guarded = [];

protected $casts = [
'metadata' => AsArrayObject::class,
];

protected static function booted(): void
{
static::creating(function (Conversation $conversation) {
if (empty($conversation->uuid)) {
$conversation->uuid = (string) Str::uuid();
}
});

/**
* Cleanup pivot records
* We choose to not use onCascade Delete at the database level for 3 reasons:
Expand Down
4 changes: 2 additions & 2 deletions src/ConversationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function configurePackage(Package $package): void
'create_conversations_table',
'create_messages_table',
'create_conversation_user_table',
'add_uuid_to_conversations_table',
])
->hasConfigFile()
->runsMigrations();
->hasConfigFile();
}
}

0 comments on commit a16264d

Please sign in to comment.