<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddCreatedByToInvoicesTable extends Migration
{
    public function up()
    {
        $this->forge->addColumn('invoices', [
            'created_by' => [
                'type' => 'BIGINT',
                'unsigned' => true,
                'null' => true,
                'after' => 'client_id',
            ],
        ]);

        $this->db->query(
            'ALTER TABLE invoices ADD CONSTRAINT invoices_created_by_foreign
             FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE'
        );
    }

    public function down()
    {
        $this->forge->dropForeignKey('invoices', 'invoices_created_by_foreign');
        $this->forge->dropColumn('invoices', 'created_by');
    }
}
