Convert from utf8mb3 to utf8mb4

This commit is contained in:
DariusIII
2025-11-05 13:42:31 +01:00
parent f66723251a
commit 1eac7bee4b
3 changed files with 110 additions and 0 deletions
@@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
// Fix collation for release_files table to use utf8mb4_unicode_ci
DB::statement('ALTER TABLE release_files CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci');
// Explicitly update the varchar columns to ensure they use utf8mb4_unicode_ci
DB::statement('ALTER TABLE release_files MODIFY COLUMN name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL');
DB::statement('ALTER TABLE release_files MODIFY COLUMN crc32 VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT \'\'');
}
/**
* Reverse the migrations.
*/
public function down(): void
{
// Revert to utf8mb3_unicode_ci (only if you really need to rollback)
DB::statement('ALTER TABLE release_files CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci');
DB::statement('ALTER TABLE release_files MODIFY COLUMN name VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL');
DB::statement('ALTER TABLE release_files MODIFY COLUMN crc32 VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT \'\'');
}
};
@@ -0,0 +1,46 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
// Get all tables with utf8mb3 collation and convert them to utf8mb4
$database = config('database.connections.mariadb.database');
$tables = DB::select("
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = ?
AND COLLATION_NAME LIKE 'utf8mb3%'
ORDER BY TABLE_NAME
", [$database]);
foreach ($tables as $table) {
$tableName = $table->TABLE_NAME;
echo "Converting table: {$tableName}\n";
try {
// Convert table to utf8mb4
DB::statement("ALTER TABLE `{$tableName}` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
} catch (\Exception $e) {
echo "Warning: Could not convert table {$tableName}: " . $e->getMessage() . "\n";
}
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
// This is intentionally left empty as reverting to utf8mb3 is generally not recommended
// and could cause data loss for characters that require utf8mb4
}
};
@@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
// Drop the foreign key constraint
DB::statement('ALTER TABLE telescope_entries_tags DROP FOREIGN KEY telescope_entries_tags_entry_uuid_foreign');
// Convert both tables to utf8mb4
DB::statement('ALTER TABLE telescope_entries CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci');
DB::statement('ALTER TABLE telescope_entries_tags CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci');
// Re-add the foreign key constraint
DB::statement('ALTER TABLE telescope_entries_tags ADD CONSTRAINT telescope_entries_tags_entry_uuid_foreign FOREIGN KEY (entry_uuid) REFERENCES telescope_entries(uuid) ON DELETE CASCADE');
}
/**
* Reverse the migrations.
*/
public function down(): void
{
// This is intentionally left empty as reverting is not recommended
}
};