Skip to content

Commit

Permalink
more test fixes for pgsql
Browse files Browse the repository at this point in the history
  • Loading branch information
NodudeWasTaken committed Oct 15, 2024
1 parent 992f481 commit 390059d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 28 deletions.
5 changes: 5 additions & 0 deletions pkg/models/relationships.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"context"
"slices"

"github.com/stashapp/stash/pkg/sliceutil"
)
Expand Down Expand Up @@ -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")
Expand Down
26 changes: 0 additions & 26 deletions pkg/sqlite/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 19 additions & 2 deletions pkg/sqlite/database_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 ""
Expand Down
26 changes: 26 additions & 0 deletions pkg/sqlite/database_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/sqlite/gallery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 390059d

Please sign in to comment.