<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddParentBirthPlaceToStudentsTable extends Migration
{
    public function up()
    {
        if (!$this->db->fieldExists('father_birth_place', 'students')) {
            $this->forge->addColumn('students', [
                'father_birth_place' => [
                    'type'       => 'VARCHAR',
                    'constraint' => 100,
                    'null'       => true,
                    'default'    => null,
                    'after'      => 'father_nik'
                ],
            ]);
        }

        if (!$this->db->fieldExists('mother_birth_place', 'students')) {
            $this->forge->addColumn('students', [
                'mother_birth_place' => [
                    'type'       => 'VARCHAR',
                    'constraint' => 100,
                    'null'       => true,
                    'default'    => null,
                    'after'      => 'mother_nik'
                ],
            ]);
        }
    }

    public function down()
    {
        $fields = ['father_birth_place', 'mother_birth_place'];
        foreach ($fields as $field) {
            if ($this->db->fieldExists($field, 'students')) {
                $this->forge->dropColumn('students', $field);
            }
        }
    }
}
