Skip to content

Commit 2c85ac6

Browse files
afonicryanmitchell
andauthored
Add index to the date column (#429)
* Add index to the date column * Add migration for existing installs * Only run migration if we have entries table * Correct version number --------- Co-authored-by: Ryan Mitchell <[email protected]>
1 parent 7c4fb13 commit 2c85ac6

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

database/migrations/entries/2024_03_07_100000_create_entries_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function up()
1515
$table->boolean('published')->default(true);
1616
$table->string('slug')->nullable();
1717
$table->string('uri')->nullable()->index();
18-
$table->string('date')->nullable();
18+
$table->string('date')->nullable()->index();
1919
$table->integer('order')->nullable()->index();
2020
$table->string('collection')->index();
2121
$table->string('blueprint', 30)->nullable()->index();

database/migrations/entries/2024_03_07_100000_create_entries_table_with_string_ids.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function up()
1515
$table->boolean('published')->default(true);
1616
$table->string('slug')->nullable();
1717
$table->string('uri')->nullable()->index();
18-
$table->string('date')->nullable();
18+
$table->string('date')->nullable()->index();
1919
$table->integer('order')->nullable()->index();
2020
$table->string('collection')->index();
2121
$table->string('blueprint', 30)->nullable()->index();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Support\Facades\Schema;
5+
use Statamic\Eloquent\Database\BaseMigration as Migration;
6+
7+
return new class extends Migration {
8+
public function up()
9+
{
10+
if (Schema::hasIndex($this->prefix('entries'), 'date')) {
11+
return;
12+
}
13+
14+
Schema::table($this->prefix('entries'), function (Blueprint $table) {
15+
$table->index(['date']);
16+
});
17+
}
18+
19+
public function down()
20+
{
21+
if (! Schema::hasIndex($this->prefix('entries'), 'date')) {
22+
return;
23+
}
24+
25+
Schema::table($this->prefix('entries'), function (Blueprint $table) {
26+
$table->dropIndex(['date']);
27+
});
28+
}
29+
};

src/ServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class ServiceProvider extends AddonServiceProvider
5757
\Statamic\Eloquent\Updates\RelateFormSubmissionsByHandle::class,
5858
\Statamic\Eloquent\Updates\DropStatusOnEntries::class,
5959
\Statamic\Eloquent\Updates\ChangeFormSubmissionsIdType::class,
60+
\Statamic\Eloquent\Updates\AddIndexToDateOnEntriesTable::class,
6061
];
6162

6263
public function boot()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Statamic\Eloquent\Updates;
4+
5+
use Illuminate\Support\Facades\Schema;
6+
use Statamic\UpdateScripts\UpdateScript;
7+
8+
class AddIndexToDateOnEntriesTable extends UpdateScript
9+
{
10+
public function shouldUpdate($newVersion, $oldVersion)
11+
{
12+
return $this->isUpdatingTo('4.22.0')
13+
&& Schema::hasTable(config('statamic.eloquent-driver.table_prefix', '').'entries')
14+
&& ! Schema::hasIndex(config('statamic.eloquent-driver.table_prefix', '').'entries', 'date');
15+
}
16+
17+
public function update()
18+
{
19+
$source = __DIR__.'/../../database/migrations/updates/add_index_to_date_on_entries_table.php.stub';
20+
$dest = database_path('migrations/'.date('Y_m_d_His').'_add_index_to_date_on_entries_table.php');
21+
22+
$this->files->copy($source, $dest);
23+
24+
$this->console()->info('Migration created');
25+
$this->console()->comment('Remember to run `php artisan migrate` to apply it to your database.');
26+
}
27+
}

0 commit comments

Comments
 (0)