fix: key constraint prevents migration (closes #1725)

This commit is contained in:
Phan An 2023-12-28 13:18:26 +01:00
parent 033c234928
commit b12b6e03b4

View file

@ -11,27 +11,27 @@ return new class extends Migration
{ {
public function up(): void public function up(): void
{ {
Schema::disableForeignKeyConstraints();
if (DB::getDriverName() !== 'sqlite') {
collect(['playlist_song', 'interactions'])->each(static function (string $table): void {
Schema::table($table, static function (Blueprint $table): void {
$table->dropForeign(['song_id']);
});
});
}
Schema::table('songs', static function (Blueprint $table): void { Schema::table('songs', static function (Blueprint $table): void {
$table->string('id', 36)->change(); $table->string('id', 36)->change();
}); });
Schema::table('playlist_song', static function (Blueprint $table): void { Schema::table('playlist_song', static function (Blueprint $table): void {
$table->string('song_id', 36)->change(); $table->string('song_id', 36)->change();
if (DB::getDriverName() !== 'sqlite') {
$table->dropForeign('playlist_song_song_id_foreign');
}
$table->foreign('song_id')->references('id')->on('songs')->cascadeOnDelete()->cascadeOnUpdate(); $table->foreign('song_id')->references('id')->on('songs')->cascadeOnDelete()->cascadeOnUpdate();
}); });
Schema::table('interactions', static function (Blueprint $table): void { Schema::table('interactions', static function (Blueprint $table): void {
$table->string('song_id', 36)->change(); $table->string('song_id', 36)->change();
if (DB::getDriverName() !== 'sqlite') {
$table->dropForeign('interactions_song_id_foreign');
}
$table->foreign('song_id')->references('id')->on('songs')->cascadeOnDelete()->cascadeOnUpdate(); $table->foreign('song_id')->references('id')->on('songs')->cascadeOnDelete()->cascadeOnUpdate();
}); });
@ -39,5 +39,7 @@ return new class extends Migration
$song->id = Str::uuid(); $song->id = Str::uuid();
$song->save(); $song->save();
}); });
Schema::enableForeignKeyConstraints();
} }
}; };