mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 04:14:39 +03:00
SQLite model refactoring (#3791)
* Remove ID from PerformerPartial * Separate studio model from sqlite model * Separate movie model from sqlite model * Separate tag model from sqlite model * Separate saved filter model from sqlite model * Separate scene marker model from sqlite model * Separate gallery chapter model from sqlite model * Move ErrNoRows checks into sqlite, improve empty result error messages * Move SQLiteDate and SQLiteTimestamp to sqlite * Use changesetTranslator everywhere, refactor for consistency * Make PerformerStore.DestroyImage private * Fix rating on movie create
This commit is contained in:
@@ -21,36 +21,27 @@ type FinderImageStashIDGetter interface {
|
||||
// ToJSON converts a Studio object into its JSON equivalent.
|
||||
func ToJSON(ctx context.Context, reader FinderImageStashIDGetter, studio *models.Studio) (*jsonschema.Studio, error) {
|
||||
newStudioJSON := jsonschema.Studio{
|
||||
Name: studio.Name,
|
||||
URL: studio.URL,
|
||||
Details: studio.Details,
|
||||
IgnoreAutoTag: studio.IgnoreAutoTag,
|
||||
CreatedAt: json.JSONTime{Time: studio.CreatedAt.Timestamp},
|
||||
UpdatedAt: json.JSONTime{Time: studio.UpdatedAt.Timestamp},
|
||||
CreatedAt: json.JSONTime{Time: studio.CreatedAt},
|
||||
UpdatedAt: json.JSONTime{Time: studio.UpdatedAt},
|
||||
}
|
||||
|
||||
if studio.Name.Valid {
|
||||
newStudioJSON.Name = studio.Name.String
|
||||
}
|
||||
|
||||
if studio.URL.Valid {
|
||||
newStudioJSON.URL = studio.URL.String
|
||||
}
|
||||
|
||||
if studio.Details.Valid {
|
||||
newStudioJSON.Details = studio.Details.String
|
||||
}
|
||||
|
||||
if studio.ParentID.Valid {
|
||||
parent, err := reader.Find(ctx, int(studio.ParentID.Int64))
|
||||
if studio.ParentID != nil {
|
||||
parent, err := reader.Find(ctx, *studio.ParentID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting parent studio: %v", err)
|
||||
}
|
||||
|
||||
if parent != nil {
|
||||
newStudioJSON.ParentStudio = parent.Name.String
|
||||
newStudioJSON.ParentStudio = parent.Name
|
||||
}
|
||||
}
|
||||
|
||||
if studio.Rating.Valid {
|
||||
newStudioJSON.Rating = int(studio.Rating.Int64)
|
||||
if studio.Rating != nil {
|
||||
newStudioJSON.Rating = *studio.Rating
|
||||
}
|
||||
|
||||
aliases, err := reader.GetAliases(ctx, studio.ID)
|
||||
|
||||
@@ -27,7 +27,7 @@ const (
|
||||
errParentStudioID = 12
|
||||
)
|
||||
|
||||
const (
|
||||
var (
|
||||
studioName = "testStudio"
|
||||
url = "url"
|
||||
details = "details"
|
||||
@@ -37,7 +37,7 @@ const (
|
||||
)
|
||||
|
||||
var parentStudio models.Studio = models.Studio{
|
||||
Name: models.NullString(parentStudioName),
|
||||
Name: parentStudioName,
|
||||
}
|
||||
|
||||
var imageBytes = []byte("imageBytes")
|
||||
@@ -59,22 +59,18 @@ var (
|
||||
|
||||
func createFullStudio(id int, parentID int) models.Studio {
|
||||
ret := models.Studio{
|
||||
ID: id,
|
||||
Name: models.NullString(studioName),
|
||||
URL: models.NullString(url),
|
||||
Details: models.NullString(details),
|
||||
CreatedAt: models.SQLiteTimestamp{
|
||||
Timestamp: createTime,
|
||||
},
|
||||
UpdatedAt: models.SQLiteTimestamp{
|
||||
Timestamp: updateTime,
|
||||
},
|
||||
Rating: models.NullInt64(rating),
|
||||
ID: id,
|
||||
Name: studioName,
|
||||
URL: url,
|
||||
Details: details,
|
||||
CreatedAt: createTime,
|
||||
UpdatedAt: updateTime,
|
||||
Rating: &rating,
|
||||
IgnoreAutoTag: autoTagIgnored,
|
||||
}
|
||||
|
||||
if parentID != 0 {
|
||||
ret.ParentID = models.NullInt64(int64(parentID))
|
||||
ret.ParentID = &parentID
|
||||
}
|
||||
|
||||
return ret
|
||||
@@ -82,13 +78,9 @@ func createFullStudio(id int, parentID int) models.Studio {
|
||||
|
||||
func createEmptyStudio(id int) models.Studio {
|
||||
return models.Studio{
|
||||
ID: id,
|
||||
CreatedAt: models.SQLiteTimestamp{
|
||||
Timestamp: createTime,
|
||||
},
|
||||
UpdatedAt: models.SQLiteTimestamp{
|
||||
Timestamp: updateTime,
|
||||
},
|
||||
ID: id,
|
||||
CreatedAt: createTime,
|
||||
UpdatedAt: updateTime,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package studio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
@@ -13,9 +12,8 @@ import (
|
||||
)
|
||||
|
||||
type NameFinderCreatorUpdater interface {
|
||||
FindByName(ctx context.Context, name string, nocase bool) (*models.Studio, error)
|
||||
Create(ctx context.Context, newStudio models.Studio) (*models.Studio, error)
|
||||
UpdateFull(ctx context.Context, updatedStudio models.Studio) (*models.Studio, error)
|
||||
NameFinderCreator
|
||||
Update(ctx context.Context, updatedStudio *models.Studio) error
|
||||
UpdateImage(ctx context.Context, studioID int, image []byte) error
|
||||
UpdateAliases(ctx context.Context, studioID int, aliases []string) error
|
||||
UpdateStashIDs(ctx context.Context, studioID int, stashIDs []models.StashID) error
|
||||
@@ -37,13 +35,13 @@ func (i *Importer) PreImport(ctx context.Context) error {
|
||||
|
||||
i.studio = models.Studio{
|
||||
Checksum: checksum,
|
||||
Name: sql.NullString{String: i.Input.Name, Valid: true},
|
||||
URL: sql.NullString{String: i.Input.URL, Valid: true},
|
||||
Details: sql.NullString{String: i.Input.Details, Valid: true},
|
||||
Name: i.Input.Name,
|
||||
URL: i.Input.URL,
|
||||
Details: i.Input.Details,
|
||||
IgnoreAutoTag: i.Input.IgnoreAutoTag,
|
||||
CreatedAt: models.SQLiteTimestamp{Timestamp: i.Input.CreatedAt.GetTime()},
|
||||
UpdatedAt: models.SQLiteTimestamp{Timestamp: i.Input.UpdatedAt.GetTime()},
|
||||
Rating: sql.NullInt64{Int64: int64(i.Input.Rating), Valid: true},
|
||||
CreatedAt: i.Input.CreatedAt.GetTime(),
|
||||
UpdatedAt: i.Input.UpdatedAt.GetTime(),
|
||||
Rating: &i.Input.Rating,
|
||||
}
|
||||
|
||||
if err := i.populateParentStudio(ctx); err != nil {
|
||||
@@ -82,13 +80,10 @@ func (i *Importer) populateParentStudio(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
i.studio.ParentID = sql.NullInt64{
|
||||
Int64: int64(parentID),
|
||||
Valid: true,
|
||||
}
|
||||
i.studio.ParentID = &parentID
|
||||
}
|
||||
} else {
|
||||
i.studio.ParentID = sql.NullInt64{Int64: int64(studio.ID), Valid: true}
|
||||
i.studio.ParentID = &studio.ID
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,14 +91,14 @@ func (i *Importer) populateParentStudio(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (i *Importer) createParentStudio(ctx context.Context, name string) (int, error) {
|
||||
newStudio := *models.NewStudio(name)
|
||||
newStudio := models.NewStudio(name)
|
||||
|
||||
created, err := i.ReaderWriter.Create(ctx, newStudio)
|
||||
err := i.ReaderWriter.Create(ctx, newStudio)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return created.ID, nil
|
||||
return newStudio.ID, nil
|
||||
}
|
||||
|
||||
func (i *Importer) PostImport(ctx context.Context, id int) error {
|
||||
@@ -146,19 +141,19 @@ func (i *Importer) FindExistingID(ctx context.Context) (*int, error) {
|
||||
}
|
||||
|
||||
func (i *Importer) Create(ctx context.Context) (*int, error) {
|
||||
created, err := i.ReaderWriter.Create(ctx, i.studio)
|
||||
err := i.ReaderWriter.Create(ctx, &i.studio)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating studio: %v", err)
|
||||
}
|
||||
|
||||
id := created.ID
|
||||
id := i.studio.ID
|
||||
return &id, nil
|
||||
}
|
||||
|
||||
func (i *Importer) Update(ctx context.Context, id int) error {
|
||||
studio := i.studio
|
||||
studio.ID = id
|
||||
_, err := i.ReaderWriter.UpdateFull(ctx, studio)
|
||||
err := i.ReaderWriter.Update(ctx, &studio)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error updating existing studio: %v", err)
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ func TestImporterPreImport(t *testing.T) {
|
||||
|
||||
assert.Nil(t, err)
|
||||
expectedStudio := createFullStudio(0, 0)
|
||||
expectedStudio.ParentID.Valid = false
|
||||
expectedStudio.ParentID = nil
|
||||
expectedStudio.Checksum = md5.FromString(studioName)
|
||||
assert.Equal(t, expectedStudio, i.studio)
|
||||
}
|
||||
@@ -88,7 +88,7 @@ func TestImporterPreImportWithParent(t *testing.T) {
|
||||
|
||||
err := i.PreImport(ctx)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, int64(existingStudioID), i.studio.ParentID.Int64)
|
||||
assert.Equal(t, existingStudioID, *i.studio.ParentID)
|
||||
|
||||
i.Input.ParentStudio = existingParentStudioErr
|
||||
err = i.PreImport(ctx)
|
||||
@@ -112,9 +112,10 @@ func TestImporterPreImportWithMissingParent(t *testing.T) {
|
||||
}
|
||||
|
||||
readerWriter.On("FindByName", ctx, missingParentStudioName, false).Return(nil, nil).Times(3)
|
||||
readerWriter.On("Create", ctx, mock.AnythingOfType("models.Studio")).Return(&models.Studio{
|
||||
ID: existingStudioID,
|
||||
}, nil)
|
||||
readerWriter.On("Create", ctx, mock.AnythingOfType("*models.Studio")).Run(func(args mock.Arguments) {
|
||||
s := args.Get(1).(*models.Studio)
|
||||
s.ID = existingStudioID
|
||||
}).Return(nil)
|
||||
|
||||
err := i.PreImport(ctx)
|
||||
assert.NotNil(t, err)
|
||||
@@ -126,7 +127,7 @@ func TestImporterPreImportWithMissingParent(t *testing.T) {
|
||||
i.MissingRefBehaviour = models.ImportMissingRefEnumCreate
|
||||
err = i.PreImport(ctx)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, int64(existingStudioID), i.studio.ParentID.Int64)
|
||||
assert.Equal(t, existingStudioID, *i.studio.ParentID)
|
||||
|
||||
readerWriter.AssertExpectations(t)
|
||||
}
|
||||
@@ -146,7 +147,7 @@ func TestImporterPreImportWithMissingParentCreateErr(t *testing.T) {
|
||||
}
|
||||
|
||||
readerWriter.On("FindByName", ctx, missingParentStudioName, false).Return(nil, nil).Once()
|
||||
readerWriter.On("Create", ctx, mock.AnythingOfType("models.Studio")).Return(nil, errors.New("Create error"))
|
||||
readerWriter.On("Create", ctx, mock.AnythingOfType("*models.Studio")).Return(errors.New("Create error"))
|
||||
|
||||
err := i.PreImport(ctx)
|
||||
assert.NotNil(t, err)
|
||||
@@ -227,11 +228,11 @@ func TestCreate(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
studio := models.Studio{
|
||||
Name: models.NullString(studioName),
|
||||
Name: studioName,
|
||||
}
|
||||
|
||||
studioErr := models.Studio{
|
||||
Name: models.NullString(studioNameErr),
|
||||
Name: studioNameErr,
|
||||
}
|
||||
|
||||
i := Importer{
|
||||
@@ -240,10 +241,11 @@ func TestCreate(t *testing.T) {
|
||||
}
|
||||
|
||||
errCreate := errors.New("Create error")
|
||||
readerWriter.On("Create", ctx, studio).Return(&models.Studio{
|
||||
ID: studioID,
|
||||
}, nil).Once()
|
||||
readerWriter.On("Create", ctx, studioErr).Return(nil, errCreate).Once()
|
||||
readerWriter.On("Create", ctx, &studio).Run(func(args mock.Arguments) {
|
||||
s := args.Get(1).(*models.Studio)
|
||||
s.ID = studioID
|
||||
}).Return(nil).Once()
|
||||
readerWriter.On("Create", ctx, &studioErr).Return(errCreate).Once()
|
||||
|
||||
id, err := i.Create(ctx)
|
||||
assert.Equal(t, studioID, *id)
|
||||
@@ -262,11 +264,11 @@ func TestUpdate(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
studio := models.Studio{
|
||||
Name: models.NullString(studioName),
|
||||
Name: studioName,
|
||||
}
|
||||
|
||||
studioErr := models.Studio{
|
||||
Name: models.NullString(studioNameErr),
|
||||
Name: studioNameErr,
|
||||
}
|
||||
|
||||
i := Importer{
|
||||
@@ -278,7 +280,7 @@ func TestUpdate(t *testing.T) {
|
||||
|
||||
// id needs to be set for the mock input
|
||||
studio.ID = studioID
|
||||
readerWriter.On("UpdateFull", ctx, studio).Return(nil, nil).Once()
|
||||
readerWriter.On("Update", ctx, &studio).Return(nil).Once()
|
||||
|
||||
err := i.Update(ctx, studioID)
|
||||
assert.Nil(t, err)
|
||||
@@ -287,7 +289,7 @@ func TestUpdate(t *testing.T) {
|
||||
|
||||
// need to set id separately
|
||||
studioErr.ID = errImageID
|
||||
readerWriter.On("UpdateFull", ctx, studioErr).Return(nil, errUpdate).Once()
|
||||
readerWriter.On("Update", ctx, &studioErr).Return(errUpdate).Once()
|
||||
|
||||
err = i.Update(ctx, errImageID)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
type NameFinderCreator interface {
|
||||
FindByName(ctx context.Context, name string, nocase bool) (*models.Studio, error)
|
||||
Create(ctx context.Context, newStudio models.Studio) (*models.Studio, error)
|
||||
Create(ctx context.Context, newStudio *models.Studio) error
|
||||
}
|
||||
|
||||
type NameExistsError struct {
|
||||
@@ -53,7 +53,7 @@ func EnsureStudioNameUnique(ctx context.Context, id int, name string, qb Queryer
|
||||
if sameNameStudio != nil && id != sameNameStudio.ID {
|
||||
return &NameUsedByAliasError{
|
||||
Name: name,
|
||||
OtherStudio: sameNameStudio.Name.String,
|
||||
OtherStudio: sameNameStudio.Name,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user