From cd4d9e5f3615d64ccf93110ebf83965e0b51c381 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 11 Sep 2026 14:12:16 +0930 Subject: [PATCH] Fix index migrations to support PostgreSQL and MariaDB The three recent index migrations used raw MySQL-only DDL (backtick identifiers, ADD INDEX inside ALTER TABLE, ALGORITHM=INPLACE/LOCK=NONE) guarded only against sqlite, so PostgreSQL instances failed with SQLSTATE[42601] on migrate (#7177). Each migration now branches on the driver: - mysql/mariadb keep the online-DDL fast path (non-blocking on large instances) - other drivers use the portable Schema::table builder Table names, index names, and columns are unchanged so already-migrated MySQL instances are unaffected. --- ...profile_idindex_to_notifications_table.php | 56 +++++++++++++------ ...26_09_07_080905_add_media_delete_index.php | 53 +++++++++++++----- ..._add_user_id_size_index_to_media_table.php | 53 ++++++++++++------ 3 files changed, 113 insertions(+), 49 deletions(-) diff --git a/database/migrations/2026_09_07_071046_add_deleted_at_profile_idindex_to_notifications_table.php b/database/migrations/2026_09_07_071046_add_deleted_at_profile_idindex_to_notifications_table.php index 656329ddc..ec4e33efd 100644 --- a/database/migrations/2026_09_07_071046_add_deleted_at_profile_idindex_to_notifications_table.php +++ b/database/migrations/2026_09_07_071046_add_deleted_at_profile_idindex_to_notifications_table.php @@ -1,39 +1,59 @@ columns) + ->map(fn ($column) => "`{$column}`") + ->implode(', '); + + DB::statement(" + ALTER TABLE `{$this->table}` + ADD INDEX `{$this->index}` ({$columns}), + ALGORITHM=INPLACE, + LOCK=NONE + "); + return; } - DB::statement(' - ALTER TABLE `notifications` - ADD INDEX `notifications_profile_deleted_id_index` - (`profile_id`, `deleted_at`, `id`), - ALGORITHM=INPLACE, - LOCK=NONE - '); + Schema::table($this->table, function (Blueprint $table) { + $table->index($this->columns, $this->index); + }); } public function down(): void { - if (DB::getDriverName() === 'sqlite') { + if (in_array(DB::getDriverName(), ['mysql', 'mariadb'], true)) { + DB::statement(" + ALTER TABLE `{$this->table}` + DROP INDEX `{$this->index}`, + ALGORITHM=INPLACE, + LOCK=NONE + "); + return; } - DB::statement(' - ALTER TABLE `notifications` - DROP INDEX `notifications_profile_deleted_id_index`, - ALGORITHM=INPLACE, - LOCK=NONE - '); + Schema::table($this->table, function (Blueprint $table) { + $table->dropIndex($this->index); + }); } }; diff --git a/database/migrations/2026_09_07_080905_add_media_delete_index.php b/database/migrations/2026_09_07_080905_add_media_delete_index.php index 85365329b..4a10e8fca 100644 --- a/database/migrations/2026_09_07_080905_add_media_delete_index.php +++ b/database/migrations/2026_09_07_080905_add_media_delete_index.php @@ -1,36 +1,59 @@ columns) + ->map(fn ($column) => "`{$column}`") + ->implode(', '); + + DB::statement(" + ALTER TABLE `{$this->table}` + ADD INDEX `{$this->index}` ({$columns}), + ALGORITHM=INPLACE, + LOCK=NONE + "); + return; } - DB::statement(' - ALTER TABLE `media` - ADD INDEX `media_unoptimized_recent_index` - (`processed_at`, `remote_url`, `deleted_at`, `created_at`, `id`), - ALGORITHM=INPLACE, - LOCK=NONE - '); + Schema::table($this->table, function (Blueprint $table) { + $table->index($this->columns, $this->index); + }); } public function down(): void { - if (DB::getDriverName() === 'sqlite') { + if (in_array(DB::getDriverName(), ['mysql', 'mariadb'], true)) { + DB::statement(" + ALTER TABLE `{$this->table}` + DROP INDEX `{$this->index}`, + ALGORITHM=INPLACE, + LOCK=NONE + "); + return; } - DB::statement(' - ALTER TABLE `media` - DROP INDEX `media_unoptimized_recent_index`, - ALGORITHM=INPLACE, - LOCK=NONE - '); + Schema::table($this->table, function (Blueprint $table) { + $table->dropIndex($this->index); + }); } }; diff --git a/database/migrations/2026_09_10_000000_add_user_id_size_index_to_media_table.php b/database/migrations/2026_09_10_000000_add_user_id_size_index_to_media_table.php index 8b03d359b..5f7af529d 100644 --- a/database/migrations/2026_09_10_000000_add_user_id_size_index_to_media_table.php +++ b/database/migrations/2026_09_10_000000_add_user_id_size_index_to_media_table.php @@ -1,44 +1,65 @@ columns) + ->map(fn ($column) => "`{$column}`") + ->implode(', '); + + DB::statement(" + ALTER TABLE `{$this->table}` + ADD INDEX `{$this->index}` ({$columns}), + ALGORITHM=INPLACE, + LOCK=NONE + "); + return; } - DB::statement(' - ALTER TABLE `media` - ADD INDEX `media_user_id_size_index` (`user_id`, `size`), - ALGORITHM=INPLACE, - LOCK=NONE - '); + Schema::table($this->table, function (Blueprint $table) { + $table->index($this->columns, $this->index); + }); } public function down(): void { - if (DB::getDriverName() === 'sqlite') { + if (in_array(DB::getDriverName(), ['mysql', 'mariadb'], true)) { + DB::statement(" + ALTER TABLE `{$this->table}` + DROP INDEX `{$this->index}`, + ALGORITHM=INPLACE, + LOCK=NONE + "); + return; } - DB::statement(' - ALTER TABLE `media` - DROP INDEX `media_user_id_size_index`, - ALGORITHM=INPLACE, - LOCK=NONE - '); + Schema::table($this->table, function (Blueprint $table) { + $table->dropIndex($this->index); + }); } };