Skip to content

[Refactor] Add indexes to personal_access_tokens#571

Merged
taylorotwell merged 2 commits intolaravel:4.xfrom
keshav-k3:feature-test-keshav
Jul 9, 2025
Merged

[Refactor] Add indexes to personal_access_tokens#571
taylorotwell merged 2 commits intolaravel:4.xfrom
keshav-k3:feature-test-keshav

Conversation

@keshav-k3
Copy link
Contributor

This PR adds database indexes to the expires_at and last_used_at columns in the personal_access_tokens table migration.

Benefit to end users

Improves query performance for token expiration and pruning, especially at scale. Keeps token management fast and efficient as data grows.

Why it’s safe

The change is backward-compatible and does not modify any existing data or application logic.
Indexes are a standard optimization

Testing

Existing tests for token creation, expiration, and pruning continue to pass.

@taylorotwell taylorotwell merged commit fd6df4f into laravel:4.x Jul 9, 2025
8 checks passed
@patrickomeara
Copy link
Contributor

Hopefully this saves someone even a tiny bit of time.

<?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('personal_access_tokens', function (Blueprint $table) {
            $table->index('expires_at');
        });
    }
};

@kylemilloy
Copy link

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('personal_access_tokens', function (Blueprint $table) {
            $table->dropIndex(['expires_at']);
        });
    }

And for the down.

@wsamoht
Copy link

wsamoht commented Aug 20, 2025

Just reviewing changes doing a composer update in our app and curious why Taylor removed the index on last_used_at 🤔 Maybe he just didn't want it as a default? Obviously we can still add it ourselves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants