<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddGradeIdToAssessments extends Migration
{
    public function up()
    {
        if (!$this->db->fieldExists('grade_id', 'assessments')) {
            $this->forge->addColumn('assessments', [
                'grade_id' => [
                    'type' => 'BIGINT',
                    'unsigned' => true,
                    'null' => true,
                    'after' => 'classroom_id'
                ],
            ]);
            $this->forge->addForeignKey('grade_id', 'grades', 'id', 'SET NULL', 'CASCADE');
        }
    }

    public function down()
    {
        if ($this->db->fieldExists('grade_id', 'assessments')) {
            $this->forge->dropForeignKey('assessments', 'assessments_grade_id_foreign');
            $this->forge->dropColumn('assessments', 'grade_id');
        }
    }
}