<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class ProtectInvoiceRelationships extends Migration
{
    public function up()
    {
        $this->db->query(
            "ALTER TABLE invoices
                DROP FOREIGN KEY invoices_client_id_foreign,
                DROP FOREIGN KEY invoices_quotation_id_foreign",
        );
        $this->db->query(
            "ALTER TABLE invoices
                ADD CONSTRAINT invoices_client_id_foreign
                    FOREIGN KEY (client_id) REFERENCES clients(id)
                    ON UPDATE CASCADE ON DELETE RESTRICT,
                ADD CONSTRAINT invoices_quotation_id_foreign
                    FOREIGN KEY (quotation_id) REFERENCES quotations(id)
                    ON UPDATE CASCADE ON DELETE RESTRICT",
        );
    }

    public function down()
    {
        $this->db->query(
            "ALTER TABLE invoices
                DROP FOREIGN KEY invoices_client_id_foreign,
                DROP FOREIGN KEY invoices_quotation_id_foreign",
        );
        $this->db->query(
            "ALTER TABLE invoices
                ADD CONSTRAINT invoices_client_id_foreign
                    FOREIGN KEY (client_id) REFERENCES clients(id)
                    ON UPDATE RESTRICT ON DELETE CASCADE,
                ADD CONSTRAINT invoices_quotation_id_foreign
                    FOREIGN KEY (quotation_id) REFERENCES quotations(id)
                    ON UPDATE RESTRICT ON DELETE CASCADE",
        );
    }
}
