<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class FixForeignKeyCascade extends Migration
{
    public function up()
    {
        // Hapus foreign key lama yang pakai CASCADE pada detail_peminjaman
        $this->forge->dropForeignKey('detail_peminjaman', 'detail_peminjaman_buku_id_foreign');
        $this->forge->dropForeignKey('detail_peminjaman', 'detail_peminjaman_peminjaman_id_foreign');

        // Tambah ulang dengan NO ACTION agar data laporan tidak ikut terhapus
        $this->forge->addForeignKey(
            'peminjaman_id',
            'peminjaman',
            'id',
            'NO ACTION',
            'NO ACTION'
        );

        $this->forge->addForeignKey(
            'buku_id',
            'buku',
            'id',
            'NO ACTION',
            'NO ACTION'
        );
    }

    public function down()
    {
        // Kembalikan ke CASCADE jika rollback
        $this->forge->dropForeignKey('detail_peminjaman', 'detail_peminjaman_buku_id_foreign');
        $this->forge->dropForeignKey('detail_peminjaman', 'detail_peminjaman_peminjaman_id_foreign');

        $this->forge->addForeignKey(
            'peminjaman_id',
            'peminjaman',
            'id',
            'CASCADE',
            'CASCADE'
        );

        $this->forge->addForeignKey(
            'buku_id',
            'buku',
            'id',
            'CASCADE',
            'CASCADE'
        );
    }
}

