Skip to content

Commit

Permalink
pgsql fix tests (suffering)
Browse files Browse the repository at this point in the history
  • Loading branch information
NodudeWasTaken committed Oct 17, 2024
1 parent 2edc214 commit 8fa2b38
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
20 changes: 8 additions & 12 deletions pkg/sqlite/database_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,16 @@ func (db *PostgresDB) open(disableForeignKeys bool, writable bool) (conn *sqlx.D

func (db *PostgresDB) Remove() (err error) {
_, err = db.writeDB.Exec(`
DO $$ DECLARE
r RECORD;
DO $$
DECLARE
r record;
BEGIN
-- Disable triggers to avoid foreign key constraint violations
EXECUTE 'SET session_replication_role = replica';
-- Drop all tables
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP
EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE';
FOR r IN SELECT quote_ident(tablename) AS tablename, quote_ident(schemaname) AS schemaname FROM pg_tables WHERE schemaname = 'public'
LOOP
RAISE INFO 'Dropping table %.%', r.schemaname, r.tablename;
EXECUTE format('DROP TABLE IF EXISTS %I.%I CASCADE', r.schemaname, r.tablename);
END LOOP;
-- Re-enable triggers
EXECUTE 'SET session_replication_role = DEFAULT';
END $$;
END$$;
`)

return err
Expand Down
6 changes: 5 additions & 1 deletion pkg/sqlite/gallery_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ func (qb *galleryFilterHandler) missingCriterionHandler(isMissing *string) crite
galleryRepository.performers.join(f, "performers_join", "galleries.id")
f.addWhere("performers_join.gallery_id IS NULL")
case "date":
f.addWhere("galleries.date IS NULL OR galleries.date IS \"\"")
q := "galleries.date IS NULL"
if dbWrapper.dbType == SqliteBackend {
q += ` OR galleries.date IS ""`
}
f.addWhere(q)
case "tags":
galleryRepository.tags.join(f, "tags_join", "galleries.id")
f.addWhere("tags_join.gallery_id IS NULL")
Expand Down
13 changes: 13 additions & 0 deletions pkg/sqlite/gallery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package sqlite_test
import (
"context"
"math"
"sort"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -61,6 +62,12 @@ func sortGallery(copy *models.Gallery) {
copy.TagIDs.Sort()
}

func sortByID[T any](list []T, getID func(T) int) {
sort.Slice(list, func(i, j int) bool {
return getID(list[i]) < getID(list[j])
})
}

func Test_galleryQueryBuilder_Create(t *testing.T) {
var (
title = "title"
Expand Down Expand Up @@ -1131,6 +1138,8 @@ func Test_galleryQueryBuilder_FindByChecksums(t *testing.T) {
return
}

sortByID(tt.want, func(g *models.Gallery) int { return g.ID })
sortByID(got, func(g *models.Gallery) int { return g.ID })
assert.Equal(tt.want, got)
})
}
Expand Down Expand Up @@ -1231,6 +1240,8 @@ func Test_galleryQueryBuilder_FindBySceneID(t *testing.T) {
return
}

sortByID(tt.want, func(g *models.Gallery) int { return g.ID })
sortByID(got, func(g *models.Gallery) int { return g.ID })
assert.Equal(tt.want, got)
})
}
Expand Down Expand Up @@ -1276,6 +1287,8 @@ func Test_galleryQueryBuilder_FindByImageID(t *testing.T) {
return
}

sortByID(tt.want, func(g *models.Gallery) int { return g.ID })
sortByID(got, func(g *models.Gallery) int { return g.ID })
assert.Equal(tt.want, got)
})
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/sqlite/scene_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,11 @@ func (qb *sceneFilterHandler) isMissingCriterionHandler(isMissing *string) crite
sceneRepository.performers.join(f, "performers_join", "scenes.id")
f.addWhere("performers_join.scene_id IS NULL")
case "date":
f.addWhere(`scenes.date IS NULL OR scenes.date IS ""`)
q := "scenes.date IS NULL"
if dbWrapper.dbType == SqliteBackend {
q += ` OR scenes.date IS ""`
}
f.addWhere(q)
case "tags":
sceneRepository.tags.join(f, "tags_join", "scenes.id")
f.addWhere("tags_join.scene_id IS NULL")
Expand Down

0 comments on commit 8fa2b38

Please sign in to comment.