forked from koel/koel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2016_04_16_082627_create_various_artists.php
54 lines (45 loc) · 1.35 KB
/
2016_04_16_082627_create_various_artists.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
use App\Models\Artist;
use Illuminate\Database\Migrations\Migration;
class CreateVariousArtists extends Migration
{
/**
* Create the "Various Artists".
*
* @return void
*/
public function up()
{
// Make sure modified artists cascade the album's artist_id field.
Schema::table('albums', function ($table) {
$table->dropForeign('albums_artist_id_foreign');
$table->foreign('artist_id')->references('id')->on('artists')->onUpdate('cascade')->onDelete('cascade');
});
Artist::unguard();
$existingArtist = Artist::find(Artist::VARIOUS_ID);
if ($existingArtist) {
if ($existingArtist->name === Artist::VARIOUS_NAME) {
goto ret;
}
// There's an existing artist with that special ID, but it's not our Various Artist
// We move it to the end of the table.
$latestArtist = Artist::orderBy('id', 'DESC')->first();
$existingArtist->id = $latestArtist->id + 1;
$existingArtist->save();
}
Artist::create([
'id' => Artist::VARIOUS_ID,
'name' => Artist::VARIOUS_NAME,
]);
ret:
Artist::reguard();
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}