OEM lab refresh: fix id=0 seed issue (NO_AUTO_VALUE_ON_ZERO) and add one-shot refresh command

This commit is contained in:
DevSoftTech Server 2026-04-03 16:21:54 +05:30
parent 9d2f91e73a
commit 46a22f6e02
2 changed files with 61 additions and 2 deletions

View File

@ -0,0 +1,58 @@
<?php
namespace App\Commands;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
class RefreshLabTemplates extends BaseCommand
{
protected $group = 'OEM';
protected $name = 'oem:refresh-lab-templates';
protected $description = 'Rebuild pathology/radiology template tables from LabTemplateSeeder (without truncating hc_items).';
protected $usage = 'oem:refresh-lab-templates';
public function run(array $params): void
{
$db = \Config\Database::connect();
CLI::write('Refreshing lab template tables...', 'yellow');
CLI::write('This will truncate: lab_repotests, lab_tests_option, lab_tests, lab_repo, lab_rgroups, radiology_ultrasound_template', 'yellow');
CLI::write('hc_items will NOT be truncated.', 'yellow');
try {
$db->query('SET FOREIGN_KEY_CHECKS = 0');
// Order matters for dependent rows
$db->query('TRUNCATE TABLE lab_repotests');
$db->query('TRUNCATE TABLE lab_tests_option');
$db->query('TRUNCATE TABLE lab_tests');
$db->query('TRUNCATE TABLE lab_repo');
$db->query('TRUNCATE TABLE lab_rgroups');
$db->query('TRUNCATE TABLE radiology_ultrasound_template');
$db->query('SET FOREIGN_KEY_CHECKS = 1');
CLI::write('Template tables truncated. Running LabTemplateSeeder...', 'green');
$seeder = \Config\Database::seeder();
$seeder->call('App\\Database\\Seeds\\LabTemplateSeeder');
$cbc = $db->query("SELECT mstRepoKey, Title FROM lab_repo WHERE Title LIKE '%CBC%' LIMIT 1")->getRow();
if ($cbc) {
CLI::write('CBC template restored: #' . (string) $cbc->mstRepoKey . ' - ' . (string) $cbc->Title, 'green');
} else {
CLI::error('CBC template still not found after refresh. Check DB connection and seed files.');
}
CLI::write('Done.', 'green');
} catch (\Throwable $e) {
try {
$db->query('SET FOREIGN_KEY_CHECKS = 1');
} catch (\Throwable $ignored) {
}
CLI::error('Refresh failed: ' . $e->getMessage());
}
}
}

View File

@ -43,8 +43,9 @@ class LabTemplateSeeder extends Seeder
{
$sqlDir = __DIR__ . DIRECTORY_SEPARATOR . 'LabOemData' . DIRECTORY_SEPARATOR;
$this->db->query('SET @OLD_SQL_MODE = @@SESSION.sql_mode');
$this->db->query('SET FOREIGN_KEY_CHECKS = 0');
$this->db->query('SET SQL_MODE = ""'); // silence STRICT for legacy MyISAM data
$this->db->query('SET SESSION SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"');
foreach (self::SQL_FILES as $file) {
$path = $sqlDir . $file;
@ -80,7 +81,7 @@ class LabTemplateSeeder extends Seeder
}
$this->db->query('SET FOREIGN_KEY_CHECKS = 1');
$this->db->query('SET SQL_MODE = DEFAULT');
$this->db->query('SET SESSION SQL_MODE = @OLD_SQL_MODE');
CLI::write('Lab template seeder complete.', 'green');
}