<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        $tables = ['students', 'teachers', 'users'];
        foreach ($tables as $table) {
            if (Schema::hasTable($table) && DB::getDriverName() !== 'sqlite') {
                DB::statement("ALTER TABLE {$table} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
            }
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // 原排序规则未知，回滚时仅保持 utf8mb4_general_ci 作为保底值。
        $tables = ['students', 'teachers', 'users'];
        foreach ($tables as $table) {
            if (Schema::hasTable($table) && DB::getDriverName() !== 'sqlite') {
                DB::statement("ALTER TABLE {$table} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci");
            }
        }
    }
};
