From ca37f5c27e00a06aab50e96a91f292967fbf635a Mon Sep 17 00:00:00 2001 From: sentriz Date: Wed, 22 May 2019 13:09:43 +0100 Subject: [PATCH] add basic foreigh key stuff --- model/model.go | 12 ++++++------ scanner/scanner.go | 27 ++++++++++----------------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/model/model.go b/model/model.go index ddb3eeb..11d6453 100644 --- a/model/model.go +++ b/model/model.go @@ -7,14 +7,14 @@ type Album struct { IDBase CrudBase AlbumArtist AlbumArtist - AlbumArtistID int `gorm:"index"` + 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 + CoverID int `sql:"type:int REFERENCES covers(id)"` Cover Cover Year int Tracks []Track @@ -24,8 +24,8 @@ type Album struct { type AlbumArtist struct { IDBase CrudBase - Albums []Album Name string `gorm:"not null;unique_index"` + Albums []Album } // Track represents the tracks table @@ -33,9 +33,9 @@ type Track struct { IDBase CrudBase Album Album - AlbumID int `gorm:"index"` + AlbumID int `gorm:"index" sql:"type:int REFERENCES albums(id) ON DELETE CASCADE"` AlbumArtist AlbumArtist - AlbumArtistID int + AlbumArtistID int `gorm:"index" sql:"type:int REFERENCES album_artists(id) ON DELETE CASCADE"` Artist string Bitrate int Codec string @@ -50,7 +50,7 @@ type Track struct { ContentType string Size int Folder Folder - FolderID int + FolderID int `gorm:"not null;index" sql:"type:int REFERENCES folders(id) ON DELETE CASCADE"` Path string `gorm:"not null;unique_index"` } diff --git a/scanner/scanner.go b/scanner/scanner.go index 5a66a8b..907cee4 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -224,27 +224,19 @@ func (s *Scanner) handleItem(fullPath string, info *godirwalk.Dirent) error { func (s *Scanner) MigrateDB() error { defer logElapsed(time.Now(), "migrating database") + s.db.Exec("PRAGMA foreign_keys = ON") s.tx = s.db.Begin() defer s.tx.Commit() s.tx.AutoMigrate( - &model.Album{}, - &model.AlbumArtist{}, - &model.Track{}, - &model.Cover{}, - &model.User{}, - &model.Setting{}, - &model.Play{}, - &model.Folder{}, + model.Album{}, + model.AlbumArtist{}, + model.Track{}, + model.Cover{}, + model.User{}, + model.Setting{}, + model.Play{}, + model.Folder{}, ) - // set starting value for `albums` table's - // auto increment - s.tx.Exec(` - INSERT INTO sqlite_sequence(name, seq) - SELECT 'albums', 500000 - WHERE NOT EXISTS (SELECT * - FROM sqlite_sequence); - `) - // create the first user if there is none s.tx.FirstOrCreate(&model.User{}, model.User{ Name: "admin", Password: "admin", @@ -260,6 +252,7 @@ func (s *Scanner) Start() error { atomic.StoreInt32(&IsScanning, 1) defer atomic.StoreInt32(&IsScanning, 0) defer logElapsed(time.Now(), "scanning") + s.db.Exec("PRAGMA foreign_keys = ON") s.tx = s.db.Begin() defer s.tx.Commit() //