workign comments strucuture
This commit is contained in:
+132
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Helpdesk\Models\Comment;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('helpdesk_comments', static function (Blueprint $table): void {
|
||||
if (! Schema::hasColumn('helpdesk_comments', 'guid')) {
|
||||
$table->uuid('guid')->nullable()->after('id');
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('helpdesk_comments', 'unid')) {
|
||||
$table->string('unid', 64)->nullable()->after('guid');
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('helpdesk_comments', 'replica_id')) {
|
||||
$table->string('replica_id', 64)->nullable()->after('unid');
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('helpdesk_comments', 'parent_unid')) {
|
||||
$table->string('parent_unid', 64)->nullable()->after('parent_id');
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('helpdesk_comments', 'root_unid')) {
|
||||
$table->string('root_unid', 64)->nullable()->after('parent_unid');
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('helpdesk_comments', 'originator_id')) {
|
||||
$table->string('originator_id', 96)->nullable()->after('root_unid');
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('helpdesk_comments', 'sync_sequence')) {
|
||||
$table->unsignedBigInteger('sync_sequence')->default(1)->after('originator_id');
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('helpdesk_comments', 'sync_hash')) {
|
||||
$table->string('sync_hash', 128)->nullable()->after('sync_sequence');
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('helpdesk_comments', 'source_system')) {
|
||||
$table->string('source_system', 80)->default('wms-helpdesk')->after('sync_hash');
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('helpdesk_comments', 'source_record_id')) {
|
||||
$table->string('source_record_id', 120)->nullable()->after('source_system');
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('helpdesk_comments', 'body_text')) {
|
||||
$table->text('body_text')->nullable()->after('body');
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('helpdesk_comments', 'replicated_at')) {
|
||||
$table->timestamp('replicated_at')->nullable()->after('updated_at');
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('helpdesk_comments', 'synced_at')) {
|
||||
$table->timestamp('synced_at')->nullable()->after('replicated_at');
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('helpdesk_comments', 'tombstoned_at')) {
|
||||
$table->timestamp('tombstoned_at')->nullable()->after('synced_at');
|
||||
}
|
||||
});
|
||||
|
||||
Comment::query()->orderBy('id')->chunkById(100, static function ($comments): void {
|
||||
foreach ($comments as $comment) {
|
||||
$guid = $comment->guid ?: (string) Str::uuid();
|
||||
$unid = $comment->unid ?: strtoupper(hash('sha256', $guid.'|'.$comment->ticket_id.'|'.$comment->id));
|
||||
$parent = $comment->parent_id ? Comment::query()->find($comment->parent_id) : null;
|
||||
$parentUnid = $parent?->unid;
|
||||
$rootUnid = $parent?->root_unid ?: $parentUnid ?: $unid;
|
||||
|
||||
DB::table('helpdesk_comments')
|
||||
->where('id', $comment->id)
|
||||
->update([
|
||||
'guid' => $guid,
|
||||
'unid' => $unid,
|
||||
'parent_unid' => $parentUnid,
|
||||
'root_unid' => $rootUnid,
|
||||
'originator_id' => $comment->originator_id ?: $unid.':1',
|
||||
'sync_sequence' => $comment->sync_sequence ?: 1,
|
||||
'sync_hash' => hash('sha256', (string) $comment->body),
|
||||
'body_text' => trim(strip_tags((string) $comment->body)),
|
||||
'source_system' => $comment->source_system ?: 'wms-helpdesk',
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
Schema::table('helpdesk_comments', static function (Blueprint $table): void {
|
||||
$table->unique('guid', 'helpdesk_comments_guid_unique');
|
||||
$table->unique('unid', 'helpdesk_comments_unid_unique');
|
||||
$table->index(['ticket_id', 'root_unid', 'parent_unid'], 'helpdesk_comments_lotus_thread_index');
|
||||
$table->index(['ticket_id', 'is_internal', 'created_at'], 'helpdesk_comments_visibility_index');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('helpdesk_comments', static function (Blueprint $table): void {
|
||||
$table->dropUnique('helpdesk_comments_guid_unique');
|
||||
$table->dropUnique('helpdesk_comments_unid_unique');
|
||||
$table->dropIndex('helpdesk_comments_lotus_thread_index');
|
||||
$table->dropIndex('helpdesk_comments_visibility_index');
|
||||
|
||||
$table->dropColumn([
|
||||
'guid',
|
||||
'unid',
|
||||
'replica_id',
|
||||
'parent_unid',
|
||||
'root_unid',
|
||||
'originator_id',
|
||||
'sync_sequence',
|
||||
'sync_hash',
|
||||
'source_system',
|
||||
'source_record_id',
|
||||
'body_text',
|
||||
'replicated_at',
|
||||
'synced_at',
|
||||
'tombstoned_at',
|
||||
]);
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user