diff --git a/app/Console/Commands/CleanupDatabase.php b/app/Console/Commands/CleanupDatabase.php index eb65007194..e3bdee090f 100644 --- a/app/Console/Commands/CleanupDatabase.php +++ b/app/Console/Commands/CleanupDatabase.php @@ -33,7 +33,8 @@ class CleanupDatabase extends Command $this->line("Keep days: $keep_days"); $this->cleanupTable('failed_jobs', 'failed_at', now()->subDays(1)); - $this->cleanupTable('sessions', 'last_activity', now()->subDays($keep_days)->timestamp); + // A session older than its lifetime can no longer be used, so do not keep it for the retention days. + $this->cleanupTable('sessions', 'last_activity', now()->subMinutes((int) config('session.lifetime'))->timestamp); $this->cleanupTable('activity_log', 'created_at', now()->subDays($keep_days)); diff --git a/tests/Feature/CleanupDatabaseCommandTest.php b/tests/Feature/CleanupDatabaseCommandTest.php index 673e6ac9d7..f950e616d7 100644 --- a/tests/Feature/CleanupDatabaseCommandTest.php +++ b/tests/Feature/CleanupDatabaseCommandTest.php @@ -1,6 +1,7 @@ pluck('id')->sort()->values()->all()) ->toBe($this->recent->pluck('id')->sort()->values()->all()); }); + +it('deletes sessions older than the session lifetime', function () { + config(['session.lifetime' => 10080]); + $insertSession = fn (string $id, int $ageInMinutes) => DB::table('sessions')->insert([ + 'id' => $id, + 'payload' => '', + 'last_activity' => now()->subMinutes($ageInMinutes)->timestamp, + ]); + $insertSession('expired', 10080 + 60); + $insertSession('active', 10080 - 60); + + $this->artisan('cleanup:database --yes') + ->expectsOutputToContain('Delete 1 entries from sessions.') + ->assertSuccessful(); + + expect(DB::table('sessions')->pluck('id')->all())->toBe(['active']); +});