108 lines
3.3 KiB
PHP
108 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Database\Migrations;
|
|
|
|
use CodeIgniter\Database\Migration;
|
|
|
|
class UpdateTuitionFeeDefaults extends Migration
|
|
{
|
|
public function up()
|
|
{
|
|
if (!$this->db->tableExists('configuration')) {
|
|
return;
|
|
}
|
|
|
|
$this->forceConfig('first_student_fee', '380.00');
|
|
$this->forceConfig('second_student_fee', '280.00');
|
|
$this->forceConfig('new_tuition_full_amount', '380.00');
|
|
$this->forceConfig('new_tuition_second_student_discount', '100.00');
|
|
$this->aliasAndForceConfig('Youth_fee', 'youth_fee', '380.00');
|
|
}
|
|
|
|
public function down()
|
|
{
|
|
if (!$this->db->tableExists('configuration')) {
|
|
return;
|
|
}
|
|
|
|
$this->rollbackConfig('first_student_fee', '370.00');
|
|
$this->rollbackConfig('second_student_fee', '200.00');
|
|
$this->rollbackConfig('youth_fee', '200.00');
|
|
$this->rollbackConfig('new_tuition_full_amount', '370.00');
|
|
$this->rollbackConfig('new_tuition_second_student_discount', '50.00');
|
|
}
|
|
|
|
protected function aliasAndForceConfig(string $legacyKey, string $canonicalKey, string $value): void
|
|
{
|
|
$legacy = $this->db->table('configuration')
|
|
->select('id, config_value')
|
|
->where('config_key', $legacyKey)
|
|
->orderBy('id', 'ASC')
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if ($legacy && strcasecmp($legacyKey, $canonicalKey) !== 0) {
|
|
$this->db->table('configuration')
|
|
->where('id', (int) $legacy['id'])
|
|
->update(['config_key' => $canonicalKey, 'config_value' => $value]);
|
|
}
|
|
|
|
$this->forceConfig($canonicalKey, $value);
|
|
}
|
|
|
|
protected function forceConfig(string $key, string $value): void
|
|
{
|
|
$row = $this->db->table('configuration')
|
|
->select('id')
|
|
->where('config_key', $key)
|
|
->orderBy('id', 'ASC')
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if (!$row) {
|
|
$this->db->table('configuration')->insert([
|
|
'config_key' => $key,
|
|
'config_value' => $value,
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$this->db->table('configuration')
|
|
->where('config_key', $key)
|
|
->update(['config_value' => $value]);
|
|
}
|
|
|
|
protected function upsertConfig(string $key, string $value, array $legacyValues): void
|
|
{
|
|
$row = $this->db->table('configuration')
|
|
->select('id, config_value')
|
|
->where('config_key', $key)
|
|
->orderBy('id', 'ASC')
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if (!$row) {
|
|
$this->db->table('configuration')->insert([
|
|
'config_key' => $key,
|
|
'config_value' => $value,
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$current = trim((string) ($row['config_value'] ?? ''));
|
|
if (in_array($current, $legacyValues, true)) {
|
|
$this->db->table('configuration')
|
|
->where('config_key', $key)
|
|
->update(['config_value' => $value]);
|
|
}
|
|
}
|
|
|
|
protected function rollbackConfig(string $key, string $value): void
|
|
{
|
|
$this->db->table('configuration')
|
|
->where('config_key', $key)
|
|
->where('config_value', '380.00')
|
|
->update(['config_value' => $value]);
|
|
}
|
|
}
|