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(); +});