mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
fix: use proper integer cast type per DB driver
This commit is contained in:
parent
bbbf270965
commit
1a35a7df3c
5 changed files with 33 additions and 7 deletions
|
@ -17,16 +17,15 @@ class AlbumBuilder extends Builder
|
|||
|
||||
public function withMeta(User $user): static
|
||||
{
|
||||
$integer = $this->integerCastType();
|
||||
|
||||
return $this->with('artist')
|
||||
->leftJoin('songs', 'albums.id', '=', 'songs.album_id')
|
||||
->leftJoin('interactions', static function (JoinClause $join) use ($user): void {
|
||||
$join->on('songs.id', '=', 'interactions.song_id')->where('interactions.user_id', $user->id);
|
||||
})
|
||||
->groupBy('albums.id')
|
||||
->select(
|
||||
'albums.*',
|
||||
DB::raw('CAST(SUM(interactions.play_count) AS UNSIGNED) AS play_count')
|
||||
)
|
||||
->select('albums.*', DB::raw("CAST(SUM(interactions.play_count) AS $integer) AS play_count"))
|
||||
->withCount('songs AS song_count')
|
||||
->withSum('songs AS length', 'length');
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@ class ArtistBuilder extends Builder
|
|||
|
||||
public function withMeta(User $user): static
|
||||
{
|
||||
$integer = $this->integerCastType();
|
||||
|
||||
return $this->leftJoin('songs', 'artists.id', '=', 'songs.artist_id')
|
||||
->leftJoin('interactions', static function (JoinClause $join) use ($user): void {
|
||||
$join->on('interactions.song_id', '=', 'songs.id')->where('interactions.user_id', $user->id);
|
||||
|
@ -24,7 +26,7 @@ class ArtistBuilder extends Builder
|
|||
->groupBy('artists.id')
|
||||
->select([
|
||||
'artists.*',
|
||||
DB::raw('CAST(SUM(interactions.play_count) AS UNSIGNED) AS play_count'),
|
||||
DB::raw("CAST(SUM(interactions.play_count) AS $integer) AS play_count"),
|
||||
DB::raw('COUNT(DISTINCT songs.album_id) AS album_count'),
|
||||
])
|
||||
->withCount('songs AS song_count')
|
||||
|
|
21
app/Providers/MacroProvider.php
Normal file
21
app/Providers/MacroProvider.php
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class MacroProvider extends ServiceProvider
|
||||
{
|
||||
public function boot(): void
|
||||
{
|
||||
Builder::macro('integerCastType', function (): string { // @phpcs:ignore
|
||||
return match (DB::getDriverName()) {
|
||||
'mysql' => 'UNSIGNED', // only newer versions of MySQL support "INTEGER"
|
||||
'sqlsrv' => 'INT',
|
||||
default => 'INTEGER',
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
|
@ -145,6 +145,7 @@ return [
|
|||
App\Providers\ITunesServiceProvider::class,
|
||||
App\Providers\StreamerServiceProvider::class,
|
||||
App\Providers\ObjectStorageServiceProvider::class,
|
||||
App\Providers\MacroProvider::class,
|
||||
],
|
||||
|
||||
/*
|
||||
|
|
|
@ -1,14 +1,17 @@
|
|||
<?php
|
||||
|
||||
use App\Models\Song;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CopyArtistToContributingArtist extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Song::with('album', 'album.artist')->get()->each(static function (Song $song): void {
|
||||
// @phpstan-ignore-line
|
||||
/** @var Collection|array<array-key, Song> $songs */
|
||||
$songs = Song::with('album', 'album.artist')->get();
|
||||
|
||||
$songs->each(static function (Song $song): void {
|
||||
if (!$song->contributing_artist_id) {
|
||||
$song->contributing_artist_id = $song->album->artist->id;
|
||||
$song->save();
|
||||
|
|
Loading…
Reference in a new issue