<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class FixProductionQueueForeignKey extends Migration
{
    public function up()
    {
        $db = \Config\Database::connect();

        if ($db->tableExists('production_queue')) {
            try {
                $db->query('ALTER TABLE production_queue DROP FOREIGN KEY fk_queue_order');
            } catch (\Throwable $e) {
            }

            $db->query('ALTER TABLE production_queue ADD CONSTRAINT fk_queue_order FOREIGN KEY (id_order) REFERENCES orders(id_order) ON DELETE CASCADE');
        }
    }

    public function down()
    {
        $db = \Config\Database::connect();

        if ($db->tableExists('production_queue')) {
            try {
                $db->query('ALTER TABLE production_queue DROP FOREIGN KEY fk_queue_order');
            } catch (\Throwable $e) {
            }
        }

        if ($db->tableExists('production_queue')) {
            $db->query('ALTER TABLE production_queue ADD CONSTRAINT fk_queue_order FOREIGN KEY (id_order) REFERENCES lama_orders(id_order) ON DELETE CASCADE');
        }
    }
}
