add basic foreigh key stuff
This commit is contained in:
@@ -7,14 +7,14 @@ type Album struct {
|
|||||||
IDBase
|
IDBase
|
||||||
CrudBase
|
CrudBase
|
||||||
AlbumArtist AlbumArtist
|
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"`
|
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
|
CoverID int `sql:"type:int REFERENCES covers(id)"`
|
||||||
Cover Cover
|
Cover Cover
|
||||||
Year int
|
Year int
|
||||||
Tracks []Track
|
Tracks []Track
|
||||||
@@ -24,8 +24,8 @@ type Album struct {
|
|||||||
type AlbumArtist struct {
|
type AlbumArtist struct {
|
||||||
IDBase
|
IDBase
|
||||||
CrudBase
|
CrudBase
|
||||||
Albums []Album
|
|
||||||
Name string `gorm:"not null;unique_index"`
|
Name string `gorm:"not null;unique_index"`
|
||||||
|
Albums []Album
|
||||||
}
|
}
|
||||||
|
|
||||||
// Track represents the tracks table
|
// Track represents the tracks table
|
||||||
@@ -33,9 +33,9 @@ type Track struct {
|
|||||||
IDBase
|
IDBase
|
||||||
CrudBase
|
CrudBase
|
||||||
Album Album
|
Album Album
|
||||||
AlbumID int `gorm:"index"`
|
AlbumID int `gorm:"index" sql:"type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||||
AlbumArtist AlbumArtist
|
AlbumArtist AlbumArtist
|
||||||
AlbumArtistID int
|
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
|
||||||
@@ -50,7 +50,7 @@ type Track struct {
|
|||||||
ContentType string
|
ContentType string
|
||||||
Size int
|
Size int
|
||||||
Folder Folder
|
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"`
|
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 {
|
func (s *Scanner) MigrateDB() error {
|
||||||
defer logElapsed(time.Now(), "migrating database")
|
defer logElapsed(time.Now(), "migrating database")
|
||||||
|
s.db.Exec("PRAGMA foreign_keys = ON")
|
||||||
s.tx = s.db.Begin()
|
s.tx = s.db.Begin()
|
||||||
defer s.tx.Commit()
|
defer s.tx.Commit()
|
||||||
s.tx.AutoMigrate(
|
s.tx.AutoMigrate(
|
||||||
&model.Album{},
|
model.Album{},
|
||||||
&model.AlbumArtist{},
|
model.AlbumArtist{},
|
||||||
&model.Track{},
|
model.Track{},
|
||||||
&model.Cover{},
|
model.Cover{},
|
||||||
&model.User{},
|
model.User{},
|
||||||
&model.Setting{},
|
model.Setting{},
|
||||||
&model.Play{},
|
model.Play{},
|
||||||
&model.Folder{},
|
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{
|
s.tx.FirstOrCreate(&model.User{}, model.User{
|
||||||
Name: "admin",
|
Name: "admin",
|
||||||
Password: "admin",
|
Password: "admin",
|
||||||
@@ -260,6 +252,7 @@ func (s *Scanner) Start() error {
|
|||||||
atomic.StoreInt32(&IsScanning, 1)
|
atomic.StoreInt32(&IsScanning, 1)
|
||||||
defer atomic.StoreInt32(&IsScanning, 0)
|
defer atomic.StoreInt32(&IsScanning, 0)
|
||||||
defer logElapsed(time.Now(), "scanning")
|
defer logElapsed(time.Now(), "scanning")
|
||||||
|
s.db.Exec("PRAGMA foreign_keys = ON")
|
||||||
s.tx = s.db.Begin()
|
s.tx = s.db.Begin()
|
||||||
defer s.tx.Commit()
|
defer s.tx.Commit()
|
||||||
//
|
//
|
||||||
|
|||||||
Reference in New Issue
Block a user