diff --git a/database/migrations/2026_05_22_175608_change_duration_to_integer_in_music_table.php b/database/migrations/2026_05_22_175608_change_duration_to_integer_in_music_table.php index d6ebc11..15a7fc4 100644 --- a/database/migrations/2026_05_22_175608_change_duration_to_integer_in_music_table.php +++ b/database/migrations/2026_05_22_175608_change_duration_to_integer_in_music_table.php @@ -3,6 +3,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Facades\DB; return new class extends Migration { @@ -11,13 +12,22 @@ */ public function up(): void { - // For PostgreSQL, we need to use a raw statement with USING clause - DB::statement('ALTER TABLE music ALTER COLUMN duration TYPE integer USING (duration::integer)'); + // Get the database driver + $driver = DB::connection()->getDriverName(); - // Then make it nullable if needed - Schema::table('music', function (Blueprint $table) { - $table->integer('duration')->nullable()->change(); - }); + if ($driver === 'pgsql') { + // PostgreSQL: Use raw statement with USING clause + DB::statement('ALTER TABLE music ALTER COLUMN duration TYPE integer USING (duration::integer)'); + + Schema::table('music', function (Blueprint $table) { + $table->integer('duration')->nullable()->change(); + }); + } elseif ($driver === 'mysql') { + // MySQL: Can directly change column type + Schema::table('music', function (Blueprint $table) { + $table->integer('duration')->nullable()->change(); + }); + } } /** @@ -25,11 +35,21 @@ public function up(): void */ public function down(): void { - // Convert back to string/text - DB::statement('ALTER TABLE music ALTER COLUMN duration TYPE text USING (duration::text)'); + // Get the database driver + $driver = DB::connection()->getDriverName(); - Schema::table('music', function (Blueprint $table) { - $table->string('duration')->nullable()->change(); - }); + if ($driver === 'pgsql') { + // PostgreSQL: Convert back to text + DB::statement('ALTER TABLE music ALTER COLUMN duration TYPE text USING (duration::text)'); + + Schema::table('music', function (Blueprint $table) { + $table->string('duration')->nullable()->change(); + }); + } elseif ($driver === 'mysql') { + // MySQL: Change back to string + Schema::table('music', function (Blueprint $table) { + $table->string('duration')->nullable()->change(); + }); + } } }; \ No newline at end of file