dbms_init...1

This commit is contained in:
choi.jh 2025-07-11 17:07:12 +09:00
parent b8ca0bcda7
commit c5a6349a1d
6 changed files with 164 additions and 127 deletions

View File

@ -7,19 +7,27 @@ use CodeIgniter\Database\BaseConnection;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Libraries\DBMigration\Process\ClientProcess;
use App\Libraries\DBMigration\Process\LineProcess;
use App\Libraries\DBMigration\Process\ServerCodeProcess;
use App\Libraries\DBMigration\Process\SwitchCodeProcess;
use App\Libraries\DBMigration\Process\MigrationProcessInterface;
abstract class DBMigration extends BaseController
{
private $_targetDB = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
}
abstract protected function getSourceDB(): BaseConnection;
abstract protected function getClient(): array;
abstract protected function getLine(): array;
abstract protected function getServerCode(): array;
abstract protected function getSwitchCode(): array;
final protected function getTargetDB(): BaseConnection
{
if ($this->_targetDB === null) {
@ -28,150 +36,59 @@ abstract class DBMigration extends BaseController
return $this->_targetDB;
}
protected function client_process(array $row, int $cnt): void
/**
* 공통 마이그레이션 처리 로직
*/
private function processMigration(MigrationProcessInterface $processor, array $rows, string $functionName): void
{
$temps = [];
$temps['code'] = $row['Client_Code'];
$temps['role'] = $row['Client_Reseller'] == 30 ? "reseller" : "user";
$temps['name'] = $row['Client_Name'];
$temps['phone'] = empty($row['Client_Phone1']) ? "" : $row['Client_Phone1'];
if (empty($temps['phone'])) {
$temps['phone'] = empty($row['Client_Phone2']) ? "" : $row['Client_Phone2'];
$this->getTargetDB()->transStart();
try {
$cnt = 1;
foreach ($rows as $row) {
$processor->process($row, $cnt);
$cnt++;
}
echo "{$functionName} 작업을 완료하였습니다.";
$this->getTargetDB()->transCommit();
} catch (\Exception $e) {
$this->getTargetDB()->transRollback();
echo "{$functionName} 작업을 실패하였습니다.\n--------------\n" . $e->getMessage() . "\n--------------\n";
}
$temps['email'] = empty($row['Client_EMail1']) ? "" : $row['Client_EMail1'];
if (empty($temps['email'])) {
$temps['email'] = empty($row['Client_EMail2']) ? "" : $row['Client_EMail2'];
}
$temps['account_balance'] = intval($row['Client_Money']);
$temps['coupon_balance'] = 0;
$temps['point_balance'] = 0;
$temps['status'] = 'default';
$temps['updated_at'] = empty($row['Client_Renew_date']) ? NULL : $row['Client_Renew_date'];;
if (!empty($row['Client_Receive_date'])) {
$temps['created_at'] = $row['Client_Receive_date'];;
}
//신규DB에 삽입
if (!$this->getTargetDB()->table('clientinfo')->insert($temps)) {
throw new \Exception($this->getTargetDB()->error()['message']);
}
echo "{$cnt} -> {$temps['name']} 고객완료.\n";
}
final public function client(): void
{
//Transaction Start
$this->getTargetDB()->transStart();
try {
$cnt = 1;
$rows = $this->getClient();
foreach ($rows as $row) {
$this->client_process($row, $cnt);
$cnt++;
}
echo __FUNCTION__ . " 작업을 완료하였습니다.";
$this->getTargetDB()->transCommit();
} catch (\Exception $e) {
//Transaction Rollback
$this->getTargetDB()->transRollback();
echo __FUNCTION__ . " 작업을 실패하였습니다.\n--------------\n" . $e->getMessage() . "\n--------------\n";
}
}
protected function line_process(array $row, int $cnt): void
{
$temps = [];
//신규DB에 삽입
if (!$this->getTargetDB()->table('lineinfo')->insert($temps)) {
throw new \Exception($this->getTargetDB()->error()['message']);
}
echo "{$cnt} -> {$temps['name']} Line완료.\n";
$this->processMigration(
new ClientProcess($this->getTargetDB()),
$this->getClient(),
__FUNCTION__
);
}
final public function line(): void
{
//Transaction Start
$this->getTargetDB()->transStart();
try {
$cnt = 1;
$rows = $this->getLine();
foreach ($rows as $row) {
$this->line_process($row, $cnt);
$cnt++;
}
echo __FUNCTION__ . " 작업을 완료하였습니다.";
$this->getTargetDB()->transCommit();
} catch (\Exception $e) {
//Transaction Rollback
$this->getTargetDB()->transRollback();
echo __FUNCTION__ . " 작업을 실패하였습니다.\n--------------\n" . $e->getMessage() . "\n--------------\n";
}
}
protected function servercode_process(array $row, int $cnt): void
{
$temps = [];
$temps['code'] = trim($row['server_code']);
$temps['status'] = 'default';
//신규DB에 삽입
if (!$this->getTargetDB()->table('codeinfo')->insert($temps)) {
throw new \Exception($this->getTargetDB()->error()['message']);
}
echo "{$cnt} -> {$temps['code']} SERVERCODE 완료.\n";
$this->processMigration(
new LineProcess($this->getTargetDB()),
$this->getLine(),
__FUNCTION__
);
}
final public function servercode(): void
{
//Transaction Start
$this->getTargetDB()->transStart();
try {
$cnt = 1;
$rows = $this->getServerCode();
// var_dump($rows);
// exit;
foreach ($rows as $row) {
$this->servercode_process($row, $cnt);
$cnt++;
}
echo __FUNCTION__ . " 작업을 완료하였습니다.";
$this->getTargetDB()->transCommit();
} catch (\Exception $e) {
//Transaction Rollback
$this->getTargetDB()->transRollback();
echo __FUNCTION__ . " 작업을 실패하였습니다.\n--------------\n" . $e->getMessage() . "\n--------------\n";
}
}
protected function switchcode_process(array $row, int $cnt): void
{
$temps = [];
$temps['code'] = trim($row['service_sw']);
$temps['status'] = 'default';
//신규DB에 삽입
if (!$this->getTargetDB()->table('switchinfo')->insert($temps)) {
throw new \Exception($this->getTargetDB()->error()['message']);
}
echo "{$cnt} -> {$temps['code']} SWITCHCODE 완료.\n";
$this->processMigration(
new ServerCodeProcess($this->getTargetDB()),
$this->getServerCode(),
__FUNCTION__
);
}
final public function switchcode(): void
{
//Transaction Start
$this->getTargetDB()->transStart();
try {
$cnt = 1;
$rows = $this->getSwitchCode();
// var_dump($rows);
// exit;
foreach ($rows as $row) {
$this->switchcode_process($row, $cnt);
$cnt++;
}
echo __FUNCTION__ . " 작업을 완료하였습니다.";
$this->getTargetDB()->transCommit();
} catch (\Exception $e) {
//Transaction Rollback
$this->getTargetDB()->transRollback();
echo __FUNCTION__ . " 작업을 실패하였습니다.\n--------------\n" . $e->getMessage() . "\n--------------\n";
}
$this->processMigration(
new SwitchCodeProcess($this->getTargetDB()),
$this->getSwitchCode(),
__FUNCTION__
);
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace App\Libraries\DBMigration\Process;
use CodeIgniter\Database\BaseConnection;
class ClientProcess implements MigrationProcessInterface
{
private $db;
public function __construct(BaseConnection $db)
{
$this->db = $db;
}
public function process(array $row, int $cnt): void
{
$temps = [];
$temps['code'] = $row['Client_Code'];
$temps['role'] = $row['Client_Reseller'] == 30 ? "reseller" : "user";
$temps['name'] = $row['Client_Name'];
$temps['phone'] = empty($row['Client_Phone1']) ? "" : $row['Client_Phone1'];
if (empty($temps['phone'])) {
$temps['phone'] = empty($row['Client_Phone2']) ? "" : $row['Client_Phone2'];
}
$temps['email'] = empty($row['Client_EMail1']) ? "" : $row['Client_EMail1'];
if (empty($temps['email'])) {
$temps['email'] = empty($row['Client_EMail2']) ? "" : $row['Client_EMail2'];
}
$temps['account_balance'] = intval($row['Client_Money']);
$temps['coupon_balance'] = 0;
$temps['point_balance'] = 0;
$temps['status'] = 'default';
$temps['updated_at'] = empty($row['Client_Renew_date']) ? NULL : $row['Client_Renew_date'];
if (!empty($row['Client_Receive_date'])) {
$temps['created_at'] = $row['Client_Receive_date'];
}
if (!$this->db->table('clientinfo')->insert($temps)) {
throw new \Exception($this->db->error()['message']);
}
echo "{$cnt} -> {$temps['name']} 고객완료.\n";
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Libraries\DBMigration\Process;
use CodeIgniter\Database\BaseConnection;
class LineProcess implements MigrationProcessInterface
{
private $db;
public function __construct(BaseConnection $db)
{
$this->db = $db;
}
public function process(array $row, int $cnt): void
{
$temps = [];
// TODO: $temps에 필요한 데이터 매핑 추가
if (!$this->db->table('lineinfo')->insert($temps)) {
throw new \Exception($this->db->error()['message']);
}
echo "{$cnt} -> {$temps['name']} Line완료.\n";
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace App\Libraries\DBMigration\Process;
interface MigrationProcessInterface
{
public function process(array $row, int $cnt): void;
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Libraries\DBMigration\Process;
use CodeIgniter\Database\BaseConnection;
class ServerCodeProcess implements MigrationProcessInterface
{
private $db;
public function __construct(BaseConnection $db)
{
$this->db = $db;
}
public function process(array $row, int $cnt): void
{
$temps = [];
$temps['code'] = trim($row['server_code']);
$temps['status'] = 'default';
if (!$this->db->table('codeinfo')->insert($temps)) {
throw new \Exception($this->db->error()['message']);
}
echo "{$cnt} -> {$temps['code']} SERVERCODE 완료.\n";
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Libraries\DBMigration\Process;
use CodeIgniter\Database\BaseConnection;
class SwitchCodeProcess implements MigrationProcessInterface
{
private $db;
public function __construct(BaseConnection $db)
{
$this->db = $db;
}
public function process(array $row, int $cnt): void
{
$temps = [];
$temps['code'] = trim($row['service_sw']);
$temps['status'] = 'default';
if (!$this->db->table('switchinfo')->insert($temps)) {
throw new \Exception($this->db->error()['message']);
}
echo "{$cnt} -> {$temps['code']} SWITCHCODE 완료.\n";
}
}