feat(tickets): group user tickets by event

This commit is contained in:
2026-09-22 12:26:16 -03:00
parent 6efe8439c7
commit 3401c934b2
12 changed files with 289 additions and 20 deletions

View File

@@ -0,0 +1,52 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('tickets', function (Blueprint $table): void {
$table->foreignId('event_id')
->nullable()
->after('tenant_code')
->constrained('events')
->cascadeOnUpdate()
->restrictOnDelete();
$table->index(['tenant_code', 'user_id', 'event_id']);
});
DB::table('tickets')
->join('catalog_items', 'catalog_items.id', '=', 'tickets.source_catalog_item_id')
->whereNotNull('catalog_items.event_id')
->select(['tickets.id', 'catalog_items.event_id as source_event_id'])
->orderBy('tickets.id')
->chunk(500, function ($tickets): void {
foreach ($tickets as $ticket) {
DB::table('tickets')
->where('id', $ticket->id)
->update(['event_id' => $ticket->source_event_id]);
}
});
}
public function down(): void
{
if (DB::getDriverName() === 'sqlite') {
Schema::table('tickets', function (Blueprint $table): void {
$table->dropIndex(['tenant_code', 'user_id', 'event_id']);
$table->dropColumn('event_id');
});
return;
}
Schema::table('tickets', function (Blueprint $table): void {
$table->dropIndex(['tenant_code', 'user_id', 'event_id']);
$table->dropConstrainedForeignId('event_id');
});
}
};