feat(tickets): group user tickets by event
This commit is contained in:
@@ -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');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user