feat: 데이터베이스 FK 제약조건 최적화 및 3단계 마이그레이션 구현
- FK 제약조건 현황 분석 완료: 8개 마이그레이션에서 15+개 FK 식별 - 중요도별 테이블 분류 (핵심/중요/일반) 및 안전한 제거 전략 수립 - 코드 영향도 분석: Eloquent 관계가 FK 독립적으로 작동하여 코드 수정 불필요 주요 변경사항: - Phase 1: classifications.tenant_id, departments.parent_id FK 제거 - Phase 2: estimates.model_set_id, estimate_items.estimate_id FK 제거 - Phase 3: product_components.material_id FK 제거 (신중한 검토 필요) - 각 단계별 동적 FK 탐지, 상세 로깅, 완전 롤백 기능 포함 - 성능 인덱스 유지/추가로 쿼리 성능 보장 예상 효과: - 견적 시스템 및 분류 관리 성능 향상 - 부서 구조 변경 및 자재 관리 유연성 증가 - FK 제약 에러 감소로 개발 생산성 향상 - 시스템 확장 시 스키마 변경 유연성 확보 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Phase 1: 중요하지 않은 FK 제약조건 제거
|
||||
* - classifications.tenant_id → tenants
|
||||
* - departments.parent_id → departments (자기참조)
|
||||
*
|
||||
* 목적: 관리 편의성 향상 및 성능 최적화
|
||||
* 영향: 없음 (Eloquent 관계는 유지, 비즈니스 로직 무결성은 앱 레벨에서 관리)
|
||||
*/
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* 기존 FK 제약조건 이름을 동적으로 찾는 헬퍼 함수
|
||||
*/
|
||||
private function findForeignKeyName(string $table, string $column): ?string
|
||||
{
|
||||
$result = DB::selectOne("
|
||||
SELECT CONSTRAINT_NAME
|
||||
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = ?
|
||||
AND COLUMN_NAME = ?
|
||||
AND REFERENCED_TABLE_NAME IS NOT NULL
|
||||
LIMIT 1
|
||||
", [$table, $column]);
|
||||
|
||||
return $result ? $result->CONSTRAINT_NAME : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* FK 제약조건을 안전하게 제거하는 헬퍼 함수
|
||||
*/
|
||||
private function dropForeignKeyIfExists(string $table, string $column): void
|
||||
{
|
||||
$fkName = $this->findForeignKeyName($table, $column);
|
||||
if ($fkName) {
|
||||
DB::statement("ALTER TABLE `{$table}` DROP FOREIGN KEY `{$fkName}`");
|
||||
echo "✅ Dropped FK: {$table}.{$column} ({$fkName})\n";
|
||||
} else {
|
||||
echo "ℹ️ No FK found: {$table}.{$column}\n";
|
||||
}
|
||||
}
|
||||
|
||||
public function up(): void
|
||||
{
|
||||
echo "🚀 Phase 1: 중요하지 않은 FK 제약조건 제거 시작\n\n";
|
||||
|
||||
// 1. classifications.tenant_id → tenants 제거
|
||||
echo "1️⃣ Classifications 테이블 FK 제거...\n";
|
||||
$this->dropForeignKeyIfExists('classifications', 'tenant_id');
|
||||
|
||||
// 2. departments.parent_id → departments (자기참조) 제거
|
||||
echo "\n2️⃣ Departments 자기참조 FK 제거...\n";
|
||||
$this->dropForeignKeyIfExists('departments', 'parent_id');
|
||||
|
||||
// 3. 인덱스는 유지 (쿼리 성능을 위해)
|
||||
echo "\n3️⃣ 인덱스 상태 확인 및 유지...\n";
|
||||
|
||||
// classifications.tenant_id 인덱스 확인
|
||||
$classificationIndexExists = DB::selectOne("
|
||||
SHOW INDEX FROM classifications WHERE Column_name = 'tenant_id'
|
||||
");
|
||||
if (!$classificationIndexExists) {
|
||||
DB::statement("CREATE INDEX idx_classifications_tenant_id ON classifications (tenant_id)");
|
||||
echo "✅ Added index: classifications.tenant_id\n";
|
||||
} else {
|
||||
echo "ℹ️ Index exists: classifications.tenant_id\n";
|
||||
}
|
||||
|
||||
// departments.parent_id 인덱스 확인
|
||||
$departmentIndexExists = DB::selectOne("
|
||||
SHOW INDEX FROM departments WHERE Column_name = 'parent_id'
|
||||
");
|
||||
if (!$departmentIndexExists) {
|
||||
DB::statement("CREATE INDEX idx_departments_parent_id ON departments (parent_id)");
|
||||
echo "✅ Added index: departments.parent_id\n";
|
||||
} else {
|
||||
echo "ℹ️ Index exists: departments.parent_id\n";
|
||||
}
|
||||
|
||||
echo "\n🎉 Phase 1 FK 제거 완료!\n";
|
||||
echo "📋 제거된 FK:\n";
|
||||
echo " - classifications.tenant_id → tenants\n";
|
||||
echo " - departments.parent_id → departments\n";
|
||||
echo "📈 예상 효과: 분류 코드 관리 및 부서 구조 변경 시 유연성 증가\n";
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
echo "🔄 Phase 1 FK 제거 롤백 시작...\n\n";
|
||||
|
||||
// 1. departments.parent_id → departments FK 복구
|
||||
echo "1️⃣ Departments 자기참조 FK 복구...\n";
|
||||
try {
|
||||
Schema::table('departments', function (Blueprint $table) {
|
||||
$table->foreign('parent_id')
|
||||
->references('id')
|
||||
->on('departments')
|
||||
->nullOnDelete();
|
||||
});
|
||||
echo "✅ Restored FK: departments.parent_id → departments\n";
|
||||
} catch (\Throwable $e) {
|
||||
echo "⚠️ Could not restore FK: departments.parent_id - " . $e->getMessage() . "\n";
|
||||
}
|
||||
|
||||
// 2. classifications.tenant_id → tenants FK 복구
|
||||
echo "\n2️⃣ Classifications 테넌트 FK 복구...\n";
|
||||
try {
|
||||
Schema::table('classifications', function (Blueprint $table) {
|
||||
$table->foreign('tenant_id')
|
||||
->references('id')
|
||||
->on('tenants')
|
||||
->cascadeOnUpdate()
|
||||
->restrictOnDelete();
|
||||
});
|
||||
echo "✅ Restored FK: classifications.tenant_id → tenants\n";
|
||||
} catch (\Throwable $e) {
|
||||
echo "⚠️ Could not restore FK: classifications.tenant_id - " . $e->getMessage() . "\n";
|
||||
}
|
||||
|
||||
echo "\n🔄 Phase 1 FK 복구 완료!\n";
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Phase 2: 견적 시스템 FK 제약조건 제거
|
||||
* - estimates.tenant_id → tenants (유지 - 멀티테넌트 핵심)
|
||||
* - estimates.model_set_id → categories (제거 - 견적은 스냅샷 성격)
|
||||
* - estimate_items.tenant_id → tenants (유지 - 멀티테넌트 핵심)
|
||||
* - estimate_items.estimate_id → estimates (제거 - 성능 우선)
|
||||
*
|
||||
* 목적: 견적 시스템 성능 향상 및 데이터 유연성 확보
|
||||
* 주의: 견적 데이터는 스냅샷 성격이므로 참조 무결성보다 성능/유연성이 중요
|
||||
*/
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* 기존 FK 제약조건 이름을 동적으로 찾는 헬퍼 함수
|
||||
*/
|
||||
private function findForeignKeyName(string $table, string $column): ?string
|
||||
{
|
||||
$result = DB::selectOne("
|
||||
SELECT CONSTRAINT_NAME
|
||||
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = ?
|
||||
AND COLUMN_NAME = ?
|
||||
AND REFERENCED_TABLE_NAME IS NOT NULL
|
||||
LIMIT 1
|
||||
", [$table, $column]);
|
||||
|
||||
return $result ? $result->CONSTRAINT_NAME : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* FK 제약조건을 안전하게 제거하는 헬퍼 함수
|
||||
*/
|
||||
private function dropForeignKeyIfExists(string $table, string $column): void
|
||||
{
|
||||
$fkName = $this->findForeignKeyName($table, $column);
|
||||
if ($fkName) {
|
||||
DB::statement("ALTER TABLE `{$table}` DROP FOREIGN KEY `{$fkName}`");
|
||||
echo "✅ Dropped FK: {$table}.{$column} ({$fkName})\n";
|
||||
} else {
|
||||
echo "ℹ️ No FK found: {$table}.{$column}\n";
|
||||
}
|
||||
}
|
||||
|
||||
public function up(): void
|
||||
{
|
||||
echo "🚀 Phase 2: 견적 시스템 FK 제약조건 제거 시작\n\n";
|
||||
|
||||
// 견적 시스템 테이블 존재 여부 확인
|
||||
$estimatesExists = Schema::hasTable('estimates');
|
||||
$estimateItemsExists = Schema::hasTable('estimate_items');
|
||||
|
||||
if (!$estimatesExists && !$estimateItemsExists) {
|
||||
echo "ℹ️ 견적 시스템 테이블이 존재하지 않습니다. 건너뜁니다.\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if ($estimatesExists) {
|
||||
echo "1️⃣ Estimates 테이블 FK 검토...\n";
|
||||
|
||||
// estimates.model_set_id → categories 제거 (견적 스냅샷 특성상 불필요)
|
||||
echo " - model_set_id FK 제거 (견적 스냅샷 특성)\n";
|
||||
$this->dropForeignKeyIfExists('estimates', 'model_set_id');
|
||||
|
||||
// estimates.tenant_id → tenants 유지 (멀티테넌트 핵심)
|
||||
echo " - tenant_id FK 유지 (멀티테넌트 보안)\n";
|
||||
|
||||
// 성능을 위한 인덱스 확인/추가
|
||||
$indexExists = DB::selectOne("
|
||||
SHOW INDEX FROM estimates WHERE Key_name = 'idx_estimates_tenant_model_set'
|
||||
");
|
||||
if (!$indexExists) {
|
||||
DB::statement("CREATE INDEX idx_estimates_tenant_model_set ON estimates (tenant_id, model_set_id)");
|
||||
echo "✅ Added performance index: estimates(tenant_id, model_set_id)\n";
|
||||
}
|
||||
}
|
||||
|
||||
if ($estimateItemsExists) {
|
||||
echo "\n2️⃣ Estimate_items 테이블 FK 검토...\n";
|
||||
|
||||
// estimate_items.estimate_id → estimates 제거 (성능 우선)
|
||||
echo " - estimate_id FK 제거 (성능 우선)\n";
|
||||
$this->dropForeignKeyIfExists('estimate_items', 'estimate_id');
|
||||
|
||||
// estimate_items.tenant_id → tenants 유지 (멀티테넌트 핵심)
|
||||
echo " - tenant_id FK 유지 (멀티테넌트 보안)\n";
|
||||
|
||||
// 성능을 위한 인덱스 확인/추가
|
||||
$indexExists = DB::selectOne("
|
||||
SHOW INDEX FROM estimate_items WHERE Key_name = 'idx_estimate_items_tenant_estimate'
|
||||
");
|
||||
if (!$indexExists) {
|
||||
DB::statement("CREATE INDEX idx_estimate_items_tenant_estimate ON estimate_items (tenant_id, estimate_id)");
|
||||
echo "✅ Added performance index: estimate_items(tenant_id, estimate_id)\n";
|
||||
}
|
||||
}
|
||||
|
||||
echo "\n🎉 Phase 2 견적 시스템 FK 제거 완료!\n";
|
||||
echo "📋 제거된 FK:\n";
|
||||
if ($estimatesExists) {
|
||||
echo " - estimates.model_set_id → categories\n";
|
||||
}
|
||||
if ($estimateItemsExists) {
|
||||
echo " - estimate_items.estimate_id → estimates\n";
|
||||
}
|
||||
echo "📈 예상 효과: 견적 생성/수정 성능 향상, 카테고리 변경 시 유연성 증가\n";
|
||||
echo "🔒 유지된 FK: 멀티테넌트 보안을 위한 tenant_id 관계\n";
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
echo "🔄 Phase 2 견적 시스템 FK 복구 시작...\n\n";
|
||||
|
||||
if (Schema::hasTable('estimate_items')) {
|
||||
echo "1️⃣ Estimate_items FK 복구...\n";
|
||||
try {
|
||||
Schema::table('estimate_items', function (Blueprint $table) {
|
||||
$table->foreign('estimate_id')
|
||||
->references('id')
|
||||
->on('estimates')
|
||||
->cascadeOnDelete();
|
||||
});
|
||||
echo "✅ Restored FK: estimate_items.estimate_id → estimates\n";
|
||||
} catch (\Throwable $e) {
|
||||
echo "⚠️ Could not restore FK: estimate_items.estimate_id - " . $e->getMessage() . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if (Schema::hasTable('estimates')) {
|
||||
echo "\n2️⃣ Estimates FK 복구...\n";
|
||||
try {
|
||||
Schema::table('estimates', function (Blueprint $table) {
|
||||
$table->foreign('model_set_id')
|
||||
->references('id')
|
||||
->on('categories')
|
||||
->restrictOnDelete();
|
||||
});
|
||||
echo "✅ Restored FK: estimates.model_set_id → categories\n";
|
||||
} catch (\Throwable $e) {
|
||||
echo "⚠️ Could not restore FK: estimates.model_set_id - " . $e->getMessage() . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
echo "\n🔄 Phase 2 FK 복구 완료!\n";
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Phase 3: 제품-자재 관계 FK 제약조건 제거 (신중한 검토 필요)
|
||||
* - product_components.material_id → materials
|
||||
*
|
||||
* 목적: 자재 변경/삭제 시 유연성 확보
|
||||
* 주의사항:
|
||||
* 1. 비즈니스 로직에서 무결성 검증 필요
|
||||
* 2. 자재 삭제 시 BOM에 미치는 영향 검토 필요
|
||||
* 3. 소프트 딜리트로 대부분 처리되므로 상대적으로 안전
|
||||
*
|
||||
* 유지되는 핵심 FK:
|
||||
* - product_components.parent_product_id → products (BOM 구조 핵심)
|
||||
* - product_components.child_product_id → products (BOM 구조 핵심)
|
||||
*/
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* 기존 FK 제약조건 이름을 동적으로 찾는 헬퍼 함수
|
||||
*/
|
||||
private function findForeignKeyName(string $table, string $column): ?string
|
||||
{
|
||||
$result = DB::selectOne("
|
||||
SELECT CONSTRAINT_NAME
|
||||
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = ?
|
||||
AND COLUMN_NAME = ?
|
||||
AND REFERENCED_TABLE_NAME IS NOT NULL
|
||||
LIMIT 1
|
||||
", [$table, $column]);
|
||||
|
||||
return $result ? $result->CONSTRAINT_NAME : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* FK 제약조건을 안전하게 제거하는 헬퍼 함수
|
||||
*/
|
||||
private function dropForeignKeyIfExists(string $table, string $column): void
|
||||
{
|
||||
$fkName = $this->findForeignKeyName($table, $column);
|
||||
if ($fkName) {
|
||||
DB::statement("ALTER TABLE `{$table}` DROP FOREIGN KEY `{$fkName}`");
|
||||
echo "✅ Dropped FK: {$table}.{$column} ({$fkName})\n";
|
||||
} else {
|
||||
echo "ℹ️ No FK found: {$table}.{$column}\n";
|
||||
}
|
||||
}
|
||||
|
||||
public function up(): void
|
||||
{
|
||||
echo "🚀 Phase 3: 제품-자재 관계 FK 제약조건 제거 시작\n\n";
|
||||
echo "⚠️ 주의: 이 작업은 신중한 검토가 필요합니다!\n";
|
||||
echo "📋 영향 범위: BOM 시스템의 자재 참조 관계\n\n";
|
||||
|
||||
// product_components 테이블 존재 여부 확인
|
||||
if (!Schema::hasTable('product_components')) {
|
||||
echo "ℹ️ product_components 테이블이 존재하지 않습니다. 건너뜁니다.\n";
|
||||
return;
|
||||
}
|
||||
|
||||
echo "1️⃣ Product_components 테이블 FK 분석...\n";
|
||||
|
||||
// 현재 FK 상태 확인
|
||||
$materialFk = $this->findForeignKeyName('product_components', 'material_id');
|
||||
$parentProductFk = $this->findForeignKeyName('product_components', 'parent_product_id');
|
||||
$childProductFk = $this->findForeignKeyName('product_components', 'child_product_id');
|
||||
|
||||
echo " 현재 FK 상태:\n";
|
||||
echo " - material_id FK: " . ($materialFk ? "존재 ({$materialFk})" : "없음") . "\n";
|
||||
echo " - parent_product_id FK: " . ($parentProductFk ? "존재 ({$parentProductFk})" : "없음") . "\n";
|
||||
echo " - child_product_id FK: " . ($childProductFk ? "존재 ({$childProductFk})" : "없음") . "\n\n";
|
||||
|
||||
// material_id FK만 제거 (핵심 제품 관계는 유지)
|
||||
echo "2️⃣ Material_id FK 제거 (자재 관리 유연성)...\n";
|
||||
$this->dropForeignKeyIfExists('product_components', 'material_id');
|
||||
|
||||
echo "3️⃣ 핵심 제품 관계 FK 유지 확인...\n";
|
||||
if ($parentProductFk) {
|
||||
echo "✅ 유지: parent_product_id → products (BOM 구조 핵심)\n";
|
||||
}
|
||||
if ($childProductFk) {
|
||||
echo "✅ 유지: child_product_id → products (BOM 구조 핵심)\n";
|
||||
}
|
||||
|
||||
// 성능을 위한 인덱스 확인/추가
|
||||
echo "\n4️⃣ 성능 인덱스 확인...\n";
|
||||
$materialIndexExists = DB::selectOne("
|
||||
SHOW INDEX FROM product_components WHERE Key_name = 'idx_components_material_id'
|
||||
");
|
||||
if (!$materialIndexExists) {
|
||||
DB::statement("CREATE INDEX idx_components_material_id ON product_components (material_id)");
|
||||
echo "✅ Added performance index: product_components.material_id\n";
|
||||
} else {
|
||||
echo "ℹ️ Index exists: product_components.material_id\n";
|
||||
}
|
||||
|
||||
echo "\n🎉 Phase 3 제품-자재 FK 제거 완료!\n";
|
||||
echo "📋 제거된 FK:\n";
|
||||
echo " - product_components.material_id → materials\n";
|
||||
echo "🔒 유지된 핵심 FK:\n";
|
||||
echo " - product_components.parent_product_id → products\n";
|
||||
echo " - product_components.child_product_id → products\n";
|
||||
echo "📈 예상 효과: 자재 변경/삭제 시 BOM 유연성 증가\n";
|
||||
echo "⚠️ 주의사항: Service 레이어에서 자재 무결성 검증 필요\n";
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
echo "🔄 Phase 3 제품-자재 FK 복구 시작...\n\n";
|
||||
|
||||
if (Schema::hasTable('product_components')) {
|
||||
echo "1️⃣ Material_id FK 복구...\n";
|
||||
try {
|
||||
Schema::table('product_components', function (Blueprint $table) {
|
||||
$table->foreign('material_id')
|
||||
->references('id')
|
||||
->on('materials')
|
||||
->nullOnDelete();
|
||||
});
|
||||
echo "✅ Restored FK: product_components.material_id → materials\n";
|
||||
} catch (\Throwable $e) {
|
||||
echo "⚠️ Could not restore FK: product_components.material_id\n";
|
||||
echo " Error: " . $e->getMessage() . "\n";
|
||||
echo " 이유: 데이터 무결성 위반이나 참조되지 않는 material_id가 있을 수 있습니다.\n";
|
||||
}
|
||||
}
|
||||
|
||||
echo "\n🔄 Phase 3 FK 복구 완료!\n";
|
||||
echo "📝 참고: FK 복구 실패 시 데이터 정합성을 먼저 확인하세요.\n";
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user