From 756d08be09b284c66edc0eb2f429c5c359eaaba8 Mon Sep 17 00:00:00 2001 From: kent Date: Sun, 21 Dec 2025 16:05:54 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20clients=20=ED=85=8C=EC=9D=B4=EB=B8=94=20?= =?UTF-8?q?ENUM=E2=86=92VARCHAR=20=EC=A0=95=EA=B7=9C=ED=99=94=20=EB=B0=8F?= =?UTF-8?q?=20common=5Fcodes=20=EC=97=B0=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - client_type, bad_debt_progress 컬럼을 ENUM에서 VARCHAR로 변경 - 기존 한글값을 common_codes의 code값으로 데이터 마이그레이션 - FormRequest에 prepareForValidation() 추가로 프론트 호환성 유지 - 한글 입력 시 자동으로 code 변환 (매입 → PURCHASE) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../Requests/Client/ClientStoreRequest.php | 37 ++++++ .../Requests/Client/ClientUpdateRequest.php | 37 ++++++ ...160215_convert_clients_enum_to_varchar.php | 110 ++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 database/migrations/2025_12_21_160215_convert_clients_enum_to_varchar.php diff --git a/app/Http/Requests/Client/ClientStoreRequest.php b/app/Http/Requests/Client/ClientStoreRequest.php index 51c344d..aa2db4e 100644 --- a/app/Http/Requests/Client/ClientStoreRequest.php +++ b/app/Http/Requests/Client/ClientStoreRequest.php @@ -12,6 +12,43 @@ public function authorize(): bool return true; } + protected function prepareForValidation(): void + { + $this->convertCommonCodeNameToCode('client_type'); + $this->convertCommonCodeNameToCode('bad_debt_progress'); + } + + /** + * common_codes의 name을 code로 변환 + */ + private function convertCommonCodeNameToCode(string $field): void + { + $value = $this->input($field); + if (! $value) { + return; + } + + // 이미 code인지 확인 + $existsAsCode = \DB::table('common_codes') + ->where('code_group', $field) + ->where('code', $value) + ->exists(); + + if ($existsAsCode) { + return; + } + + // name으로 code 조회 + $code = \DB::table('common_codes') + ->where('code_group', $field) + ->where('name', $value) + ->value('code'); + + if ($code) { + $this->merge([$field => $code]); + } + } + public function rules(): array { return [ diff --git a/app/Http/Requests/Client/ClientUpdateRequest.php b/app/Http/Requests/Client/ClientUpdateRequest.php index 636001f..5995bdb 100644 --- a/app/Http/Requests/Client/ClientUpdateRequest.php +++ b/app/Http/Requests/Client/ClientUpdateRequest.php @@ -12,6 +12,43 @@ public function authorize(): bool return true; } + protected function prepareForValidation(): void + { + $this->convertCommonCodeNameToCode('client_type'); + $this->convertCommonCodeNameToCode('bad_debt_progress'); + } + + /** + * common_codes의 name을 code로 변환 + */ + private function convertCommonCodeNameToCode(string $field): void + { + $value = $this->input($field); + if (! $value) { + return; + } + + // 이미 code인지 확인 + $existsAsCode = \DB::table('common_codes') + ->where('code_group', $field) + ->where('code', $value) + ->exists(); + + if ($existsAsCode) { + return; + } + + // name으로 code 조회 + $code = \DB::table('common_codes') + ->where('code_group', $field) + ->where('name', $value) + ->value('code'); + + if ($code) { + $this->merge([$field => $code]); + } + } + public function rules(): array { return [ diff --git a/database/migrations/2025_12_21_160215_convert_clients_enum_to_varchar.php b/database/migrations/2025_12_21_160215_convert_clients_enum_to_varchar.php new file mode 100644 index 0000000..09aada0 --- /dev/null +++ b/database/migrations/2025_12_21_160215_convert_clients_enum_to_varchar.php @@ -0,0 +1,110 @@ +where('code_group', 'client_type') + ->pluck('code', 'name') + ->toArray(); + + $badDebtProgressMap = DB::table('common_codes') + ->where('code_group', 'bad_debt_progress') + ->pluck('code', 'name') + ->toArray(); + + // 2. 임시 컬럼 추가 + Schema::table('clients', function (Blueprint $table) { + $table->string('client_type_new', 20)->nullable()->after('client_type'); + $table->string('bad_debt_progress_new', 20)->nullable()->after('bad_debt_progress'); + }); + + // 3. 데이터 변환 (name → code) + foreach ($clientTypeMap as $name => $code) { + DB::table('clients') + ->where('client_type', $name) + ->update(['client_type_new' => $code]); + } + + foreach ($badDebtProgressMap as $name => $code) { + DB::table('clients') + ->where('bad_debt_progress', $name) + ->update(['bad_debt_progress_new' => $code]); + } + + // 4. 기존 ENUM 컬럼 삭제 및 새 컬럼 이름 변경 + Schema::table('clients', function (Blueprint $table) { + $table->dropColumn('client_type'); + $table->dropColumn('bad_debt_progress'); + }); + + Schema::table('clients', function (Blueprint $table) { + $table->renameColumn('client_type_new', 'client_type'); + $table->renameColumn('bad_debt_progress_new', 'bad_debt_progress'); + }); + + // 5. 컬럼 코멘트 추가 + DB::statement("ALTER TABLE `clients` MODIFY `client_type` VARCHAR(20) NULL COMMENT '거래처 유형 (common_codes.client_type)'"); + DB::statement("ALTER TABLE `clients` MODIFY `bad_debt_progress` VARCHAR(20) NULL COMMENT '악성채권 진행상태 (common_codes.bad_debt_progress)'"); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + // 1. common_codes에서 code → name 매핑 조회 + $clientTypeMap = DB::table('common_codes') + ->where('code_group', 'client_type') + ->pluck('name', 'code') + ->toArray(); + + $badDebtProgressMap = DB::table('common_codes') + ->where('code_group', 'bad_debt_progress') + ->pluck('name', 'code') + ->toArray(); + + // 2. 임시 컬럼 추가 (ENUM) + DB::statement("ALTER TABLE `clients` ADD `client_type_old` ENUM('매입','매출','매입매출') NULL AFTER `client_type`"); + DB::statement("ALTER TABLE `clients` ADD `bad_debt_progress_old` ENUM('협의중','소송중','회수완료','대손처리') NULL AFTER `bad_debt_progress`"); + + // 3. 데이터 변환 (code → name) + foreach ($clientTypeMap as $code => $name) { + DB::table('clients') + ->where('client_type', $code) + ->update(['client_type_old' => $name]); + } + + foreach ($badDebtProgressMap as $code => $name) { + DB::table('clients') + ->where('bad_debt_progress', $code) + ->update(['bad_debt_progress_old' => $name]); + } + + // 4. VARCHAR 컬럼 삭제 및 ENUM 컬럼 이름 변경 + Schema::table('clients', function (Blueprint $table) { + $table->dropColumn('client_type'); + $table->dropColumn('bad_debt_progress'); + }); + + Schema::table('clients', function (Blueprint $table) { + $table->renameColumn('client_type_old', 'client_type'); + $table->renameColumn('bad_debt_progress_old', 'bad_debt_progress'); + }); + + // 5. 기본값 설정 + DB::statement("ALTER TABLE `clients` MODIFY `client_type` ENUM('매입','매출','매입매출') NOT NULL DEFAULT '매입' COMMENT '거래처 유형'"); + DB::statement("ALTER TABLE `clients` MODIFY `bad_debt_progress` ENUM('협의중','소송중','회수완료','대손처리') NULL COMMENT '진행 상태'"); + } +};