diff --git a/pkg/models/relationships.go b/pkg/models/relationships.go index 5495f858b17..a899490ec52 100644 --- a/pkg/models/relationships.go +++ b/pkg/models/relationships.go @@ -2,6 +2,7 @@ package models import ( "context" + "slices" "github.com/stashapp/stash/pkg/sliceutil" ) @@ -86,6 +87,10 @@ func (r RelatedIDs) Loaded() bool { return r.list != nil } +func (r RelatedIDs) Sort() { + slices.Sort(r.list) +} + func (r RelatedIDs) mustLoaded() { if !r.Loaded() { panic("list has not been loaded") diff --git a/pkg/sqlite/database.go b/pkg/sqlite/database.go index c0d5e56a410..e68a14aa209 100644 --- a/pkg/sqlite/database.go +++ b/pkg/sqlite/database.go @@ -299,32 +299,6 @@ func (db *Database) initialise() error { return nil } -func (db *Database) openReadDB() error { - const ( - disableForeignKeys = false - writable = false - ) - var err error - db.readDB, err = db.open(disableForeignKeys, writable) - db.readDB.SetMaxOpenConns(maxReadConnections) - db.readDB.SetMaxIdleConns(maxReadConnections) - db.readDB.SetConnMaxIdleTime(dbConnTimeout) - return err -} - -func (db *Database) openWriteDB() error { - const ( - disableForeignKeys = false - writable = true - ) - var err error - db.writeDB, err = db.open(disableForeignKeys, writable) - db.writeDB.SetMaxOpenConns(maxWriteConnections) - db.writeDB.SetMaxIdleConns(maxWriteConnections) - db.writeDB.SetConnMaxIdleTime(dbConnTimeout) - return err -} - func (db *Database) Anonymise(outPath string) error { anon, err := NewAnonymiser(db, outPath) diff --git a/pkg/sqlite/database_postgres.go b/pkg/sqlite/database_postgres.go index a5cba4a9309..e4b3540bb2a 100644 --- a/pkg/sqlite/database_postgres.go +++ b/pkg/sqlite/database_postgres.go @@ -34,6 +34,25 @@ func NewPostgresDatabase(dbConnector string) *PostgresDB { func (db *PostgresDB) lock() {} func (db *PostgresDB) unlock() {} +func (db *PostgresDB) openReadDB() error { + const ( + disableForeignKeys = false + writable = true + ) + var err error + db.readDB, err = db.open(disableForeignKeys, writable) + db.readDB.SetConnMaxIdleTime(dbConnTimeout) + db.writeDB = db.readDB + return err +} + +func (db *PostgresDB) openWriteDB() error { + if db.writeDB == nil { + return db.openReadDB() + } + return nil +} + func (db *PostgresDB) DatabaseType() DatabaseType { return PostgresBackend } @@ -97,13 +116,11 @@ func (db *PostgresDB) Backup(backupPath string) (err error) { return nil } -// RestoreFromBackup restores the database from a backup file at the given path. func (db *PostgresDB) RestoreFromBackup(backupPath string) (err error) { logger.Warn("Postgres backend detected, ignoring RestoreFromBackup request") return nil } -// DatabaseBackupPath returns the path to a database backup file for the given directory. func (db *PostgresDB) DatabaseBackupPath(backupDirectoryPath string) string { logger.Warn("Postgres backend detected, ignoring DatabaseBackupPath request") return "" diff --git a/pkg/sqlite/database_sqlite.go b/pkg/sqlite/database_sqlite.go index c8d9ebdda29..4f302c10643 100644 --- a/pkg/sqlite/database_sqlite.go +++ b/pkg/sqlite/database_sqlite.go @@ -56,6 +56,32 @@ func (db *SQLiteDB) unlock() { } } +func (db *SQLiteDB) openReadDB() error { + const ( + disableForeignKeys = false + writable = false + ) + var err error + db.readDB, err = db.open(disableForeignKeys, writable) + db.readDB.SetMaxOpenConns(maxReadConnections) + db.readDB.SetMaxIdleConns(maxReadConnections) + db.readDB.SetConnMaxIdleTime(dbConnTimeout) + return err +} + +func (db *SQLiteDB) openWriteDB() error { + const ( + disableForeignKeys = false + writable = true + ) + var err error + db.writeDB, err = db.open(disableForeignKeys, writable) + db.writeDB.SetMaxOpenConns(maxWriteConnections) + db.writeDB.SetMaxIdleConns(maxWriteConnections) + db.writeDB.SetConnMaxIdleTime(dbConnTimeout) + return err +} + func (db *SQLiteDB) AppSchemaVersion() uint { return appSchemaVersion } diff --git a/pkg/sqlite/gallery_test.go b/pkg/sqlite/gallery_test.go index ee602ef05b6..9408ec1b40e 100644 --- a/pkg/sqlite/gallery_test.go +++ b/pkg/sqlite/gallery_test.go @@ -54,6 +54,13 @@ func loadGalleryRelationships(ctx context.Context, expected models.Gallery, actu return nil } +func sortGallery(copy *models.Gallery) { + // Ordering is not ensured + copy.SceneIDs.Sort() + copy.PerformerIDs.Sort() + copy.TagIDs.Sort() +} + func Test_galleryQueryBuilder_Create(t *testing.T) { var ( title = "title" @@ -180,6 +187,10 @@ func Test_galleryQueryBuilder_Create(t *testing.T) { return } + // Ordering is not ensured + sortGallery(copy) + sortGallery(s) + assert.Equal(copy, s) // ensure can find the scene @@ -380,6 +391,10 @@ func Test_galleryQueryBuilder_Update(t *testing.T) { return } + // Ordering is not ensured + sortGallery(copy) + sortGallery(s) + assert.Equal(copy, *s) return @@ -809,6 +824,11 @@ func Test_galleryQueryBuilder_UpdatePartialRelationships(t *testing.T) { return } + // Ordering is not ensured + sortGallery(copy) + sortGallery(s) + sortGallery(got) + // only compare fields that were in the partial if tt.partial.PerformerIDs != nil { assert.ElementsMatch(tt.want.PerformerIDs.List(), got.PerformerIDs.List())