fix: fix project migrations

This commit is contained in:
Jeremy Munsch 2026-06-24 13:22:36 +02:00
parent 91f4773c60
commit 6854af708b
No known key found for this signature in database
GPG key ID: 7A502D8B9839C78C
11 changed files with 140 additions and 83 deletions

View file

@ -16,12 +16,12 @@ class CreateCreditLogsTable extends Migration
Schema::create('credit_logs', function (Blueprint $table) {
$table->id();
$table->unsignedTinyInteger('type')->comment('Enum Credit\\BalanceOperationEnum');
$table->unsignedFloat('amount')->comment('Montant de l\'opération');
$table->tinyInteger('type')->unsigned()->comment('Enum Credit\\BalanceOperationEnum');
$table->float('amount')->unsigned()->comment('Montant de l\'opération');
$table->foreignId('credit_account_id')
->constrained('credit_accounts')
->cascadeOnDelete();
->constrained('credit_accounts')
->cascadeOnDelete();
$table->timestamp('created_at');
});

View file

@ -18,24 +18,24 @@ class CreateCreditEstimationsTable extends Migration
Schema::create('credit_estimations', function (Blueprint $table) {
$table->id();
$table->unsignedTinyInteger('status')->default(EstimationStatusEnum::pending)
->comment('Enum Credit\\EstimationStatusEnum');
$table->tinyInteger('status')->unsigned()->default(EstimationStatusEnum::pending)
->comment('Enum Credit\\EstimationStatusEnum');
$table->unsignedFloat('amount')->comment('Estimation amount');
$table->float('amount')->unsigned()->comment('Estimation amount');
$table->foreignIdFor(Ticket::class)
->constrained();
$table->foreignId('credit_account_id')
->comment('The account to debit.')
->constrained('credit_accounts');
->comment('The account to debit.')
->constrained('credit_accounts');
$table->foreignId('estimator_id')
->comment('The person who has done the estimate.')
->constrained('users');
->comment('The person who has done the estimate.')
->constrained('users');
$table->foreignId('validator_id')
->nullable()
->comment('The person who has validate the estimate.')
->constrained('users');
->nullable()
->comment('The person who has validate the estimate.')
->constrained('users');
$table->timestamps();
});

View file

@ -1,6 +1,7 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ChangeAmoutFloatingPointOnCreditEstimations extends Migration
@ -12,8 +13,8 @@ class ChangeAmoutFloatingPointOnCreditEstimations extends Migration
*/
public function up()
{
Schema::table('credit_estimations', function ($table) {
$table->unsignedFloat('amount', 8, 3)
Schema::table('credit_estimations', function (Blueprint $table) {
$table->decimal('amount', 8, 3)->unsigned()
->comment('Estimation amount')->change();
});
}
@ -25,8 +26,8 @@ class ChangeAmoutFloatingPointOnCreditEstimations extends Migration
*/
public function down()
{
Schema::table('credit_estimations', function ($table) {
$table->unsignedFloat('amount')
Schema::table('credit_estimations', function (Blueprint $table) {
$table->float('amount')->unsigned()
->comment('Estimation amount')->change();
});
}

View file

@ -17,7 +17,7 @@ class CreatePackagesTable extends Migration
$table->id();
$table->string('name');
$table->unsignedFloat('amount')->comment('The cost per day');
$table->float('amount')->unsigned()->comment('The cost per day');
});
}

View file

@ -37,8 +37,8 @@ class CreateInvoicesTable extends Migration
'A JSON object, each line has to be contructed using these fields description, days, type Invoices\TaskTypeEnum, amount'
);
$table->unsignedFloat('total')->comment('The total calculated using all tasks');
$table->unsignedFloat('total_days')->comment('The total days calculated using all tasks');
$table->float('total')->unsigned()->comment('The total calculated using all tasks');
$table->float('total_days')->unsigned()->comment('The total days calculated using all tasks');
$table->text('comment')->nullable()
->comment('Any comment joined to the invoice');

View file

@ -1,6 +1,6 @@
<?php
use App\Models\Ticket;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Migrations\Migration;
class ChangeTicketsStatus extends Migration
@ -12,17 +12,22 @@ class ChangeTicketsStatus extends Migration
*/
public function up()
{
Ticket::whereIn('status', [4, 5, 7, 10])
DB::table('tickets')->whereIn('status', [4, 5, 7, 10])
->update(['status' => 3]);
Ticket::where('status', 9)
DB::table('tickets')->where('status', 9)
->update(['status' => 4]);
Ticket::where('status', 17)
DB::table('tickets')->where('status', 17)
->update(['status' => 5]);
Ticket::where('status', 12)
DB::table('tickets')->where('status', 12)
->update(['status' => 6]);
Ticket::where('status', 13)
DB::table('tickets')->where('status', 13)
->update(['status' => 7]);
Ticket::where('status', 15)
DB::table('tickets')->where('status', 15)
->update(['status' => 8]);
}
@ -33,17 +38,22 @@ class ChangeTicketsStatus extends Migration
*/
public function down()
{
Ticket::where('status', 3)
DB::table('tickets')->where('status', 3)
->update(['status' => 10]);
Ticket::where('status', 4)
DB::table('tickets')->where('status', 4)
->update(['status' => 9]);
Ticket::where('status', 5)
DB::table('tickets')->where('status', 5)
->update(['status' => 17]);
Ticket::where('status', 6)
DB::table('tickets')->where('status', 6)
->update(['status' => 12]);
Ticket::where('status', 7)
DB::table('tickets')->where('status', 7)
->update(['status' => 13]);
Ticket::where('status', 8)
DB::table('tickets')->where('status', 8)
->update(['status' => 15]);
}
}

View file

@ -1,9 +1,8 @@
<?php
use App\Models\Ticket;
use Carbon\Carbon;
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
@ -17,15 +16,32 @@ return new class extends Migration
{
Schema::table('tickets', function (Blueprint $table) {
$table->timestamp('last_activity')
->default(Carbon::now())
->useCurrent()
->comment('Last activity on the ticket.')
->after('is_internal');
});
Ticket::get()->map(function (object $ticket) {
$lastMessage = $ticket->messages->whereNotNull('user_id')->sortByDesc('updated_at')->first();
$ticket->last_activity =
$lastMessage->updated_at ?? $ticket->updated_at;
$ticket->saveQuietly();
DB::table('tickets')->orderBy('id')->chunk(500, function ($tickets) {
$ticketIds = $tickets->pluck('id')->toArray();
$messages = DB::table('messages')
->whereIn('ticket_id', $ticketIds)
->whereNotNull('user_id')
->orderBy('updated_at', 'desc')
->get()
->groupBy('ticket_id');
foreach ($tickets as $ticket) {
$lastMessageDate = $messages->has($ticket->id)
? $messages->get($ticket->id)->first()->updated_at
: null;
DB::table('tickets')
->where('id', $ticket->id)
->update([
'last_activity' => $lastMessageDate ?? $ticket->updated_at
]);
}
});
}

View file

@ -1,10 +1,9 @@
<?php
use App\Models\Ticket;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
@ -22,11 +21,15 @@ return new class extends Migration
});
DB::transaction(function () {
$tickets = Ticket::all();
foreach ($tickets as $ticket) {
$ticket->title = 'ts-' . $ticket->id;
$ticket->saveQuietly();
}
DB::table('tickets')->orderBy('id')->chunkById(1000, function ($tickets) {
foreach ($tickets as $ticket) {
DB::table('tickets')
->where('id', $ticket->id)
->update([
'title' => 'ts-' . $ticket->id
]);
}
});
});
}

View file

@ -1,8 +1,7 @@
<?php
use App\Models\Ticket;
use App\Models\Ticket\TicketType;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
@ -13,13 +12,19 @@ return new class extends Migration
*/
public function up(): void
{
Ticket::query()->get()->map(function (Ticket $model) {
$model->type = $model->getRawOriginal('type') + 1;
$model->saveQuietly();
});
TicketType::query()->orderBy('type', 'DESC')->get()->map(function (TicketType $model) {
$model->type = $model->getRawOriginal('type') + 1;
$model->saveQuietly();
DB::transaction(function () {
DB::table('tickets')->orderBy('id')->chunkById(1000, function ($tickets) {
$ids = $tickets->pluck('id')->toArray();
DB::table('tickets')->whereIn('id', $ids)->increment('type');
});
$ticketTypes = DB::table('ticket_types')->orderBy('type', 'desc')->get();
foreach ($ticketTypes as $type) {
DB::table('ticket_types')
->where('id', $type->id)
->update(['type' => $type->type + 1]);
}
});
}
@ -30,13 +35,20 @@ return new class extends Migration
*/
public function down(): void
{
Ticket::query()->get()->map(function (Ticket $model) {
$model->type = $model->getRawOriginal('type') - 1;
$model->saveQuietly();
});
TicketType::query()->orderBy('type', 'DESC')->get()->map(function (TicketType $model) {
$model->type = $model->getRawOriginal('type') - 1;
$model->saveQuietly();
DB::transaction(function () {
// 1. Restauration des tickets
DB::table('tickets')->orderBy('id')->chunkById(1000, function ($tickets) {
$ids = $tickets->pluck('id')->toArray();
DB::table('tickets')->whereIn('id', $ids)->decrement('type');
});
$ticketTypes = DB::table('ticket_types')->orderBy('type', 'asc')->get();
foreach ($ticketTypes as $type) {
DB::table('ticket_types')
->where('id', $type->id)
->update(['type' => $type->type - 1]);
}
});
}
};

View file

@ -1,7 +1,7 @@
<?php
use App\Models\Ticket;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
@ -12,10 +12,9 @@ return new class extends Migration
*/
public function up(): void
{
Ticket::query()->where('status', '>', 6)->get()->map(function (Ticket $model) {
$model->status = $model->getRawOriginal('status') + 1;
$model->saveQuietly();
});
DB::table('tickets')
->where('status', '>', 6)
->increment('status');
}
/**
@ -25,9 +24,8 @@ return new class extends Migration
*/
public function down(): void
{
Ticket::query()->where('status', '>', 6)->get()->map(function (Ticket $model) {
$model->status = $model->getRawOriginal('status') - 1;
$model->saveQuietly();
});
DB::table('tickets')
->where('status', '>', 6)
->decrement('status');
}
};

View file

@ -1,9 +1,9 @@
<?php
use Illuminate\Support\Facades\Schema;
use App\Models\Site;
use Illuminate\Database\Schema\Blueprint;
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
{
@ -15,14 +15,31 @@ return new class extends Migration
public function up(): void
{
Schema::table('tickets', function (Blueprint $table) {
$table->integer('order')->after('status')->comment('Ticket order.');
$table->integer('order')->default(0)->after('status')->comment('Ticket order.');
});
Site::query()->get()->map(function ($site) {
$site->tickets->map(function ($ticket, $ticketIndex) {
$ticket->order = $ticketIndex + 1;
$ticket->saveQuietly();
});
DB::transaction(function () {
$currentSiteId = null;
$orderCounter = 1;
DB::table('tickets')
->orderBy('site_id')
->orderBy('id')
->chunk(1000, function ($tickets) use (&$currentSiteId, &$orderCounter) {
foreach ($tickets as $ticket) {
if ($ticket->site_id !== $currentSiteId) {
$currentSiteId = $ticket->site_id;
$orderCounter = 1;
}
DB::table('tickets')
->where('id', $ticket->id)
->update(['order' => $orderCounter]);
$orderCounter++;
}
});
});
}