Implement ticket statuses

This commit is contained in:
2026-04-14 03:09:12 +02:00
parent 2dc12c5d60
commit 8f4a8276ac
40 changed files with 1602 additions and 100 deletions
@@ -0,0 +1,48 @@
<?php
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::create('ticket_statuses', function (Blueprint $table) {
$table->id();
$table->foreignId('project_id')->constrained()->cascadeOnDelete();
$table->foreignId('board_column_id')->nullable()->constrained()->nullOnDelete();
$table->string('name');
$table->string('slug');
$table->unsignedInteger('position');
$table->timestamps();
$table->index(['project_id', 'board_column_id', 'position']);
$table->index(['project_id', 'slug']);
});
Schema::create('ticket_status_transitions', function (Blueprint $table) {
$table->id();
$table->foreignId('from_ticket_status_id')->constrained('ticket_statuses')->cascadeOnDelete();
$table->foreignId('to_ticket_status_id')->constrained('ticket_statuses')->cascadeOnDelete();
$table->timestamps();
$table->unique(
['from_ticket_status_id', 'to_ticket_status_id'],
'ticket_status_transitions_unique',
);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('ticket_status_transitions');
Schema::dropIfExists('ticket_statuses');
}
};
@@ -0,0 +1,133 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('tickets', function (Blueprint $table) {
$table->foreignId('ticket_status_id')
->nullable()
->after('board_column_id')
->constrained()
->nullOnDelete();
});
$this->backfillTicketStatuses();
Schema::table('tickets', function (Blueprint $table) {
$table->dropConstrainedForeignId('board_column_id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('tickets', function (Blueprint $table) {
$table->foreignId('board_column_id')
->nullable()
->after('author_user_id')
->constrained()
->nullOnDelete();
});
DB::table('tickets')
->leftJoin('ticket_statuses', 'tickets.ticket_status_id', '=', 'ticket_statuses.id')
->update([
'tickets.board_column_id' => DB::raw('ticket_statuses.board_column_id'),
]);
Schema::table('tickets', function (Blueprint $table) {
$table->dropConstrainedForeignId('ticket_status_id');
});
}
private function backfillTicketStatuses(): void
{
DB::table('projects')
->orderBy('id')
->get()
->each(function (object $project): void {
$backlogStatusId = $this->createTicketStatus(
(int) $project->id,
null,
'Backlog',
1,
);
DB::table('tickets')
->where('project_id', $project->id)
->whereNull('board_column_id')
->update(['ticket_status_id' => $backlogStatusId]);
DB::table('board_columns')
->where('project_id', $project->id)
->orderBy('position')
->get()
->each(function (object $boardColumn) use ($project): void {
$ticketStatusId = $this->createTicketStatus(
(int) $project->id,
(int) $boardColumn->id,
(string) $boardColumn->name,
1,
);
DB::table('tickets')
->where('project_id', $project->id)
->where('board_column_id', $boardColumn->id)
->update(['ticket_status_id' => $ticketStatusId]);
});
$this->createFullTransitionMatrix((int) $project->id);
});
}
private function createTicketStatus(
int $projectId,
?int $boardColumnId,
string $name,
int $position,
): int {
$now = now();
return (int) DB::table('ticket_statuses')->insertGetId([
'board_column_id' => $boardColumnId,
'created_at' => $now,
'name' => $name,
'position' => $position,
'project_id' => $projectId,
'slug' => Str::slug($name),
'updated_at' => $now,
]);
}
private function createFullTransitionMatrix(int $projectId): void
{
$now = now();
$statusIds = DB::table('ticket_statuses')
->where('project_id', $projectId)
->pluck('id')
->map(fn (int|string $statusId): int => (int) $statusId);
$statusIds->each(function (int $fromStatusId) use ($now, $statusIds): void {
$statusIds->each(function (int $toStatusId) use ($fromStatusId, $now): void {
DB::table('ticket_status_transitions')->insertOrIgnore([
'created_at' => $now,
'from_ticket_status_id' => $fromStatusId,
'to_ticket_status_id' => $toStatusId,
'updated_at' => $now,
]);
});
});
}
};