Skip to content

Commit

Permalink
Refactor database date formats
Browse files Browse the repository at this point in the history
  • Loading branch information
InfiniteStash committed Aug 31, 2024
1 parent 52d8823 commit 0de6696
Show file tree
Hide file tree
Showing 19 changed files with 270 additions and 451 deletions.
8 changes: 0 additions & 8 deletions gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ models:
fields:
url:
resolver: true
PerformerEdit:
fields:
birthdate:
resolver: true
SceneEdit:
fields:
date:
resolver: true
QueryPerformersResultType:
model: github.com/stashapp/stash-box/pkg/models.PerformerQuery
QueryEditsResultType:
Expand Down
10 changes: 2 additions & 8 deletions pkg/api/performer_edit_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,13 @@ func (s *performerEditTestRunner) verifyPerformerEditDetails(input models.Perfor
c := fieldComparator{r: &s.testRunner}
c.strPtrStrPtr(input.Name, performerDetails.Name, "Name")
c.strPtrStrPtr(input.Disambiguation, performerDetails.Disambiguation, "Disambiguation")
c.strPtrStrPtr(input.Birthdate, performerDetails.Birthdate, "Birthdate")

assert.DeepEqual(s.t, input.Aliases, performerDetails.AddedAliases)
assert.Assert(s.t, input.Gender.IsValid() && (input.Gender.String() == *performerDetails.Gender))

s.compareURLs(input.Urls, performerDetails.AddedUrls)

date, accuracy, _ := models.ParseFuzzyString(input.Birthdate)
assert.Assert(s.t, accuracy.Valid && (accuracy.String == *performerDetails.BirthdateAccuracy))
assert.Assert(s.t, date.Valid && (date.String == *performerDetails.Birthdate))
assert.Assert(s.t, input.Ethnicity.IsValid() && (input.Ethnicity.String() == *performerDetails.Ethnicity))
assert.Assert(s.t, input.Country != nil && (*input.Country == *performerDetails.Country))
assert.Assert(s.t, input.EyeColor.IsValid() && (input.EyeColor.String() == *performerDetails.EyeColor))
Expand Down Expand Up @@ -142,14 +140,10 @@ func (s *performerEditTestRunner) verifyPerformerEdit(input models.PerformerEdit
urls, _ := resolver.Urls(s.ctx, performer)
s.compareURLs(input.Urls, urls)

date, accuracy, _ := models.ParseFuzzyString(input.Birthdate)

if input.Birthdate == nil {
assert.Assert(s.t, !performer.BirthdateAccuracy.Valid)
assert.Assert(s.t, !performer.Birthdate.Valid)
} else {
assert.Equal(s.t, accuracy.String, performer.BirthdateAccuracy.String)
assert.Equal(s.t, date.String, performer.Birthdate.String)
assert.Equal(s.t, *input.Birthdate, performer.Birthdate.String)
}

if input.Ethnicity == nil {
Expand Down
10 changes: 4 additions & 6 deletions pkg/api/performer_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ func (s *performerTestRunner) verifyCreatedPerformer(input models.PerformerCreat
urls, _ := s.resolver.Performer().Urls(s.ctx, performer)
assert.Assert(s.t, compareUrls(input.Urls, urls), "Urls")

inputDate, inputAccuracy, _ := models.ParseFuzzyString(input.Birthdate)
birthdate, _ := r.Birthdate(s.ctx, performer)
assert.Assert(s.t, bothNil(birthdate, input.Birthdate) || (!oneNil(birthdate, input.Birthdate) && birthdate.Date == inputDate.String && birthdate.Accuracy.String() == inputAccuracy.String))
birthdate, _ := r.BirthDate(s.ctx, performer)
assert.DeepEqual(s.t, birthdate, input.Birthdate)

ethnicity, _ := r.Ethnicity(s.ctx, performer)
assert.DeepEqual(s.t, ethnicity, input.Ethnicity)
Expand Down Expand Up @@ -263,9 +262,8 @@ func (s *performerTestRunner) verifyUpdatedPerformer(input models.PerformerUpdat
urls, _ := s.resolver.Performer().Urls(s.ctx, performer)
assert.Assert(s.t, compareUrls(input.Urls, urls))

inputDate, inputAccuracy, _ := models.ParseFuzzyString(input.Birthdate)
birthdate, _ := r.Birthdate(s.ctx, performer)
assert.Assert(s.t, birthdate == nil || (birthdate.Date == inputDate.String && birthdate.Accuracy.String() == inputAccuracy.String))
birthdate, _ := s.resolver.Performer().BirthDate(s.ctx, performer)
assert.DeepEqual(s.t, birthdate, input.Birthdate)

tattoos, _ := s.resolver.Performer().Tattoos(s.ctx, performer)
assert.Assert(s.t, compareBodyMods(input.Tattoos, tattoos))
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/resolver_model_performer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func (r *performerResolver) Urls(ctx context.Context, obj *models.Performer) ([]
return dataloader.For(ctx).PerformerUrlsByID.Load(obj.ID)
}

// Deprecated: use `BirthDate`
func (r *performerResolver) Birthdate(ctx context.Context, obj *models.Performer) (*models.FuzzyDate, error) {
ret := obj.ResolveBirthdate()
return &ret, nil
return resolveFuzzyDate(obj.Birthdate), nil
}

func (r *performerResolver) BirthDate(ctx context.Context, obj *models.Performer) (*string, error) {
return resolveFuzzyDate(&obj.Birthdate.String, &obj.BirthdateAccuracy.String), nil
return resolveNullString(obj.Birthdate), nil
}

func (r *performerResolver) Age(ctx context.Context, obj *models.Performer) (*int, error) {
Expand Down
4 changes: 0 additions & 4 deletions pkg/api/resolver_model_performer_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ func (r *performerEditResolver) RemovedImages(ctx context.Context, obj *models.P
return imageList(ctx, obj.RemovedImages)
}

func (r *performerEditResolver) Birthdate(ctx context.Context, obj *models.PerformerEdit) (*string, error) {
return resolveFuzzyDate(obj.Birthdate, obj.BirthdateAccuracy), nil
}

func (r *performerEditResolver) Images(ctx context.Context, obj *models.PerformerEdit) ([]*models.Image, error) {
fac := r.getRepoFactory(ctx)
id, err := fac.Edit().FindPerformerID(obj.EditID)
Expand Down
10 changes: 7 additions & 3 deletions pkg/api/resolver_model_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ func (r *sceneResolver) Code(ctx context.Context, obj *models.Scene) (*string, e
return resolveNullString(obj.Code), nil
}

// Deprecated: use `DateFuzzy`
// Deprecated: use `ReleaseDate`
func (r *sceneResolver) Date(ctx context.Context, obj *models.Scene) (*string, error) {
return &obj.ResolveDate().Date, nil
ret := resolveFuzzyDate(obj.Date)
if ret != nil {
return &ret.Date, nil
}
return nil, nil
}

func (r *sceneResolver) ReleaseDate(ctx context.Context, obj *models.Scene) (*string, error) {
return resolveFuzzyDate(&obj.Date.String, &obj.DateAccuracy.String), nil
return resolveNullString(obj.Date), nil
}

func (r *sceneResolver) Studio(ctx context.Context, obj *models.Scene) (*models.Studio, error) {
Expand Down
4 changes: 0 additions & 4 deletions pkg/api/resolver_model_scene_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ func (r *sceneEditResolver) Fingerprints(ctx context.Context, obj *models.SceneE
return ret, nil
}

func (r *sceneEditResolver) Date(ctx context.Context, obj *models.SceneEdit) (*string, error) {
return resolveFuzzyDate(obj.Date, obj.DateAccuracy), nil
}

func (r *sceneEditResolver) Images(ctx context.Context, obj *models.SceneEdit) ([]*models.Image, error) {
fac := r.getRepoFactory(ctx)
id, err := fac.Edit().FindSceneID(obj.EditID)
Expand Down
19 changes: 2 additions & 17 deletions pkg/api/scene_edit_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ func (s *sceneEditTestRunner) verifySceneEditDetails(input models.SceneEditDetai
c.strPtrStrPtr(input.Code, sceneDetails.Code, "Code")
c.uuidPtrUUIDPtr(input.StudioID, sceneDetails.StudioID, "StudioID")
c.intPtrInt64Ptr(input.Duration, sceneDetails.Duration, "Duration")

inputDate, inputAccuracy, _ := models.ParseFuzzyString(input.Date)
assert.Assert(s.t, inputAccuracy.Valid && (inputAccuracy.String == *sceneDetails.DateAccuracy), "DateAccuracy mismatch")
assert.Equal(s.t, inputDate.String, *sceneDetails.Date)
c.strPtrStrPtr(input.Date, sceneDetails.Date, "Date")

s.compareURLs(input.Urls, sceneDetails.AddedUrls)

Expand All @@ -119,19 +116,7 @@ func (s *sceneEditTestRunner) verifySceneEdit(input models.SceneEditDetailsInput
c.strPtrNullStr(input.Code, scene.Code, "Code")
c.uuidPtrNullUUID(input.StudioID, scene.StudioID, "StudioID")
c.intPtrNullInt64(input.Duration, scene.Duration, "Duration")

inputDate, inputAccuracy, _ := models.ParseFuzzyString(input.Date)
if input.Date == nil {
assert.Assert(s.t, !scene.DateAccuracy.Valid)
} else {
assert.Equal(s.t, inputAccuracy.String, scene.DateAccuracy.String)
}

if input.Date == nil {
assert.Assert(s.t, !scene.Date.Valid)
} else {
assert.Equal(s.t, inputDate.String, scene.Date.String)
}
c.strPtrNullStr(input.Date, scene.Date, "Date")

urls, _ := resolver.Urls(s.ctx, scene)
s.compareURLs(input.Urls, urls)
Expand Down
35 changes: 19 additions & 16 deletions pkg/api/sql_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"database/sql"
"fmt"

"github.com/stashapp/stash-box/pkg/models"
"github.com/stashapp/stash-box/pkg/utils"
Expand Down Expand Up @@ -31,25 +32,27 @@ func resolveNullInt64(value sql.NullInt64) (*int, error) {
return nil, nil
}

func resolveFuzzyDate(date *string, accuracy *string) *string {
if date == nil || *date == "" {
func resolveFuzzyDate(date sql.NullString) *models.FuzzyDate {
if !date.Valid {
return nil
}

resolvedAccuracy := models.DateAccuracyEnumDay.String()
if accuracy != nil && *accuracy != "" {
resolvedAccuracy = *accuracy
}

switch resolvedAccuracy {
case models.DateAccuracyEnumDay.String():
return date
case models.DateAccuracyEnumMonth.String():
ret := (*date)[0:7]
return &ret
case models.DateAccuracyEnumYear.String():
ret := (*date)[0:4]
return &ret
switch {
case len(date.String) == 4:
return &models.FuzzyDate{
Accuracy: models.DateAccuracyEnumYear,
Date: fmt.Sprintf("%s-01-01", date.String),
}
case len(date.String) == 7:
return &models.FuzzyDate{
Accuracy: models.DateAccuracyEnumMonth,
Date: fmt.Sprintf("%s-01", date.String),
}
case len(date.String) == 10:
return &models.FuzzyDate{
Accuracy: models.DateAccuracyEnumDay,
Date: date.String,
}
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/jmoiron/sqlx"
)

var appSchemaVersion uint = 36
var appSchemaVersion uint = 37

var databaseProviders map[string]databaseProvider

Expand Down
29 changes: 29 additions & 0 deletions pkg/database/migrations/postgres/37_date_columns.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- Transform scene date columns to single string column
ALTER TABLE "scenes" RENAME COLUMN "date" TO "date_old";
ALTER TABLE "scenes" ADD COLUMN "date" TEXT;

UPDATE "scenes" SET "date" = (
CASE
WHEN "date_accuracy" = 'DAY' THEN TO_CHAR("date_old", 'YYYY-MM-DD')
WHEN "date_accuracy" = 'MONTH' THEN TO_CHAR("date_old", 'YYYY-MM')
WHEN "date_accuracy" = 'YEAR' THEN TO_CHAR("date_old", 'YYYY')
END
);

ALTER TABLE "scenes" DROP COLUMN "date_old";
ALTER TABLE "scenes" DROP COLUMN "date_accuracy";

-- Transform performers birthdate columns to single string column
ALTER TABLE "performers" RENAME COLUMN "birthdate" TO "birthdate_old";
ALTER TABLE "performers" ADD COLUMN "birthdate" TEXT;

UPDATE "performers" SET "birthdate" = (
CASE
WHEN "birthdate_accuracy" = 'DAY' THEN TO_CHAR("birthdate_old", 'YYYY-MM-DD')
WHEN "birthdate_accuracy" = 'MONTH' THEN TO_CHAR("birthdate_old", 'YYYY-MM')
WHEN "birthdate_accuracy" = 'YEAR' THEN TO_CHAR("birthdate_old", 'YYYY')
END
);

ALTER TABLE "performers" DROP COLUMN "birthdate_old";
ALTER TABLE "performers" DROP COLUMN "birthdate_accuracy";
4 changes: 2 additions & 2 deletions pkg/models/extension_edit_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (e PerformerEditDetailsInput) PerformerEditFromDiff(orig Performer, inputAr
oldData.Gender, newData.Gender = ed.nullStringEnum(orig.Gender, e.Gender)
}
if e.Birthdate != nil || inputArgs.Field("birthdate").IsNull() {
oldData.Birthdate, oldData.BirthdateAccuracy, newData.Birthdate, newData.BirthdateAccuracy = ed.fuzzyDate(orig.Birthdate, orig.BirthdateAccuracy, e.Birthdate)
oldData.Birthdate, newData.Birthdate = ed.nullString(orig.Birthdate, e.Birthdate)
}
if e.Ethnicity != nil || inputArgs.Field("ethnicity").IsNull() {
oldData.Ethnicity, newData.Ethnicity = ed.nullStringEnum(orig.Ethnicity, e.Ethnicity)
Expand Down Expand Up @@ -183,7 +183,7 @@ func (e SceneEditDetailsInput) SceneEditFromDiff(orig Scene, inputArgs utils.Arg
oldData.Details, newData.Details = ed.nullString(orig.Details, e.Details)
}
if e.Date != nil || inputArgs.Field("date").IsNull() {
oldData.Date, oldData.DateAccuracy, newData.Date, newData.DateAccuracy = ed.fuzzyDate(orig.Date, orig.DateAccuracy, e.Date)
oldData.Date, newData.Date = ed.nullString(orig.Date, e.Date)
}
if e.StudioID != nil || inputArgs.Field("studio_id").IsNull() {
oldData.StudioID, newData.StudioID = ed.nullUUID(orig.StudioID, e.StudioID)
Expand Down
Loading

0 comments on commit 0de6696

Please sign in to comment.