add basic foreigh key stuff
This commit is contained in:
@@ -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"`
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user