fix: use proper integer cast type per DB driver

This commit is contained in:
Phan An 2022-08-10 09:00:54 +02:00
parent bbbf270965
commit 1a35a7df3c
No known key found for this signature in database
GPG key ID: A81E4477F0BB6FDC
5 changed files with 33 additions and 7 deletions

View file

@ -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');
}

View file

@ -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')

View 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',
};
});
}
}

View file

@ -145,6 +145,7 @@ return [
App\Providers\ITunesServiceProvider::class,
App\Providers\StreamerServiceProvider::class,
App\Providers\ObjectStorageServiceProvider::class,
App\Providers\MacroProvider::class,
],
/*

View file

@ -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();