From 0618b7011032aa792f7da47ccd36cb13a66bd8da Mon Sep 17 00:00:00 2001 From: viktorstrate Date: Sat, 6 Nov 2021 13:23:59 +0100 Subject: [PATCH] Enable foreign keys for sqlite --- api/database/database.go | 5 +++++ api/scanner/cleanup_media.go | 4 ++-- api/scanner/cleanup_media_test.go | 33 ++++++++++++++++++------------ api/utils/environment_variables.go | 4 ++-- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/api/database/database.go b/api/database/database.go index ac94e125..b13564dd 100644 --- a/api/database/database.go +++ b/api/database/database.go @@ -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 } diff --git a/api/scanner/cleanup_media.go b/api/scanner/cleanup_media.go index 96f72cc3..7dff49bd 100644 --- a/api/scanner/cleanup_media.go +++ b/api/scanner/cleanup_media.go @@ -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 } diff --git a/api/scanner/cleanup_media_test.go b/api/scanner/cleanup_media_test.go index 7df7b1ec..ed5e708e 100644 --- a/api/scanner/cleanup_media_test.go +++ b/api/scanner/cleanup_media_test.go @@ -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) { + + // }) } diff --git a/api/utils/environment_variables.go b/api/utils/environment_variables.go index 612ff793..76d5861a 100644 --- a/api/utils/environment_variables.go +++ b/api/utils/environment_variables.go @@ -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