<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AllowStandaloneInvoices extends Migration
{
    public function up()
    {
        $this->db->query(
            "ALTER TABLE invoices
                DROP FOREIGN KEY invoices_quotation_id_foreign,
                MODIFY quotation_id INT UNSIGNED NULL",
        );
        $this->db->query(
            "ALTER TABLE invoices
                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_quotation_id_foreign,
                MODIFY quotation_id INT UNSIGNED NOT NULL",
        );
        $this->db->query(
            "ALTER TABLE invoices
                ADD CONSTRAINT invoices_quotation_id_foreign
                    FOREIGN KEY (quotation_id) REFERENCES quotations(id)
                    ON UPDATE CASCADE ON DELETE RESTRICT",
        );
    }
}
