add pointer for ids

This commit is contained in:
sentriz
2019-05-23 16:02:09 +01:00
parent ff009b8851
commit 5f59660702
6 changed files with 20 additions and 25 deletions

1
go.mod
View File

@@ -21,7 +21,6 @@ require (
github.com/peterbourgon/ff v1.2.0 github.com/peterbourgon/ff v1.2.0
github.com/pkg/errors v0.8.1 github.com/pkg/errors v0.8.1
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be 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 github.com/wader/gormstore v0.0.0-20190302154359-acb787ba3755
golang.org/x/crypto v0.0.0-20190422183909-d864b10871cd // indirect golang.org/x/crypto v0.0.0-20190422183909-d864b10871cd // indirect
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect

2
go.sum
View File

@@ -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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 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/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 h1:pNaEDfvqe9W2h4D+xm5f+lnZdao3Rob6O0b8SovpGbE=
github.com/wader/gormstore v0.0.0-20190302154359-acb787ba3755/go.mod h1:PbEnTGtqU8NGCALR62gu2+eQYO8zQDEvaMJiPaj5Hic= github.com/wader/gormstore v0.0.0-20190302154359-acb787ba3755/go.mod h1:PbEnTGtqU8NGCALR62gu2+eQYO8zQDEvaMJiPaj5Hic=

View File

@@ -10,5 +10,5 @@ type CrudBase struct {
} }
type IDBase struct { type IDBase struct {
ID int `gorm:"primary_key"` ID *int `gorm:"primary_key"`
} }

View File

@@ -2,19 +2,24 @@ package model
import "time" 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 // Album represents the albums table
type Album struct { type Album struct {
IDBase IDBase
CrudBase CrudBase
AlbumArtist AlbumArtist 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"` Title string `gorm:"not null;index"`
// an Album having a `Path` is a little weird when browsing by tags // 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 // (for the most part - the library's folder structure is treated as
// if it were flat), but this solves the "American Football problem" // if it were flat), but this solves the "American Football problem"
// https://en.wikipedia.org/wiki/American_Football_(band)#Discography // https://en.wikipedia.org/wiki/American_Football_(band)#Discography
Path string `gorm:"not null;unique_index"` 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 Cover Cover
Year int Year int
Tracks []Track Tracks []Track
@@ -34,9 +39,9 @@ type Track struct {
IDBase IDBase
CrudBase CrudBase
Album Album 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 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 Artist string
Bitrate int Bitrate int
Codec string Codec string
@@ -51,7 +56,7 @@ type Track struct {
ContentType string ContentType string
Size int Size int
Folder Folder 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"` Path string `gorm:"not null;unique_index"`
} }
@@ -85,11 +90,11 @@ type Setting struct {
type Play struct { type Play struct {
IDBase IDBase
User User User User
UserID int `gorm:"not null;index"` UserID *int `gorm:"not null;index"`
Album Album Album Album
AlbumID int `gorm:"not null;index"` AlbumID *int `gorm:"not null;index"`
Folder Folder Folder Folder
FolderID int `gorm:"not null;index"` FolderID *int `gorm:"not null;index"`
Time time.Time Time time.Time
Count int Count int
} }
@@ -101,8 +106,8 @@ type Folder struct {
Name string Name string
Path string `gorm:"not null;unique_index"` Path string `gorm:"not null;unique_index"`
Parent *Folder Parent *Folder
ParentID int ParentID *int
CoverID int CoverID *int
HasTracks bool `gorm:"not null;index"` HasTracks bool `gorm:"not null;index"`
Cover Cover Cover Cover
IsNew bool `gorm:"-"` IsNew bool `gorm:"-"`

View File

@@ -26,10 +26,10 @@ func (s *folderStack) Peek() model.Folder {
return (*s)[l-1] return (*s)[l-1]
} }
func (s *folderStack) PeekID() int { func (s *folderStack) PeekID() *int {
l := len(*s) l := len(*s)
if l == 0 { if l == 0 {
return 0 return nil
} }
return (*s)[l-1].ID return (*s)[l-1].ID
} }

View File

@@ -25,7 +25,6 @@ import (
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/karrick/godirwalk" "github.com/karrick/godirwalk"
"github.com/pkg/errors" "github.com/pkg/errors"
gormbulk "github.com/t-tiger/gorm-bulk-insert"
"github.com/sentriz/gonic/model" "github.com/sentriz/gonic/model"
) )
@@ -119,17 +118,11 @@ func (s *Scanner) handleFolderCompletion(fullPath string, info *godirwalk.Dirent
folder.HasTracks = len(s.curTracks) > 1 folder.HasTracks = len(s.curTracks) > 1
s.tx.Save(&folder) s.tx.Save(&folder)
} }
for _, track := range s.curTracks { for _, t := range s.curTracks {
track.AlbumID = s.curAlbum.ID
track.FolderID = folder.ID
}
toInsert := make([]interface{}, len(s.curTracks))
for i, t := range s.curTracks {
t.FolderID = folder.ID t.FolderID = folder.ID
t.AlbumID = s.curAlbum.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.curTracks = make([]model.Track, 0)
s.curCover = model.Cover{} s.curCover = model.Cover{}