feat: 파일 저장 시스템 DB 마이그레이션
- enhance_files_table: 이중 파일명 시스템 (display_name/stored_name), 폴더 관리, 문서 연결 지원
- create_folders_table: 동적 폴더 관리 시스템 (tenant별 커스터마이징 가능)
- 5개 stub 마이그레이션 생성 (file_share_links, file_deletion_logs, storage_usage_history, add_storage_columns_to_tenants)
- FolderSeeder stub 생성
- CURRENT_WORKS.md에 Phase 1 진행상황 문서화
fix: 파일 공유 및 삭제 기능 버그 수정
- ShareLinkRequest: PATH 파라미터 {id}를 file_id로 자동 병합
- routes/api.php: 공유 링크 다운로드를 auth.apikey 그룹 밖으로 이동 (인증 불필요)
- FileShareLink: File, Tenant 클래스 import 추가
- File 모델: softDeleteFile()에서 SoftDeletes의 delete() 메서드 사용
- FileStorageService: getTrash(), restoreFile(), permanentDelete()에서 onlyTrashed() 사용
- File 모델: Tenant 네임스페이스 수정 (App\Models\Tenants\Tenant)
refactor: Swagger 문서 정리 - File 태그를 Files로 통합
- FileApi.php의 모든 태그를 Files로 변경
- 구 파일 시스템 라우트 삭제 (prefix 'file')
- 구 FileController.php 삭제
- 신규 파일 저장소 시스템으로 완전 통합
fix: 모든 legacy 파일 컬럼 nullable 일괄 처리
- 5개 legacy 컬럼을 한 번에 nullable로 변경
* original_name, file_name, file_name_old (string)
* fileable_id, fileable_type (polymorphic)
- foreach 루프로 반복 작업 자동화
- 신규/기존 시스템 간 완전한 하위 호환성 확보
fix: legacy 파일 컬럼 nullable 처리 완료
- file_name, file_name_old 컬럼도 nullable로 변경
- 기존 시스템과 신규 시스템 간 완전한 하위 호환성 확보
- Legacy: original_name, file_name, file_name_old (nullable)
- New: display_name, stored_name (required)
fix: original_name 컬럼 nullable 처리
- original_name을 nullable로 변경하여 하위 호환성 유지
- 새 시스템에서는 display_name 사용, 기존 시스템은 original_name 사용 가능
fix: 파일 업로드 DB 컬럼 누락 및 메시지 구조 개선
- files 테이블에 감사 컬럼 추가 (created_by, updated_by, uploaded_by)
- ApiResponse::handle() 메시지 로직 개선 (접미사 제거)
- 다국어 지원을 위한 완성된 문장 구조 유지
- FileUploadRequest 파일 검증 규칙 수정
fix: 파일 저장소 버그 수정 및 신규 테넌트 폴더 자동 생성
- FolderSeeder 네임스페이스 수정 (App\Models\Tenant → App\Models\Tenants\Tenant)
- FileStorageController use 문 구문 오류 수정 (/ → \)
- TenantObserver에 신규 테넌트 기본 폴더 자동 생성 로직 추가
- 5개 기본 폴더 (생산관리, 품질관리, 회계, 인사, 일반)
- 에러 처리 및 로깅
- 회원가입 시 자동 실행
This commit is contained in:
162
database/migrations/2025_11_10_190208_enhance_files_table.php
Normal file
162
database/migrations/2025_11_10_190208_enhance_files_table.php
Normal file
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('files', function (Blueprint $table) {
|
||||
// Legacy 컬럼 nullable 변경 (하위 호환성) - 한 번에 처리
|
||||
$legacyColumns = [
|
||||
'original_name' => 'string',
|
||||
'file_name' => 'string',
|
||||
'file_name_old' => 'string',
|
||||
'fileable_id' => 'bigInteger',
|
||||
'fileable_type' => 'string',
|
||||
];
|
||||
|
||||
foreach ($legacyColumns as $column => $type) {
|
||||
if (Schema::hasColumn('files', $column)) {
|
||||
if ($type === 'bigInteger') {
|
||||
$table->unsignedBigInteger($column)->nullable()->change();
|
||||
} else {
|
||||
$table->string($column, 255)->nullable()->change();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 파일명 시스템 개선
|
||||
if (! Schema::hasColumn('files', 'display_name')) {
|
||||
$table->string('display_name', 255)->after('file_path')->comment('사용자가 보는 이름');
|
||||
}
|
||||
if (! Schema::hasColumn('files', 'stored_name')) {
|
||||
$table->string('stored_name', 255)->after('display_name')->comment('실제 저장 이름 (64bit 난수)');
|
||||
}
|
||||
|
||||
// 폴더 관리
|
||||
if (! Schema::hasColumn('files', 'folder_id')) {
|
||||
$table->unsignedBigInteger('folder_id')->nullable()->after('tenant_id')->comment('folders 테이블 FK');
|
||||
}
|
||||
if (! Schema::hasColumn('files', 'is_temp')) {
|
||||
$table->boolean('is_temp')->default(true)->after('folder_id')->comment('temp 폴더 여부');
|
||||
}
|
||||
|
||||
// 파일 분류
|
||||
if (! Schema::hasColumn('files', 'file_type')) {
|
||||
$table->enum('file_type', ['document', 'image', 'excel', 'archive'])->after('mime_type')->comment('파일 타입');
|
||||
}
|
||||
|
||||
// 문서 연결
|
||||
if (! Schema::hasColumn('files', 'document_id')) {
|
||||
$table->unsignedBigInteger('document_id')->nullable()->after('file_type')->comment('문서 ID');
|
||||
}
|
||||
if (! Schema::hasColumn('files', 'document_type')) {
|
||||
$table->string('document_type', 50)->nullable()->after('document_id')->comment('문서 타입');
|
||||
}
|
||||
|
||||
// 감사 컬럼
|
||||
if (! Schema::hasColumn('files', 'uploaded_by')) {
|
||||
$table->unsignedBigInteger('uploaded_by')->nullable()->after('description')->comment('업로더 user_id');
|
||||
}
|
||||
if (! Schema::hasColumn('files', 'deleted_by')) {
|
||||
$table->unsignedBigInteger('deleted_by')->nullable()->after('deleted_at')->comment('삭제자 user_id');
|
||||
}
|
||||
if (! Schema::hasColumn('files', 'created_by')) {
|
||||
$table->unsignedBigInteger('created_by')->nullable()->after('deleted_by')->comment('생성자 user_id');
|
||||
}
|
||||
if (! Schema::hasColumn('files', 'updated_by')) {
|
||||
$table->unsignedBigInteger('updated_by')->nullable()->after('created_by')->comment('수정자 user_id');
|
||||
}
|
||||
});
|
||||
|
||||
// 인덱스 추가
|
||||
Schema::table('files', function (Blueprint $table) {
|
||||
$table->index(['tenant_id', 'folder_id'], 'idx_tenant_folder');
|
||||
$table->index('is_temp');
|
||||
$table->index('document_id');
|
||||
$table->index('created_at');
|
||||
$table->index('stored_name');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
// 인덱스 삭제 (에러 무시)
|
||||
try {
|
||||
Schema::table('files', function (Blueprint $table) {
|
||||
$table->dropIndex('idx_tenant_folder');
|
||||
});
|
||||
} catch (\Exception $e) {
|
||||
// 인덱스가 없으면 무시
|
||||
}
|
||||
|
||||
try {
|
||||
Schema::table('files', function (Blueprint $table) {
|
||||
$table->dropIndex(['is_temp']);
|
||||
});
|
||||
} catch (\Exception $e) {
|
||||
// 인덱스가 없으면 무시
|
||||
}
|
||||
|
||||
try {
|
||||
Schema::table('files', function (Blueprint $table) {
|
||||
$table->dropIndex(['document_id']);
|
||||
});
|
||||
} catch (\Exception $e) {
|
||||
// 인덱스가 없으면 무시
|
||||
}
|
||||
|
||||
try {
|
||||
Schema::table('files', function (Blueprint $table) {
|
||||
$table->dropIndex(['created_at']);
|
||||
});
|
||||
} catch (\Exception $e) {
|
||||
// 인덱스가 없으면 무시
|
||||
}
|
||||
|
||||
try {
|
||||
Schema::table('files', function (Blueprint $table) {
|
||||
$table->dropIndex(['stored_name']);
|
||||
});
|
||||
} catch (\Exception $e) {
|
||||
// 인덱스가 없으면 무시
|
||||
}
|
||||
|
||||
// 컬럼 삭제 (존재하는 것만)
|
||||
Schema::table('files', function (Blueprint $table) {
|
||||
$columnsToCheck = [
|
||||
'display_name',
|
||||
'stored_name',
|
||||
'folder_id',
|
||||
'is_temp',
|
||||
'file_type',
|
||||
'document_id',
|
||||
'document_type',
|
||||
'uploaded_by',
|
||||
'deleted_by',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
];
|
||||
|
||||
$columnsToDrop = [];
|
||||
foreach ($columnsToCheck as $column) {
|
||||
if (Schema::hasColumn('files', $column)) {
|
||||
$columnsToDrop[] = $column;
|
||||
}
|
||||
}
|
||||
|
||||
if (! empty($columnsToDrop)) {
|
||||
$table->dropColumn($columnsToDrop);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('folders', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
|
||||
|
||||
// 폴더 정보
|
||||
$table->string('folder_key', 50)->comment('폴더 키 (product, quality, accounting)');
|
||||
$table->string('folder_name', 100)->comment('폴더명 (생산관리, 품질관리, 회계)');
|
||||
$table->text('description')->nullable()->comment('설명');
|
||||
|
||||
// 순서 및 표시
|
||||
$table->integer('display_order')->default(0)->comment('표시 순서');
|
||||
$table->boolean('is_active')->default(true)->comment('활성 여부');
|
||||
|
||||
// UI 커스터마이징 (선택)
|
||||
$table->string('icon', 50)->nullable()->comment('아이콘 (icon-production, icon-quality)');
|
||||
$table->string('color', 20)->nullable()->comment('색상 (#3B82F6)');
|
||||
|
||||
// 감사 컬럼
|
||||
$table->unsignedBigInteger('created_by')->nullable();
|
||||
$table->unsignedBigInteger('updated_by')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
// 인덱스
|
||||
$table->unique(['tenant_id', 'folder_key'], 'uq_tenant_folder_key');
|
||||
$table->index(['tenant_id', 'is_active'], 'idx_active');
|
||||
$table->index(['tenant_id', 'display_order'], 'idx_display_order');
|
||||
|
||||
// 외래키
|
||||
$table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('folders');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('tenants', function (Blueprint $table) {
|
||||
// 용량 관리
|
||||
$table->bigInteger('storage_limit')->default(10737418240)->comment('저장소 한도 (10GB)');
|
||||
$table->bigInteger('storage_used')->default(0)->comment('사용 중인 용량 (bytes)');
|
||||
$table->timestamp('storage_warning_sent_at')->nullable()->comment('90% 경고 발송 시간');
|
||||
$table->timestamp('storage_grace_period_until')->nullable()->comment('유예 기간 종료 시간 (7일)');
|
||||
|
||||
// 인덱스
|
||||
$table->index(['storage_used'], 'idx_storage_used');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('tenants', function (Blueprint $table) {
|
||||
$table->dropIndex('idx_storage_used');
|
||||
$table->dropColumn([
|
||||
'storage_limit',
|
||||
'storage_used',
|
||||
'storage_warning_sent_at',
|
||||
'storage_grace_period_until',
|
||||
]);
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('file_deletion_logs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
|
||||
$table->unsignedBigInteger('file_id')->comment('파일 ID');
|
||||
|
||||
// 파일 정보 (삭제 시점 스냅샷)
|
||||
$table->string('file_name', 255)->comment('파일명 (display_name)');
|
||||
$table->string('file_path', 1000)->comment('파일 경로');
|
||||
$table->bigInteger('file_size')->comment('파일 크기 (bytes)');
|
||||
|
||||
// 연관 정보
|
||||
$table->unsignedBigInteger('folder_id')->nullable()->comment('폴더 ID');
|
||||
$table->unsignedBigInteger('document_id')->nullable()->comment('문서 ID');
|
||||
$table->string('document_type', 100)->nullable()->comment('문서 타입');
|
||||
|
||||
// 삭제 정보
|
||||
$table->unsignedBigInteger('deleted_by')->comment('삭제자 ID');
|
||||
$table->timestamp('deleted_at')->comment('삭제 시간');
|
||||
$table->enum('deletion_type', ['soft', 'permanent'])->default('soft')->comment('삭제 유형');
|
||||
|
||||
// 인덱스
|
||||
$table->index(['tenant_id'], 'idx_tenant');
|
||||
$table->index(['file_id'], 'idx_file');
|
||||
$table->index(['deleted_at'], 'idx_deleted_at');
|
||||
$table->index(['deletion_type'], 'idx_deletion_type');
|
||||
|
||||
// 외래키
|
||||
$table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('file_deletion_logs');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('file_share_links', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('file_id')->comment('파일 ID');
|
||||
$table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
|
||||
|
||||
// 공유 링크 정보
|
||||
$table->string('token', 64)->unique()->comment('공유 토큰 (64자 랜덤)');
|
||||
$table->timestamp('expires_at')->comment('만료 시간 (24시간 기본)');
|
||||
|
||||
// 다운로드 추적
|
||||
$table->integer('download_count')->default(0)->comment('다운로드 횟수');
|
||||
$table->integer('max_downloads')->nullable()->comment('최대 다운로드 횟수 (null=무제한)');
|
||||
$table->timestamp('last_downloaded_at')->nullable()->comment('마지막 다운로드 시간');
|
||||
$table->string('last_downloaded_ip', 45)->nullable()->comment('마지막 다운로드 IP');
|
||||
|
||||
// 감사 정보
|
||||
$table->unsignedBigInteger('created_by')->comment('생성자 ID');
|
||||
$table->timestamp('created_at')->useCurrent();
|
||||
|
||||
// 인덱스
|
||||
$table->index(['file_id'], 'idx_file');
|
||||
$table->index(['tenant_id'], 'idx_tenant');
|
||||
$table->index(['token'], 'idx_token');
|
||||
$table->index(['expires_at'], 'idx_expires');
|
||||
|
||||
// 외래키
|
||||
$table->foreign('file_id')->references('id')->on('files')->onDelete('cascade');
|
||||
$table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('file_share_links');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('storage_usage_history', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
|
||||
|
||||
// 용량 정보
|
||||
$table->bigInteger('storage_used')->comment('사용량 (bytes)');
|
||||
$table->integer('file_count')->comment('파일 개수');
|
||||
|
||||
// 폴더별 상세 (JSON)
|
||||
$table->json('folder_usage')->nullable()->comment('폴더별 용량 {"product": 1024000, "quality": 512000}');
|
||||
|
||||
// 기록 시간
|
||||
$table->timestamp('recorded_at')->useCurrent()->comment('기록 시간');
|
||||
|
||||
// 인덱스
|
||||
$table->index(['tenant_id', 'recorded_at'], 'idx_tenant_recorded');
|
||||
|
||||
// 외래키
|
||||
$table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('storage_usage_history');
|
||||
}
|
||||
};
|
||||
91
database/seeders/FolderSeeder.php
Normal file
91
database/seeders/FolderSeeder.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class FolderSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* Usage:
|
||||
* php artisan db:seed --class=FolderSeeder
|
||||
* or from code: (new FolderSeeder)->run(tenantId: 1)
|
||||
*/
|
||||
public function run(?int $tenantId = null): void
|
||||
{
|
||||
// 테넌트 ID가 지정되지 않으면 모든 테넌트에 대해 실행
|
||||
$tenants = $tenantId
|
||||
? [\App\Models\Tenants\Tenant::findOrFail($tenantId)]
|
||||
: \App\Models\Tenants\Tenant::all();
|
||||
|
||||
foreach ($tenants as $tenant) {
|
||||
// 이미 폴더가 있는지 확인
|
||||
if (\App\Models\Folder::where('tenant_id', $tenant->id)->exists()) {
|
||||
$this->command->info("Tenant {$tenant->id} already has folders. Skipping...");
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// 기본 폴더 생성
|
||||
$defaultFolders = [
|
||||
[
|
||||
'folder_key' => 'product',
|
||||
'folder_name' => '생산관리',
|
||||
'description' => '생산 관련 문서 및 도면',
|
||||
'icon' => 'icon-production',
|
||||
'color' => '#3B82F6',
|
||||
'display_order' => 1,
|
||||
],
|
||||
[
|
||||
'folder_key' => 'quality',
|
||||
'folder_name' => '품질관리',
|
||||
'description' => '품질 검사 및 인증 문서',
|
||||
'icon' => 'icon-quality',
|
||||
'color' => '#10B981',
|
||||
'display_order' => 2,
|
||||
],
|
||||
[
|
||||
'folder_key' => 'accounting',
|
||||
'folder_name' => '회계',
|
||||
'description' => '회계 관련 증빙 서류',
|
||||
'icon' => 'icon-accounting',
|
||||
'color' => '#F59E0B',
|
||||
'display_order' => 3,
|
||||
],
|
||||
[
|
||||
'folder_key' => 'hr',
|
||||
'folder_name' => '인사',
|
||||
'description' => '인사 관련 문서',
|
||||
'icon' => 'icon-hr',
|
||||
'color' => '#8B5CF6',
|
||||
'display_order' => 4,
|
||||
],
|
||||
[
|
||||
'folder_key' => 'general',
|
||||
'folder_name' => '일반',
|
||||
'description' => '기타 문서',
|
||||
'icon' => 'icon-general',
|
||||
'color' => '#6B7280',
|
||||
'display_order' => 5,
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($defaultFolders as $folder) {
|
||||
\App\Models\Folder::create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'folder_key' => $folder['folder_key'],
|
||||
'folder_name' => $folder['folder_name'],
|
||||
'description' => $folder['description'],
|
||||
'icon' => $folder['icon'],
|
||||
'color' => $folder['color'],
|
||||
'display_order' => $folder['display_order'],
|
||||
'is_active' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
$this->command->info("Created default folders for tenant {$tenant->id}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user