From 31c77e11af1789cc102d15d7cc388604e813d160 Mon Sep 17 00:00:00 2001 From: Craig Potter Date: Sat, 18 Jul 2026 09:30:43 +0100 Subject: [PATCH] Index barstools.created_at Pruning (WHERE created_at <= ?) and recent-first browsing queries were scanning the table, which grows by design. Adds the index to the create stub for new installs plus an upgrade migration guarded with hasIndex so it is a no-op where the index already exists. --- ...eated_at_index_to_barstools_table.php.stub | 33 +++++++++++++++++++ .../create_barstools_table.php.stub | 2 +- src/BarstoolServiceProvider.php | 6 +++- tests/BarstoolTest.php | 10 ++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 database/migrations/add_created_at_index_to_barstools_table.php.stub diff --git a/database/migrations/add_created_at_index_to_barstools_table.php.stub b/database/migrations/add_created_at_index_to_barstools_table.php.stub new file mode 100644 index 0000000..17ac016 --- /dev/null +++ b/database/migrations/add_created_at_index_to_barstools_table.php.stub @@ -0,0 +1,33 @@ +getConnection()); + + if ($schema->hasIndex('barstools', 'barstools_created_at_index')) { + return; + } + + $schema->table('barstools', function (Blueprint $table) { + $table->index('created_at'); + }); + } +}; diff --git a/database/migrations/create_barstools_table.php.stub b/database/migrations/create_barstools_table.php.stub index 01efe03..99b83d5 100644 --- a/database/migrations/create_barstools_table.php.stub +++ b/database/migrations/create_barstools_table.php.stub @@ -25,7 +25,7 @@ return new class extends Migration $schema->create('barstools', function (Blueprint $table) { $table->id(); $table->uuid('uuid')->index(); - $table->timestamp('created_at')->useCurrent(); + $table->timestamp('created_at')->useCurrent()->index(); $table->string('connector_class')->index(); $table->string('request_class')->index(); $table->string('method'); diff --git a/src/BarstoolServiceProvider.php b/src/BarstoolServiceProvider.php index 310a02d..0e708f8 100644 --- a/src/BarstoolServiceProvider.php +++ b/src/BarstoolServiceProvider.php @@ -20,7 +20,11 @@ public function configurePackage(Package $package): void ->name('barstool') ->hasConfigFile() ->hasViews() - ->hasMigrations(['create_barstools_table', 'add_context_to_barstools_table']); + ->hasMigrations([ + 'create_barstools_table', + 'add_context_to_barstools_table', + 'add_created_at_index_to_barstools_table', + ]); } public function packageRegistered(): void diff --git a/tests/BarstoolTest.php b/tests/BarstoolTest.php index 5eb886e..14c5ff3 100644 --- a/tests/BarstoolTest.php +++ b/tests/BarstoolTest.php @@ -8,6 +8,7 @@ use Saloon\Barstool\Models\Barstool; use Saloon\Http\Faking\MockResponse; use Illuminate\Support\Facades\Queue; +use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Artisan; use Saloon\Barstool\Enums\RecordingType; use Saloon\Http\Connectors\NullConnector; @@ -1161,3 +1162,12 @@ NullConnector::class, SoloUserRequest::class, ]); + +it('adds the created_at index via the upgrade migration', function () { + $migration = include __DIR__.'/../database/migrations/add_created_at_index_to_barstools_table.php.stub'; + $migration->up(); + + $schema = Schema::connection(config('barstool.connection')); + + expect($schema->hasIndex('barstools', 'barstools_created_at_index'))->toBeTrue(); +});