<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddPurposeToMemberInvitations extends Migration
{
    public function up()
    {
        if ($this->db->tableExists('member_invitations') && !$this->db->fieldExists('purpose', 'member_invitations')) {
            $this->forge->addColumn('member_invitations', [
                'purpose' => [
                    'type' => 'VARCHAR',
                    'constraint' => 50,
                    'null' => false,
                    'default' => 'register',
                ],
            ]);

            $this->db->table('member_invitations')
                ->where('purpose', null)
                ->set(['purpose' => 'register'])
                ->update();
        }
    }

    public function down()
    {
        if ($this->db->tableExists('member_invitations') && $this->db->fieldExists('purpose', 'member_invitations')) {
            $this->forge->dropColumn('member_invitations', 'purpose');
        }
    }
}
