<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class FixPrintRequestsForeignKeyAgain extends Migration
{
    public function up()
    {
        $db = \Config\Database::connect();
        $keys = $db->getForeignKeyData('print_requests');
        foreach ($keys as $key) {
            if ($key->constraint_name === 'print_requests_class_id_foreign') {
                return;
            }
        }

        $this->forge->addForeignKey('class_id', 'classSection', 'class_id', 'CASCADE', 'CASCADE');
        $this->forge->processIndexes('print_requests');
    }

    public function down()
    {
        $this->forge->dropForeignKey('print_requests', 'print_requests_class_id_foreign');
    }
}
