diff --git a/app/Controllers/CLI/DBMigration/DBMigration.php b/app/Controllers/CLI/DBMigration/DBMigration.php index da47c79..ed3ea43 100644 --- a/app/Controllers/CLI/DBMigration/DBMigration.php +++ b/app/Controllers/CLI/DBMigration/DBMigration.php @@ -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__ + ); } } diff --git a/app/Libraries/DBMigration/Process/ClientProcess.php b/app/Libraries/DBMigration/Process/ClientProcess.php new file mode 100644 index 0000000..6a1d218 --- /dev/null +++ b/app/Libraries/DBMigration/Process/ClientProcess.php @@ -0,0 +1,41 @@ +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"; + } +} diff --git a/app/Libraries/DBMigration/Process/LineProcess.php b/app/Libraries/DBMigration/Process/LineProcess.php new file mode 100644 index 0000000..9392100 --- /dev/null +++ b/app/Libraries/DBMigration/Process/LineProcess.php @@ -0,0 +1,23 @@ +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"; + } +} diff --git a/app/Libraries/DBMigration/Process/MigrationProcessInterface.php b/app/Libraries/DBMigration/Process/MigrationProcessInterface.php new file mode 100644 index 0000000..5ec7809 --- /dev/null +++ b/app/Libraries/DBMigration/Process/MigrationProcessInterface.php @@ -0,0 +1,8 @@ +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"; + } +} diff --git a/app/Libraries/DBMigration/Process/SwitchCodeProcess.php b/app/Libraries/DBMigration/Process/SwitchCodeProcess.php new file mode 100644 index 0000000..8046654 --- /dev/null +++ b/app/Libraries/DBMigration/Process/SwitchCodeProcess.php @@ -0,0 +1,24 @@ +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"; + } +}