<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class UpdateHistoryPengajuansUserIdForeign extends Migration
{
    public function up()
    {
        // 1. Drop foreign key lama
        DB::statement('ALTER TABLE history_pengajuans DROP FOREIGN KEY history_pengajuans_user_id_foreign');

        // 2. Ubah kolom jadi nullable
        DB::statement('ALTER TABLE history_pengajuans MODIFY COLUMN user_id BIGINT UNSIGNED NULL');

        // 3. Tambah foreign key baru dengan ON DELETE SET NULL
        DB::statement('ALTER TABLE history_pengajuans ADD CONSTRAINT history_pengajuans_user_id_foreign 
                        FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL');
    }

    public function down()
    {
        DB::statement('ALTER TABLE history_pengajuans DROP FOREIGN KEY history_pengajuans_user_id_foreign');
        DB::statement('ALTER TABLE history_pengajuans MODIFY COLUMN user_id BIGINT UNSIGNED NOT NULL');
        DB::statement('ALTER TABLE history_pengajuans ADD CONSTRAINT history_pengajuans_user_id_foreign 
                        FOREIGN KEY (user_id) REFERENCES users(id)');
    }
}