<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddClienteIdToEnvios extends Migration
{
    public function up()
    {
        $this->forge->addColumn('envios', [
            'cliente_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true, // IMPORTANTE
                'null'       => true,
                'after'      => 'id',
            ],
        ]);

        $this->db->query('ALTER TABLE envios ADD CONSTRAINT fk_envios_cliente FOREIGN KEY (cliente_id) REFERENCES clientes(id) ON DELETE SET NULL ON UPDATE CASCADE');
    }

    public function down()
    {
        $this->db->query('ALTER TABLE envios DROP FOREIGN KEY fk_envios_cliente');
        $this->forge->dropColumn('envios', 'cliente_id');
    }
}
