From 70047fe20efd43e2256ba2de4b53eb54abe0a1a7 Mon Sep 17 00:00:00 2001
From: Deluan <deluan@deluan.com>
Date: Fri, 8 May 2020 09:50:33 -0400
Subject: [PATCH] Add `songCount` column to Artist table

---
 .../20200508093059_add_artist_song_count.go   | 27 +++++++++++++++++++
 model/artist.go                               |  3 ++-
 persistence/artist_repository.go              |  4 +--
 ui/src/artist/ArtistList.js                   |  1 +
 ui/src/i18n/en.json                           |  3 ++-
 5 files changed, 34 insertions(+), 4 deletions(-)
 create mode 100644 db/migration/20200508093059_add_artist_song_count.go

diff --git a/db/migration/20200508093059_add_artist_song_count.go b/db/migration/20200508093059_add_artist_song_count.go
new file mode 100644
index 000000000..29d2f0ed2
--- /dev/null
+++ b/db/migration/20200508093059_add_artist_song_count.go
@@ -0,0 +1,27 @@
+package migration
+
+import (
+	"database/sql"
+
+	"github.com/pressly/goose"
+)
+
+func init() {
+	goose.AddMigration(Up20200508093059, Down20200508093059)
+}
+
+func Up20200508093059(tx *sql.Tx) error {
+	_, err := tx.Exec(`
+alter table artist
+	add song_count integer default 0 not null;
+`)
+	if err != nil {
+		return err
+	}
+	notice(tx, "A full rescan will be performed to calculate artists' song counts")
+	return forceFullRescan(tx)
+}
+
+func Down20200508093059(tx *sql.Tx) error {
+	return nil
+}
diff --git a/model/artist.go b/model/artist.go
index 123e9c19d..76f134902 100644
--- a/model/artist.go
+++ b/model/artist.go
@@ -5,7 +5,8 @@ import "time"
 type Artist struct {
 	ID              string `json:"id"          orm:"column(id)"`
 	Name            string `json:"name"`
-	AlbumCount      int    `json:"albumCount"  orm:"column(album_count)"`
+	AlbumCount      int    `json:"albumCount"`
+	SongCount       int    `json:"songCount"`
 	FullText        string `json:"fullText"`
 	SortArtistName  string `json:"sortArtistName"`
 	OrderArtistName string `json:"orderArtistName"`
diff --git a/persistence/artist_repository.go b/persistence/artist_repository.go
index 126b73324..3305f70e6 100644
--- a/persistence/artist_repository.go
+++ b/persistence/artist_repository.go
@@ -114,8 +114,8 @@ func (r *artistRepository) Refresh(ids ...string) error {
 	}
 	var artists []refreshArtist
 	sel := Select("f.album_artist_id as id", "f.album_artist as name", "count(*) as album_count", "a.id as current_id",
-		"f.sort_album_artist_name as sort_artist_name",
-		"f.order_album_artist_name as order_artist_name").
+		"f.sort_album_artist_name as sort_artist_name", "f.order_album_artist_name as order_artist_name",
+		"sum(f.song_count) as song_count").
 		From("album f").
 		LeftJoin("artist a on f.album_artist_id = a.id").
 		Where(Eq{"f.album_artist_id": ids}).
diff --git a/ui/src/artist/ArtistList.js b/ui/src/artist/ArtistList.js
index f7f64aba6..fecd8c61e 100644
--- a/ui/src/artist/ArtistList.js
+++ b/ui/src/artist/ArtistList.js
@@ -38,6 +38,7 @@ const ArtistList = (props) => (
     <Datagrid rowClick={artistRowClick}>
       <TextField source="name" />
       <NumberField source="albumCount" />
+      <NumberField source="songCount" />
     </Datagrid>
   </List>
 )
diff --git a/ui/src/i18n/en.json b/ui/src/i18n/en.json
index 43de5b22c..7e11e7a35 100644
--- a/ui/src/i18n/en.json
+++ b/ui/src/i18n/en.json
@@ -49,7 +49,8 @@
             "name": "Artist |||| Artists",
             "fields": {
                 "name": "Name",
-                "albumCount": "Album Count"
+                "albumCount": "Album Count",
+                "songCount": "Song Count"
             }
         },
         "user": {