From 6854af708b84cedd34fe82da908c82ff5bef5289 Mon Sep 17 00:00:00 2001 From: Jeremy Munsch Date: Wed, 24 Jun 2026 13:22:36 +0200 Subject: [PATCH] fix: fix project migrations --- ..._05_30_091412_create_credit_logs_table.php | 8 ++-- ...091438_create_credit_estimations_table.php | 20 ++++----- ...t_floating_point_on_credit_estimations.php | 9 ++-- ...022_08_22_154457_create_packages_table.php | 2 +- ...022_08_23_085833_create_invoices_table.php | 4 +- ...023_04_06_101917_change_tickets_status.php | 36 +++++++++------ ...142748_add_last_activity_tickets_table.php | 32 ++++++++++---- ...1_13_144755_add_title_to_tickets_table.php | 17 ++++--- .../2025_02_14_122301_rebase_tickets_type.php | 44 ++++++++++++------- ...025_02_19_154440_rebase_tickets_status.php | 16 +++---- ...5_03_12_102925_change_order_to_tickets.php | 35 +++++++++++---- 11 files changed, 140 insertions(+), 83 deletions(-) diff --git a/database/migrations/2022_05_30_091412_create_credit_logs_table.php b/database/migrations/2022_05_30_091412_create_credit_logs_table.php index 3caeb08..465bfec 100644 --- a/database/migrations/2022_05_30_091412_create_credit_logs_table.php +++ b/database/migrations/2022_05_30_091412_create_credit_logs_table.php @@ -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'); }); diff --git a/database/migrations/2022_05_30_091438_create_credit_estimations_table.php b/database/migrations/2022_05_30_091438_create_credit_estimations_table.php index 6100458..7ac4bf0 100644 --- a/database/migrations/2022_05_30_091438_create_credit_estimations_table.php +++ b/database/migrations/2022_05_30_091438_create_credit_estimations_table.php @@ -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(); }); diff --git a/database/migrations/2022_06_15_182442_change_amout_floating_point_on_credit_estimations.php b/database/migrations/2022_06_15_182442_change_amout_floating_point_on_credit_estimations.php index e60f793..d5131b2 100644 --- a/database/migrations/2022_06_15_182442_change_amout_floating_point_on_credit_estimations.php +++ b/database/migrations/2022_06_15_182442_change_amout_floating_point_on_credit_estimations.php @@ -1,6 +1,7 @@ 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(); }); } diff --git a/database/migrations/2022_08_22_154457_create_packages_table.php b/database/migrations/2022_08_22_154457_create_packages_table.php index 65d1bbb..bc6de72 100644 --- a/database/migrations/2022_08_22_154457_create_packages_table.php +++ b/database/migrations/2022_08_22_154457_create_packages_table.php @@ -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'); }); } diff --git a/database/migrations/2022_08_23_085833_create_invoices_table.php b/database/migrations/2022_08_23_085833_create_invoices_table.php index c079c23..e40dc8d 100644 --- a/database/migrations/2022_08_23_085833_create_invoices_table.php +++ b/database/migrations/2022_08_23_085833_create_invoices_table.php @@ -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'); diff --git a/database/migrations/2023_04_06_101917_change_tickets_status.php b/database/migrations/2023_04_06_101917_change_tickets_status.php index 5b2aa0d..9fa03d3 100644 --- a/database/migrations/2023_04_06_101917_change_tickets_status.php +++ b/database/migrations/2023_04_06_101917_change_tickets_status.php @@ -1,6 +1,6 @@ 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]); } } diff --git a/database/migrations/2023_08_14_142748_add_last_activity_tickets_table.php b/database/migrations/2023_08_14_142748_add_last_activity_tickets_table.php index e4b1128..589bea8 100644 --- a/database/migrations/2023_08_14_142748_add_last_activity_tickets_table.php +++ b/database/migrations/2023_08_14_142748_add_last_activity_tickets_table.php @@ -1,9 +1,8 @@ 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 + ]); + } }); } diff --git a/database/migrations/2023_11_13_144755_add_title_to_tickets_table.php b/database/migrations/2023_11_13_144755_add_title_to_tickets_table.php index 548d20d..7e66081 100644 --- a/database/migrations/2023_11_13_144755_add_title_to_tickets_table.php +++ b/database/migrations/2023_11_13_144755_add_title_to_tickets_table.php @@ -1,10 +1,9 @@ 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 + ]); + } + }); }); } diff --git a/database/migrations/2025_02_14_122301_rebase_tickets_type.php b/database/migrations/2025_02_14_122301_rebase_tickets_type.php index 6c8fcf1..1a1408a 100644 --- a/database/migrations/2025_02_14_122301_rebase_tickets_type.php +++ b/database/migrations/2025_02_14_122301_rebase_tickets_type.php @@ -1,8 +1,7 @@ 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]); + } }); } }; diff --git a/database/migrations/2025_02_19_154440_rebase_tickets_status.php b/database/migrations/2025_02_19_154440_rebase_tickets_status.php index cc26af3..f5753c9 100644 --- a/database/migrations/2025_02_19_154440_rebase_tickets_status.php +++ b/database/migrations/2025_02_19_154440_rebase_tickets_status.php @@ -1,7 +1,7 @@ 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'); } }; diff --git a/database/migrations/2025_03_12_102925_change_order_to_tickets.php b/database/migrations/2025_03_12_102925_change_order_to_tickets.php index 3483b0a..443da3b 100644 --- a/database/migrations/2025_03_12_102925_change_order_to_tickets.php +++ b/database/migrations/2025_03_12_102925_change_order_to_tickets.php @@ -1,9 +1,9 @@ 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++; + } + }); }); }