Skip to content

Commit 95a723d

Browse files
authored
Fixes MariaDB 11 migration issues (#4488)
1 parent 28866f1 commit 95a723d

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

database/migrations/2026_07_01_120000_deduplicate_and_constrain_access_permissions.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
use Illuminate\Support\Facades\DB;
1414
use Illuminate\Support\Facades\Schema;
1515

16+
require_once 'TemporaryModels/OptimizeTables.php';
17+
1618
return new class() extends Migration {
19+
private OptimizeTables $optimize;
20+
1721
private const TABLE_NAME = 'access_permissions';
1822

1923
private const BASE_ALBUM_ID = 'base_album_id';
@@ -32,6 +36,11 @@
3236
// four column names) exceeds MySQL's 64-character identifier limit.
3337
private const UNIQUE_INDEX_NAME = 'access_permissions_dedup_unique';
3438

39+
public function __construct()
40+
{
41+
$this->optimize = new OptimizeTables();
42+
}
43+
3544
/**
3645
* Run the migrations.
3746
*/
@@ -51,10 +60,14 @@ public function up(): void
5160
// in-place table rebuild, which InnoDB rejects with error 1215
5261
// ("Cannot add foreign key constraint"). Drop it here and
5362
// recreate it once the new columns are in place.
54-
$table->dropForeign([self::USER_ID]);
55-
$table->dropUnique([self::BASE_ALBUM_ID, self::USER_ID]);
56-
$table->unsignedInteger(self::USER_ID_UNIQUE_KEY)->nullable(false)->storedAs('COALESCE(' . self::USER_ID . ', 0)');
57-
$table->unsignedInteger(self::USER_GROUP_ID_UNIQUE_KEY)->nullable(false)->storedAs('COALESCE(' . self::USER_GROUP_ID . ', 0)');
63+
$this->optimize->dropForeignIfExists($table, 'access_permissions_user_id_foreign');
64+
$this->optimize->dropUniqueIfExists($table, 'access_permissions_base_album_id_user_id_unique');
65+
// No ->nullable(false): MariaDB's grammar rejects an explicit NOT
66+
// NULL clause on generated columns entirely (even in CREATE TABLE),
67+
// so Laravel must omit it here. COALESCE already guarantees the
68+
// stored value is never actually NULL.
69+
$table->unsignedInteger(self::USER_ID_UNIQUE_KEY)->storedAs('COALESCE(' . self::USER_ID . ', 0)');
70+
$table->unsignedInteger(self::USER_GROUP_ID_UNIQUE_KEY)->storedAs('COALESCE(' . self::USER_GROUP_ID . ', 0)');
5871
});
5972

6073
Schema::table(self::TABLE_NAME, function (Blueprint $table) {
@@ -76,7 +89,7 @@ public function down(): void
7689
{
7790
Schema::table(self::TABLE_NAME, function (Blueprint $table) {
7891
$table->dropForeign([self::USER_ID]);
79-
$table->dropUnique(self::UNIQUE_INDEX_NAME);
92+
$this->optimize->dropUniqueIfExists($table, self::UNIQUE_INDEX_NAME);
8093
$table->dropColumn([self::USER_ID_UNIQUE_KEY, self::USER_GROUP_ID_UNIQUE_KEY]);
8194
});
8295

0 commit comments

Comments
 (0)