diff --git a/database/migrations/2024_04_14_082519_create_auth_logs_table.php b/database/migrations/2024_04_14_082519_create_auth_logs_table.php index bc3f9268..11347549 100644 --- a/database/migrations/2024_04_14_082519_create_auth_logs_table.php +++ b/database/migrations/2024_04_14_082519_create_auth_logs_table.php @@ -41,11 +41,20 @@ public function up(): void $table->boolean('cleared_by_user')->default(false); $table->string('guard', 40)->nullable(); $table->string('login_method', 40)->nullable(); + + $table->foreign('authenticatable_id')->references('id')->on('users')->cascadeOnDelete(); }); } public function down(): void { + Schema::whenTableHasColumn('auth_logs', 'authenticatable_id', function (Blueprint $table) { + // cannot drop foreign keys in SQLite: + if (DB::getDriverName() !== 'sqlite') { + $table->dropForeign(['authenticatable_id']); + } + }); + Schema::dropIfExists('auth_logs'); } }; diff --git a/tests/Feature/Models/UserModelTest.php b/tests/Feature/Models/UserModelTest.php index 935cf33a..b779a3f2 100644 --- a/tests/Feature/Models/UserModelTest.php +++ b/tests/Feature/Models/UserModelTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature\Models; +use App\Models\AuthLog; use App\Models\Group; use App\Models\TwoFAccount; use App\Models\User; @@ -113,6 +114,7 @@ public function test_delete_removes_user_data() { $user = User::factory()->create(); TwoFAccount::factory()->for($user)->create(); + AuthLog::factory()->for($user, 'authenticatable')->create(); Group::factory()->for($user)->create(); DB::table('webauthn_credentials')->insert([ @@ -154,6 +156,9 @@ public function test_delete_removes_user_data() $this->assertDatabaseMissing(config('auth.passwords.users.table'), [ 'email' => $user->email, ]); + $this->assertDatabaseMissing('auth_logs', [ + 'authenticatable_id' => $user->id, + ]); } /**