diff --git a/cmd/scanner/main.go b/cmd/scanner/main.go index 29f1699..7ee8f62 100644 --- a/cmd/scanner/main.go +++ b/cmd/scanner/main.go @@ -48,7 +48,7 @@ var ( type lastAlbum struct { coverModTime time.Time // 1st needed for cover insertion coverPath string // 2rd needed for cover insertion - id string // 3nd needed for cover insertion + id uint // 3nd needed for cover insertion } func (l *lastAlbum) isEmpty() bool { @@ -69,7 +69,7 @@ func isCover(filename string) bool { func readTags(fullPath string) (tag.Metadata, error) { trackData, err := os.Open(fullPath) if err != nil { - return nil, fmt.Errorf("when tags from disk: %v\n", err) + return nil, fmt.Errorf("when tags from disk: %v", err) } defer trackData.Close() tags, err := tag.ReadFrom(trackData) @@ -94,7 +94,7 @@ func handleFolderCompletion(fullPath string, info *godirwalk.Dirent) error { } image, err := ioutil.ReadFile(cLastAlbum.coverPath) if err != nil { - return fmt.Errorf("when reading cover: %v\n", err) + return fmt.Errorf("when reading cover: %v", err) } cover.Image = image cover.AlbumID = cLastAlbum.id @@ -103,13 +103,13 @@ func handleFolderCompletion(fullPath string, info *godirwalk.Dirent) error { return nil } -func processFile(fullPath string, info *godirwalk.Dirent) error { +func handleFile(fullPath string, info *godirwalk.Dirent) error { if info.IsDir() { return nil } stat, err := os.Stat(fullPath) if err != nil { - return fmt.Errorf("when stating file: %v\n", err) + return fmt.Errorf("when stating file: %v", err) } modTime := stat.ModTime() _, filename := path.Split(fullPath) @@ -126,12 +126,13 @@ func processFile(fullPath string, info *godirwalk.Dirent) error { Path: fullPath, } err = tx.Where(track).First(&track).Error - if !gorm.IsRecordNotFoundError(err) && !modTime.After(track.UpdatedAt) { + if !gorm.IsRecordNotFoundError(err) && + !modTime.After(track.UpdatedAt) { return nil } tags, err := readTags(fullPath) if err != nil { - return fmt.Errorf("when reading tags: %v\n", err) + return fmt.Errorf("when reading tags: %v", err) } trackNumber, totalTracks := tags.Track() discNumber, TotalDiscs := tags.Disc() @@ -184,10 +185,16 @@ func main() { &model.Track{}, &model.Cover{}, ) + // 🤫🤫🤫 + orm.Exec(` + INSERT INTO sqlite_sequence(name, seq) + SELECT 'albums', 500000 + WHERE NOT EXISTS (SELECT * FROM sqlite_sequence) + `) startTime := time.Now() tx = orm.Begin() err := godirwalk.Walk(os.Args[1], &godirwalk.Options{ - Callback: processFile, + Callback: handleFile, PostChildrenCallback: handleFolderCompletion, Unsorted: true, }) diff --git a/model/album.go b/model/album.go index c70e962..0ab8a4b 100644 --- a/model/album.go +++ b/model/album.go @@ -2,9 +2,9 @@ package model // Album represents the albums table type Album struct { - BaseWithUUID + Base Artist Artist - ArtistID string - Title string + ArtistID uint + Title string `gorm:"not null;index"` Tracks []Track } diff --git a/model/artist.go b/model/artist.go index bf68652..d012f14 100644 --- a/model/artist.go +++ b/model/artist.go @@ -2,7 +2,7 @@ package model // Artist represents the artists table type Artist struct { - BaseWithUUID + Base Albums []Album - Name string `gorm:"unique;n"` + Name string `gorm:"not null;unique_index"` } diff --git a/model/cover.go b/model/cover.go index 0ac4a8e..988ef4a 100644 --- a/model/cover.go +++ b/model/cover.go @@ -4,7 +4,7 @@ package model type Cover struct { Base Album Album - AlbumID string + AlbumID uint Image []byte - Path string + Path string `gorm:"not null;unique_index"` } diff --git a/model/track.go b/model/track.go index 29b3d54..fd2f03d 100644 --- a/model/track.go +++ b/model/track.go @@ -4,9 +4,9 @@ package model type Track struct { Base Album Album - AlbumID string + AlbumID uint Artist Artist - ArtistID string + ArtistID uint Bitrate int Codec string DiscNumber int @@ -16,5 +16,5 @@ type Track struct { TotalTracks int TrackNumber int Year int - Path string `gorm:"not null;unique"` + Path string `gorm:"not null;unique_index"` }