Skip to content

Commit ff252a5

Browse files
committed
Added MySQL event to automatically delete expired tokens
- Added a new migration to create a MySQL event named 'delete_expired_tokens'. - The event is scheduled to run every day and removes tokens from the 'personal_access_tokens' table where the 'expires_at' column is less than the current time.
1 parent 14506ff commit ff252a5

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
/**
10+
* Run the migrations.
11+
*/
12+
public function up()
13+
{
14+
// Tạo sự kiện xóa token hết hạn
15+
DB::statement("
16+
CREATE EVENT IF NOT EXISTS delete_expired_tokens
17+
ON SCHEDULE EVERY 1 DAY
18+
DO
19+
DELETE FROM personal_access_tokens
20+
WHERE expires_at IS NOT NULL AND expires_at < NOW();
21+
");
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*/
27+
public function down(): void
28+
{
29+
DB::statement("DROP EVENT IF EXISTS delete_expired_tokens");
30+
}
31+
};

0 commit comments

Comments
 (0)