<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class FixProductionQueueProductForeignKey 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_product');
            } catch (\Throwable $e) {
            }

            $db->query('ALTER TABLE production_queue ADD CONSTRAINT fk_queue_product FOREIGN KEY (id_product) REFERENCES products(id_product) 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_product');
            } catch (\Throwable $e) {
            }
        }

        if ($db->tableExists('production_queue')) {
            $db->query('ALTER TABLE production_queue ADD CONSTRAINT fk_queue_product FOREIGN KEY (id_product) REFERENCES backup_product(id_product)');
        }
    }
}
