add pointer for ids
This commit is contained in:
1
go.mod
1
go.mod
@@ -21,7 +21,6 @@ require (
|
||||
github.com/peterbourgon/ff v1.2.0
|
||||
github.com/pkg/errors v0.8.1
|
||||
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be
|
||||
github.com/t-tiger/gorm-bulk-insert v0.0.0-20190401142620-ba33202b110e
|
||||
github.com/wader/gormstore v0.0.0-20190302154359-acb787ba3755
|
||||
golang.org/x/crypto v0.0.0-20190422183909-d864b10871cd // indirect
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -169,8 +169,6 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/t-tiger/gorm-bulk-insert v0.0.0-20190401142620-ba33202b110e h1:mOgAh77WyFUaHUCziKMExurMgVJobHeEZCnwIBdActY=
|
||||
github.com/t-tiger/gorm-bulk-insert v0.0.0-20190401142620-ba33202b110e/go.mod h1:SK1RZT4TR1aMUNGtbk6YxTPgx2D/gfbxB571QGnAV+c=
|
||||
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
|
||||
github.com/wader/gormstore v0.0.0-20190302154359-acb787ba3755 h1:pNaEDfvqe9W2h4D+xm5f+lnZdao3Rob6O0b8SovpGbE=
|
||||
github.com/wader/gormstore v0.0.0-20190302154359-acb787ba3755/go.mod h1:PbEnTGtqU8NGCALR62gu2+eQYO8zQDEvaMJiPaj5Hic=
|
||||
|
||||
@@ -10,5 +10,5 @@ type CrudBase struct {
|
||||
}
|
||||
|
||||
type IDBase struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
ID *int `gorm:"primary_key"`
|
||||
}
|
||||
|
||||
@@ -2,19 +2,24 @@ package model
|
||||
|
||||
import "time"
|
||||
|
||||
// q: why in tarnation are all the foreign keys pointers to ints?
|
||||
//
|
||||
// a: so they will be true sqlite null values instead of go zero
|
||||
// values when we save a row without that value
|
||||
|
||||
// Album represents the albums table
|
||||
type Album struct {
|
||||
IDBase
|
||||
CrudBase
|
||||
AlbumArtist AlbumArtist
|
||||
AlbumArtistID int `gorm:"index" sql:"type:int REFERENCES album_artists(id) ON DELETE CASCADE"`
|
||||
AlbumArtistID *int `gorm:"index" sql:"type:int REFERENCES album_artists(id) ON DELETE CASCADE"`
|
||||
Title string `gorm:"not null;index"`
|
||||
// an Album having a `Path` is a little weird when browsing by tags
|
||||
// (for the most part - the library's folder structure is treated as
|
||||
// if it were flat), but this solves the "American Football problem"
|
||||
// https://en.wikipedia.org/wiki/American_Football_(band)#Discography
|
||||
Path string `gorm:"not null;unique_index"`
|
||||
CoverID int `sql:"type:int REFERENCES covers(id)"`
|
||||
CoverID *int `sql:"type:int REFERENCES covers(id)"`
|
||||
Cover Cover
|
||||
Year int
|
||||
Tracks []Track
|
||||
@@ -34,9 +39,9 @@ type Track struct {
|
||||
IDBase
|
||||
CrudBase
|
||||
Album Album
|
||||
AlbumID int `gorm:"index" sql:"type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||
AlbumID *int `gorm:"index" sql:"type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||
AlbumArtist AlbumArtist
|
||||
AlbumArtistID int `gorm:"index" sql:"type:int REFERENCES album_artists(id) ON DELETE CASCADE"`
|
||||
AlbumArtistID *int `gorm:"index" sql:"type:int REFERENCES album_artists(id) ON DELETE CASCADE"`
|
||||
Artist string
|
||||
Bitrate int
|
||||
Codec string
|
||||
@@ -51,7 +56,7 @@ type Track struct {
|
||||
ContentType string
|
||||
Size int
|
||||
Folder Folder
|
||||
FolderID int `gorm:"not null;index" sql:"type:int REFERENCES folders(id) ON DELETE CASCADE"`
|
||||
FolderID *int `gorm:"not null;index" sql:"type:int REFERENCES folders(id) ON DELETE CASCADE"`
|
||||
Path string `gorm:"not null;unique_index"`
|
||||
}
|
||||
|
||||
@@ -85,11 +90,11 @@ type Setting struct {
|
||||
type Play struct {
|
||||
IDBase
|
||||
User User
|
||||
UserID int `gorm:"not null;index"`
|
||||
UserID *int `gorm:"not null;index"`
|
||||
Album Album
|
||||
AlbumID int `gorm:"not null;index"`
|
||||
AlbumID *int `gorm:"not null;index"`
|
||||
Folder Folder
|
||||
FolderID int `gorm:"not null;index"`
|
||||
FolderID *int `gorm:"not null;index"`
|
||||
Time time.Time
|
||||
Count int
|
||||
}
|
||||
@@ -101,8 +106,8 @@ type Folder struct {
|
||||
Name string
|
||||
Path string `gorm:"not null;unique_index"`
|
||||
Parent *Folder
|
||||
ParentID int
|
||||
CoverID int
|
||||
ParentID *int
|
||||
CoverID *int
|
||||
HasTracks bool `gorm:"not null;index"`
|
||||
Cover Cover
|
||||
IsNew bool `gorm:"-"`
|
||||
|
||||
@@ -26,10 +26,10 @@ func (s *folderStack) Peek() model.Folder {
|
||||
return (*s)[l-1]
|
||||
}
|
||||
|
||||
func (s *folderStack) PeekID() int {
|
||||
func (s *folderStack) PeekID() *int {
|
||||
l := len(*s)
|
||||
if l == 0 {
|
||||
return 0
|
||||
return nil
|
||||
}
|
||||
return (*s)[l-1].ID
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ import (
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/karrick/godirwalk"
|
||||
"github.com/pkg/errors"
|
||||
gormbulk "github.com/t-tiger/gorm-bulk-insert"
|
||||
|
||||
"github.com/sentriz/gonic/model"
|
||||
)
|
||||
@@ -119,17 +118,11 @@ func (s *Scanner) handleFolderCompletion(fullPath string, info *godirwalk.Dirent
|
||||
folder.HasTracks = len(s.curTracks) > 1
|
||||
s.tx.Save(&folder)
|
||||
}
|
||||
for _, track := range s.curTracks {
|
||||
track.AlbumID = s.curAlbum.ID
|
||||
track.FolderID = folder.ID
|
||||
}
|
||||
toInsert := make([]interface{}, len(s.curTracks))
|
||||
for i, t := range s.curTracks {
|
||||
for _, t := range s.curTracks {
|
||||
t.FolderID = folder.ID
|
||||
t.AlbumID = s.curAlbum.ID
|
||||
toInsert[i] = t
|
||||
s.tx.Save(&t)
|
||||
}
|
||||
gormbulk.BulkInsert(s.tx, toInsert, 3000)
|
||||
//
|
||||
s.curTracks = make([]model.Track, 0)
|
||||
s.curCover = model.Cover{}
|
||||
|
||||
Reference in New Issue
Block a user