Skip to content

Commit 277b095

Browse files
FrittenKeeZjasonvargaryanmitchell
authored
Replaced foreign keys with simple indexes (#100)
Co-authored-by: Jason Varga <[email protected]> Co-authored-by: Ryan Mitchell <[email protected]>
1 parent 47934d7 commit 277b095

7 files changed

+85
-14
lines changed

database/migrations/create_entries_table.php.stub

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,17 @@ return new class extends Migration {
1010
Schema::create($this->prefix('entries'), function (Blueprint $table) {
1111
$table->id();
1212
$table->string('site')->index();
13-
$table->unsignedBigInteger('origin_id')->nullable();
13+
$table->unsignedBigInteger('origin_id')->nullable()->index();
1414
$table->boolean('published')->default(true);
1515
$table->string('status');
1616
$table->string('slug')->nullable();
1717
$table->string('uri')->nullable()->index();
1818
$table->string('date')->nullable();
19-
$table->integer('order')->nullable();
19+
$table->integer('order')->nullable()->index();
2020
$table->string('collection')->index();
2121
$table->string('blueprint', 30)->nullable()->index();
2222
$table->jsonb('data');
2323
$table->timestamps();
24-
25-
$table->foreign('origin_id')
26-
->references('id')
27-
->on($this->prefix('entries'))
28-
->onDelete('set null');
2924
});
3025
}
3126

database/migrations/create_entries_table_with_string_ids.php.stub

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,19 @@ return new class extends Migration {
1010
Schema::create($this->prefix('entries'), function (Blueprint $table) {
1111
$table->uuid('id');
1212
$table->string('site')->index();
13-
$table->uuid('origin_id')->nullable();
13+
$table->uuid('origin_id')->nullable()->index();
1414
$table->boolean('published')->default(true);
1515
$table->string('status');
1616
$table->string('slug')->nullable();
1717
$table->string('uri')->nullable()->index();
1818
$table->string('date')->nullable();
19-
$table->integer('order')->nullable();
19+
$table->integer('order')->nullable()->index();
2020
$table->string('collection')->index();
2121
$table->string('blueprint', 30)->nullable()->index();
2222
$table->jsonb('data');
2323
$table->timestamps();
2424

2525
$table->primary('id');
26-
$table->foreign('origin_id')
27-
->references('id')
28-
->on($this->prefix('entries'))
29-
->onDelete('set null');
3026
});
3127
}
3228

database/migrations/create_form_submissions_table.php.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ return new class extends Migration {
99
{
1010
Schema::create($this->prefix('form_submissions'), function (Blueprint $table) {
1111
$table->id();
12-
$table->foreignId('form_id')->constrained($this->prefix('forms'))->cascadeOnDelete();
12+
$table->unsignedBigInteger('form_id')->index();
1313
$table->jsonb('data')->nullable();
1414
$table->timestamps(6);
1515

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
Schema::table($this->prefix('entries'), function (Blueprint $table) {
11+
$table->dropForeign(['origin_id']);
12+
$table->index('order');
13+
});
14+
}
15+
16+
public function down()
17+
{
18+
Schema::table($this->prefix('entries'), function (Blueprint $table) {
19+
$table->foreign('origin_id')
20+
->references('id')
21+
->on($this->prefix('entries'))
22+
->onDelete('set null');
23+
$table->dropIndex(['order']);
24+
});
25+
}
26+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
Schema::table($this->prefix('form_submissions'), function (Blueprint $table) {
11+
$table->dropForeign(['origin_id']);
12+
});
13+
}
14+
15+
public function down()
16+
{
17+
Schema::table($this->prefix('form_submissions'), function (Blueprint $table) {
18+
$table->foreign('form_id')
19+
->references('id')
20+
->on($this->prefix('forms'))
21+
->onDelete('cascade');
22+
});
23+
}
24+
};

src/ServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class ServiceProvider extends AddonServiceProvider
4242
protected $updateScripts = [
4343
\Statamic\Eloquent\Updates\AddOrderToEntriesTable::class,
4444
\Statamic\Eloquent\Updates\AddBlueprintToEntriesTable::class,
45+
\Statamic\Eloquent\Updates\DropForeignKeysOnEntriesAndForms::class,
4546
];
4647

4748
protected $listen = [
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Statamic\Eloquent\Updates;
4+
5+
use Statamic\UpdateScripts\UpdateScript;
6+
7+
class DropForeignKeysOnEntriesAndForms extends UpdateScript
8+
{
9+
public function shouldUpdate($newVersion, $oldVersion)
10+
{
11+
return $this->isUpdatingTo('2.3.0');
12+
}
13+
14+
public function update()
15+
{
16+
$source = __DIR__.'/../../database/migrations/updates/drop_foreign_keys_on_entries.php.stub';
17+
$dest = database_path('migrations/'.date('Y_m_d_His').'_drop_foreign_keys_on_entries.php');
18+
19+
$this->files->copy($source, $dest);
20+
21+
$source = __DIR__.'/../../database/migrations/updates/drop_foreign_keys_on_forms.php.stub';
22+
$dest = database_path('migrations/'.date('Y_m_d_His').'_drop_foreign_keys_on_forms.php');
23+
24+
$this->files->copy($source, $dest);
25+
26+
$this->console()->info('Migrations created');
27+
$this->console()->comment('Remember to run `php artisan migrate` to apply it to your database.');
28+
}
29+
}

0 commit comments

Comments
 (0)