50 lines
1.7 KiB
PHP
50 lines
1.7 KiB
PHP
<?php
|
|
|
|
use Illuminate\Database\Migrations\Migration;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
return new class extends Migration
|
|
{
|
|
/**
|
|
* Run the migrations.
|
|
*/
|
|
public function up(): void
|
|
{
|
|
// Clean up existing data and update status enum without touching old migrations
|
|
|
|
// First backup existing data (optional)
|
|
// DB::statement("CREATE TABLE mutations_backup AS SELECT * FROM mutations");
|
|
|
|
// Update existing 'pending' status to 'sent'
|
|
DB::table('mutations')
|
|
->where('status', 'pending')
|
|
->update(['status' => 'sent']);
|
|
|
|
// Update existing 'completed' status to 'approved'
|
|
DB::table('mutations')
|
|
->where('status', 'completed')
|
|
->update(['status' => 'approved']);
|
|
|
|
// Update the enum to only include valid statuses
|
|
DB::statement("ALTER TABLE mutations MODIFY COLUMN status ENUM('sent', 'received', 'approved', 'rejected', 'cancelled') DEFAULT 'sent'");
|
|
|
|
// Update any orphaned mutation_details that reference completed status
|
|
DB::statement("
|
|
UPDATE mutation_details md
|
|
JOIN mutations m ON md.mutation_id = m.id
|
|
SET md.quantity_approved = md.quantity_requested
|
|
WHERE m.status = 'approved' AND md.quantity_approved IS NULL
|
|
");
|
|
}
|
|
|
|
/**
|
|
* Reverse the migrations.
|
|
*/
|
|
public function down(): void
|
|
{
|
|
// Restore original enum (if needed)
|
|
DB::statement("ALTER TABLE mutations MODIFY COLUMN status ENUM('pending', 'sent', 'received', 'approved', 'rejected', 'completed', 'cancelled') DEFAULT 'sent'");
|
|
}
|
|
};
|