Files
sam-api/database/migrations/2025_07_26_000818_create_users_table.php
kent f5534e437b fix : 테이블 추가 및 수정
- 신규 (user_roles, user_tenants)
- 수정 (roles, tenants, users)
2025-07-26 01:23:02 +09:00

52 lines
2.0 KiB
PHP

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
// 1. tenant_id NOT NULL 추가
$table->unsignedBigInteger('tenant_id')->after('id')->comment('FK: 테넌트 ID')->default(1); // default 값은 마이그레이션 중 기존 데이터 보호용
// 2. 컬럼별 주석 보강
$table->string('name', 255)->comment('회원 이름')->change();
$table->string('email', 255)->comment('이메일')->change();
$table->string('password', 255)->comment('비밀번호')->change();
$table->string('profile_photo_path', 2048)->nullable()->comment('프로필 사진 경로')->change();
$table->string('remember_token', 100)->nullable()->comment('자동로그인 토큰')->change();
// 3. soft delete 추가
$table->softDeletes()->comment('삭제일시(소프트삭제)');
// 4. 유니크키 변경: (tenant_id, email)로
$table->dropUnique(['email']);
$table->unique(['tenant_id', 'email'], 'uk_users_tenant_email');
});
// FK 추가 (이미 tenants 테이블이 있어야 함)
Schema::table('users', function (Blueprint $table) {
$table->foreign('tenant_id')->references('id')->on('tenants');
});
}
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
// 롤백: FK/유니크/컬럼 제거
$table->dropForeign(['tenant_id']);
$table->dropUnique('uk_users_tenant_email');
$table->dropColumn('tenant_id');
$table->dropSoftDeletes();
});
// 기존 유니크 복구
Schema::table('users', function (Blueprint $table) {
$table->unique('email');
});
}
};