<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * FinFlo references are stored in msisdn as 'finflo-TX-260301-XXXXXX' (23+ chars).
 * VARCHAR(20) was truncating them; increase to 80 for transactions and tips_unlock.
 */
class IncreaseMsisdnLengthForFinflo extends Migration
{
    public function up()
    {
        $this->db->query('ALTER TABLE transactions MODIFY COLUMN msisdn VARCHAR(80) NOT NULL');
        $this->db->query('ALTER TABLE tips_unlock MODIFY COLUMN msisdn VARCHAR(80) NOT NULL');

        // tip_sales may have msisdn; extend if it exists
        if ($this->db->fieldExists('msisdn', 'tip_sales')) {
            $this->db->query('ALTER TABLE tip_sales MODIFY COLUMN msisdn VARCHAR(80) NULL');
        }
    }

    public function down()
    {
        $this->db->query('ALTER TABLE transactions MODIFY COLUMN msisdn VARCHAR(20) NOT NULL');
        $this->db->query('ALTER TABLE tips_unlock MODIFY COLUMN msisdn VARCHAR(20) NOT NULL');
        if ($this->db->fieldExists('msisdn', 'tip_sales')) {
            $this->db->query('ALTER TABLE tip_sales MODIFY COLUMN msisdn VARCHAR(20) NULL');
        }
    }
}
