1
Fork 0

Enable foreign keys for sqlite

This commit is contained in:
viktorstrate 2021-11-06 13:23:59 +01:00
parent c48ede175a
commit 0618b70110
No known key found for this signature in database
GPG Key ID: 3F855605109C1E8A
4 changed files with 29 additions and 17 deletions

View File

@ -103,6 +103,11 @@ func ConfigureDatabase(config *gorm.Config) (*gorm.DB, error) {
return nil, err
}
// Manually enable foreign keys for sqlite, as this isn't done by default
if drivers.DatabaseDriver() == drivers.DatabaseDriverSqlite {
db.Exec("PRAGMA foreign_keys = ON")
}
return db, nil
}

View File

@ -108,11 +108,11 @@ func deleteOldUserAlbums(db *gorm.DB, scannedAlbums []*models.Album, user *model
// Delete old albums from database
err := db.Transaction(func(tx *gorm.DB) error {
if err := tx.Where("album_id IN ?", deleteAlbumIDs).Delete(&models.UserAlbums{}).Error; err != nil {
if err := tx.Where("album_id IN (?)", deleteAlbumIDs).Delete(&models.UserAlbums{}).Error; err != nil {
return err
}
if err := tx.Where("id IN ?", deleteAlbumIDs).Delete(models.Album{}).Error; err != nil {
if err := tx.Where("id IN (?)", deleteAlbumIDs).Delete(models.Album{}).Error; err != nil {
return err
}

View File

@ -63,19 +63,26 @@ func TestCleanupMedia(t *testing.T) {
return
}
test_utils.RunScannerOnUser(t, db, user1)
assert.Equal(t, 9, countAllMedia())
assert.Equal(t, 18, countAllMediaURLs())
t.Run("Modify albums", func(t *testing.T) {
// move faces directory
assert.NoError(t, os.Rename(path.Join(test_dir, "faces"), path.Join(test_dir, "faces_moved")))
test_utils.RunScannerAll(t, db)
assert.Equal(t, 9, countAllMedia())
assert.Equal(t, 18, countAllMediaURLs())
test_utils.RunScannerOnUser(t, db, user1)
assert.Equal(t, 9, countAllMedia())
assert.Equal(t, 18, countAllMediaURLs())
// remove faces_moved directory
assert.NoError(t, os.RemoveAll(path.Join(test_dir, "faces_moved")))
test_utils.RunScannerAll(t, db)
assert.Equal(t, 3, countAllMedia())
assert.Equal(t, 6, countAllMediaURLs())
// move faces directory
assert.NoError(t, os.Rename(path.Join(test_dir, "faces"), path.Join(test_dir, "faces_moved")))
test_utils.RunScannerAll(t, db)
assert.Equal(t, 9, countAllMedia())
assert.Equal(t, 18, countAllMediaURLs())
// remove faces_moved directory
assert.NoError(t, os.RemoveAll(path.Join(test_dir, "faces_moved")))
test_utils.RunScannerAll(t, db)
assert.Equal(t, 3, countAllMedia())
assert.Equal(t, 6, countAllMediaURLs())
})
// t.Run("Modify images", func(t *testing.T) {
// })
}

View File

@ -66,13 +66,13 @@ func (v EnvironmentVariable) GetBool() bool {
// ShouldServeUI whether or not the "serve ui" option is enabled
func ShouldServeUI() bool {
return EnvServeUI.GetValue() == "1"
return EnvServeUI.GetBool()
}
// DevelopmentMode describes whether or not the server is running in development mode,
// and should thus print debug informations and enable other features related to developing.
func DevelopmentMode() bool {
return EnvDevelopmentMode.GetValue() == "1"
return EnvDevelopmentMode.GetBool()
}
// UIPath returns the value from where the static UI files are located if SERVE_UI=1