|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Database\Migrations\Migration; |
| 4 | +use Illuminate\Database\Schema\Blueprint; |
| 5 | +use Illuminate\Support\Facades\Schema; |
| 6 | + |
| 7 | +return new class extends Migration |
| 8 | +{ |
| 9 | + public function up(): void |
| 10 | + { |
| 11 | + $tableName = config('authentication-log.table_name', 'authentication_log'); |
| 12 | + |
| 13 | + if (! Schema::hasTable($tableName)) { |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + // Check for columns outside the closure for better database compatibility |
| 18 | + Schema::table($tableName, function (Blueprint $table) use ($tableName) { |
| 19 | + // Add device_id column if it doesn't exist |
| 20 | + if (! Schema::hasColumn($tableName, 'device_id')) { |
| 21 | + $table->string('device_id')->nullable()->index()->after('user_agent'); |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + Schema::table($tableName, function (Blueprint $table) use ($tableName) { |
| 26 | + // Add device_name column if it doesn't exist |
| 27 | + if (! Schema::hasColumn($tableName, 'device_name')) { |
| 28 | + $table->string('device_name')->nullable()->after('device_id'); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + Schema::table($tableName, function (Blueprint $table) use ($tableName) { |
| 33 | + // Add is_trusted column if it doesn't exist |
| 34 | + if (! Schema::hasColumn($tableName, 'is_trusted')) { |
| 35 | + $table->boolean('is_trusted')->default(false)->after('device_name'); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + Schema::table($tableName, function (Blueprint $table) use ($tableName) { |
| 40 | + // Add last_activity_at column if it doesn't exist |
| 41 | + if (! Schema::hasColumn($tableName, 'last_activity_at')) { |
| 42 | + $table->timestamp('last_activity_at')->nullable()->after('logout_at'); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + Schema::table($tableName, function (Blueprint $table) use ($tableName) { |
| 47 | + // Add is_suspicious column if it doesn't exist |
| 48 | + if (! Schema::hasColumn($tableName, 'is_suspicious')) { |
| 49 | + $table->boolean('is_suspicious')->default(false)->after('location'); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + Schema::table($tableName, function (Blueprint $table) use ($tableName) { |
| 54 | + // Add suspicious_reason column if it doesn't exist |
| 55 | + if (! Schema::hasColumn($tableName, 'suspicious_reason')) { |
| 56 | + $table->string('suspicious_reason')->nullable()->after('is_suspicious'); |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + public function down(): void |
| 62 | + { |
| 63 | + $tableName = config('authentication-log.table_name', 'authentication_log'); |
| 64 | + |
| 65 | + if (! Schema::hasTable($tableName)) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + Schema::table($tableName, function (Blueprint $table) use ($tableName) { |
| 70 | + $columns = [ |
| 71 | + 'device_id', |
| 72 | + 'device_name', |
| 73 | + 'is_trusted', |
| 74 | + 'last_activity_at', |
| 75 | + 'is_suspicious', |
| 76 | + 'suspicious_reason', |
| 77 | + ]; |
| 78 | + |
| 79 | + foreach ($columns as $column) { |
| 80 | + if (Schema::hasColumn($tableName, $column)) { |
| 81 | + $table->dropColumn($column); |
| 82 | + } |
| 83 | + } |
| 84 | + }); |
| 85 | + } |
| 86 | +}; |
| 87 | + |
0 commit comments