<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class MakeTpIdNullableInModuls extends Migration
{
    public function up()
    {
        // Drop existing FK constraint first
        $this->forge->dropForeignKey('learning_device_moduls', 'learning_device_moduls_tp_id_foreign');

        // Modify tp_id to allow NULL
        $this->forge->modifyColumn('learning_device_moduls', [
            'tp_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
                'null'       => true,
            ],
        ]);
    }

    public function down()
    {
        // Revert to NOT NULL
        $this->forge->modifyColumn('learning_device_moduls', [
            'tp_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
                'null'       => false,
            ],
        ]);

        // Re-add FK constraint
        $this->forge->addForeignKey('tp_id', 'learning_device_tps', 'id', 'CASCADE', 'CASCADE', 'learning_device_moduls_tp_id_foreign');
    }
}
