dont use uuid

This commit is contained in:
sentriz
2019-03-29 22:19:23 +00:00
parent e9c0f09d0c
commit 5607db787f
5 changed files with 25 additions and 18 deletions

View File

@@ -48,7 +48,7 @@ var (
type lastAlbum struct { type lastAlbum struct {
coverModTime time.Time // 1st needed for cover insertion coverModTime time.Time // 1st needed for cover insertion
coverPath string // 2rd 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 { func (l *lastAlbum) isEmpty() bool {
@@ -69,7 +69,7 @@ func isCover(filename string) bool {
func readTags(fullPath string) (tag.Metadata, error) { func readTags(fullPath string) (tag.Metadata, error) {
trackData, err := os.Open(fullPath) trackData, err := os.Open(fullPath)
if err != nil { 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() defer trackData.Close()
tags, err := tag.ReadFrom(trackData) tags, err := tag.ReadFrom(trackData)
@@ -94,7 +94,7 @@ func handleFolderCompletion(fullPath string, info *godirwalk.Dirent) error {
} }
image, err := ioutil.ReadFile(cLastAlbum.coverPath) image, err := ioutil.ReadFile(cLastAlbum.coverPath)
if err != nil { if err != nil {
return fmt.Errorf("when reading cover: %v\n", err) return fmt.Errorf("when reading cover: %v", err)
} }
cover.Image = image cover.Image = image
cover.AlbumID = cLastAlbum.id cover.AlbumID = cLastAlbum.id
@@ -103,13 +103,13 @@ func handleFolderCompletion(fullPath string, info *godirwalk.Dirent) error {
return nil return nil
} }
func processFile(fullPath string, info *godirwalk.Dirent) error { func handleFile(fullPath string, info *godirwalk.Dirent) error {
if info.IsDir() { if info.IsDir() {
return nil return nil
} }
stat, err := os.Stat(fullPath) stat, err := os.Stat(fullPath)
if err != nil { if err != nil {
return fmt.Errorf("when stating file: %v\n", err) return fmt.Errorf("when stating file: %v", err)
} }
modTime := stat.ModTime() modTime := stat.ModTime()
_, filename := path.Split(fullPath) _, filename := path.Split(fullPath)
@@ -126,12 +126,13 @@ func processFile(fullPath string, info *godirwalk.Dirent) error {
Path: fullPath, Path: fullPath,
} }
err = tx.Where(track).First(&track).Error 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 return nil
} }
tags, err := readTags(fullPath) tags, err := readTags(fullPath)
if err != nil { if err != nil {
return fmt.Errorf("when reading tags: %v\n", err) return fmt.Errorf("when reading tags: %v", err)
} }
trackNumber, totalTracks := tags.Track() trackNumber, totalTracks := tags.Track()
discNumber, TotalDiscs := tags.Disc() discNumber, TotalDiscs := tags.Disc()
@@ -184,10 +185,16 @@ func main() {
&model.Track{}, &model.Track{},
&model.Cover{}, &model.Cover{},
) )
// 🤫🤫🤫
orm.Exec(`
INSERT INTO sqlite_sequence(name, seq)
SELECT 'albums', 500000
WHERE NOT EXISTS (SELECT * FROM sqlite_sequence)
`)
startTime := time.Now() startTime := time.Now()
tx = orm.Begin() tx = orm.Begin()
err := godirwalk.Walk(os.Args[1], &godirwalk.Options{ err := godirwalk.Walk(os.Args[1], &godirwalk.Options{
Callback: processFile, Callback: handleFile,
PostChildrenCallback: handleFolderCompletion, PostChildrenCallback: handleFolderCompletion,
Unsorted: true, Unsorted: true,
}) })

View File

@@ -2,9 +2,9 @@ package model
// Album represents the albums table // Album represents the albums table
type Album struct { type Album struct {
BaseWithUUID Base
Artist Artist Artist Artist
ArtistID string ArtistID uint
Title string Title string `gorm:"not null;index"`
Tracks []Track Tracks []Track
} }

View File

@@ -2,7 +2,7 @@ package model
// Artist represents the artists table // Artist represents the artists table
type Artist struct { type Artist struct {
BaseWithUUID Base
Albums []Album Albums []Album
Name string `gorm:"unique;n"` Name string `gorm:"not null;unique_index"`
} }

View File

@@ -4,7 +4,7 @@ package model
type Cover struct { type Cover struct {
Base Base
Album Album Album Album
AlbumID string AlbumID uint
Image []byte Image []byte
Path string Path string `gorm:"not null;unique_index"`
} }

View File

@@ -4,9 +4,9 @@ package model
type Track struct { type Track struct {
Base Base
Album Album Album Album
AlbumID string AlbumID uint
Artist Artist Artist Artist
ArtistID string ArtistID uint
Bitrate int Bitrate int
Codec string Codec string
DiscNumber int DiscNumber int
@@ -16,5 +16,5 @@ type Track struct {
TotalTracks int TotalTracks int
TrackNumber int TrackNumber int
Year int Year int
Path string `gorm:"not null;unique"` Path string `gorm:"not null;unique_index"`
} }