From 0564dc9c9c8de943c56ac95818c562fbd1db6f41 Mon Sep 17 00:00:00 2001 From: StealWonders <6114858+StealWonders@users.noreply.github.com> Date: Fri, 14 Jun 2024 21:59:21 +0200 Subject: [PATCH] fix: make image dimensions non-null --- internal/database/ent/image.go | 22 +++++------- internal/database/ent/image/where.go | 10 ------ internal/database/ent/image_create.go | 35 +++---------------- internal/database/ent/image_update.go | 18 ---------- internal/database/ent/migrate/schema.go | 2 +- internal/database/ent/mutation.go | 29 ++------------- .../20240614062517_add_image_dimensions.sql | 2 -- .../20240614195816_add_image_dimensions.sql | 2 ++ internal/database/migrations/atlas.sum | 4 +-- internal/database/schema/image.go | 4 +-- internal/graphql/gqlserver/generated.go | 32 +++++++++++------ internal/graphql/models/generated.go | 4 +-- .../graphql/resolvers/mutations.resolvers.go | 12 ++----- internal/graphql/schema/inputs.graphql | 4 +-- internal/graphql/schema/types.graphql | 4 +-- 15 files changed, 54 insertions(+), 130 deletions(-) delete mode 100644 internal/database/migrations/20240614062517_add_image_dimensions.sql create mode 100644 internal/database/migrations/20240614195816_add_image_dimensions.sql diff --git a/internal/database/ent/image.go b/internal/database/ent/image.go index 9c64de3..57c0dc4 100644 --- a/internal/database/ent/image.go +++ b/internal/database/ent/image.go @@ -19,9 +19,9 @@ type Image struct { // ID of the ent. ID uuid.UUID `json:"id,omitempty"` // Width holds the value of the "width" field. - Width *int `json:"width,omitempty"` + Width int `json:"width,omitempty"` // Height holds the value of the "height" field. - Height *int `json:"height,omitempty"` + Height int `json:"height,omitempty"` // Edges holds the relations/edges for other nodes in the graph. // The values are being populated by the ImageQuery when eager-loading is set. Edges ImageEdges `json:"edges"` @@ -85,15 +85,13 @@ func (i *Image) assignValues(columns []string, values []any) error { if value, ok := values[j].(*sql.NullInt64); !ok { return fmt.Errorf("unexpected type %T for field width", values[j]) } else if value.Valid { - i.Width = new(int) - *i.Width = int(value.Int64) + i.Width = int(value.Int64) } case image.FieldHeight: if value, ok := values[j].(*sql.NullInt64); !ok { return fmt.Errorf("unexpected type %T for field height", values[j]) } else if value.Valid { - i.Height = new(int) - *i.Height = int(value.Int64) + i.Height = int(value.Int64) } case image.ForeignKeys[0]: if value, ok := values[j].(*sql.NullScanner); !ok { @@ -143,15 +141,11 @@ func (i *Image) String() string { var builder strings.Builder builder.WriteString("Image(") builder.WriteString(fmt.Sprintf("id=%v, ", i.ID)) - if v := i.Width; v != nil { - builder.WriteString("width=") - builder.WriteString(fmt.Sprintf("%v", *v)) - } + builder.WriteString("width=") + builder.WriteString(fmt.Sprintf("%v", i.Width)) builder.WriteString(", ") - if v := i.Height; v != nil { - builder.WriteString("height=") - builder.WriteString(fmt.Sprintf("%v", *v)) - } + builder.WriteString("height=") + builder.WriteString(fmt.Sprintf("%v", i.Height)) builder.WriteByte(')') return builder.String() } diff --git a/internal/database/ent/image/where.go b/internal/database/ent/image/where.go index 1f5c6c9..b4e8865 100644 --- a/internal/database/ent/image/where.go +++ b/internal/database/ent/image/where.go @@ -104,16 +104,6 @@ func WidthLTE(v int) predicate.Image { return predicate.Image(sql.FieldLTE(FieldWidth, v)) } -// WidthIsNil applies the IsNil predicate on the "width" field. -func WidthIsNil() predicate.Image { - return predicate.Image(sql.FieldIsNull(FieldWidth)) -} - -// WidthNotNil applies the NotNil predicate on the "width" field. -func WidthNotNil() predicate.Image { - return predicate.Image(sql.FieldNotNull(FieldWidth)) -} - // HeightEQ applies the EQ predicate on the "height" field. func HeightEQ(v int) predicate.Image { return predicate.Image(sql.FieldEQ(FieldHeight, v)) diff --git a/internal/database/ent/image_create.go b/internal/database/ent/image_create.go index 4477c93..0acc7c3 100644 --- a/internal/database/ent/image_create.go +++ b/internal/database/ent/image_create.go @@ -30,14 +30,6 @@ func (ic *ImageCreate) SetWidth(i int) *ImageCreate { return ic } -// SetNillableWidth sets the "width" field if the given value is not nil. -func (ic *ImageCreate) SetNillableWidth(i *int) *ImageCreate { - if i != nil { - ic.SetWidth(*i) - } - return ic -} - // SetHeight sets the "height" field. func (ic *ImageCreate) SetHeight(i int) *ImageCreate { ic.mutation.SetHeight(i) @@ -112,6 +104,9 @@ func (ic *ImageCreate) defaults() { // check runs all checks and user-defined validators on the builder. func (ic *ImageCreate) check() error { + if _, ok := ic.mutation.Width(); !ok { + return &ValidationError{Name: "width", err: errors.New(`ent: missing required field "Image.width"`)} + } if v, ok := ic.mutation.Width(); ok { if err := image.WidthValidator(v); err != nil { return &ValidationError{Name: "width", err: fmt.Errorf(`ent: validator failed for field "Image.width": %w`, err)} @@ -166,11 +161,11 @@ func (ic *ImageCreate) createSpec() (*Image, *sqlgraph.CreateSpec) { } if value, ok := ic.mutation.Width(); ok { _spec.SetField(image.FieldWidth, field.TypeInt, value) - _node.Width = &value + _node.Width = value } if value, ok := ic.mutation.Height(); ok { _spec.SetField(image.FieldHeight, field.TypeInt, value) - _node.Height = &value + _node.Height = value } if nodes := ic.mutation.ReviewIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ @@ -259,12 +254,6 @@ func (u *ImageUpsert) AddWidth(v int) *ImageUpsert { return u } -// ClearWidth clears the value of the "width" field. -func (u *ImageUpsert) ClearWidth() *ImageUpsert { - u.SetNull(image.FieldWidth) - return u -} - // SetHeight sets the "height" field. func (u *ImageUpsert) SetHeight(v int) *ImageUpsert { u.Set(image.FieldHeight, v) @@ -352,13 +341,6 @@ func (u *ImageUpsertOne) UpdateWidth() *ImageUpsertOne { }) } -// ClearWidth clears the value of the "width" field. -func (u *ImageUpsertOne) ClearWidth() *ImageUpsertOne { - return u.Update(func(s *ImageUpsert) { - s.ClearWidth() - }) -} - // SetHeight sets the "height" field. func (u *ImageUpsertOne) SetHeight(v int) *ImageUpsertOne { return u.Update(func(s *ImageUpsert) { @@ -616,13 +598,6 @@ func (u *ImageUpsertBulk) UpdateWidth() *ImageUpsertBulk { }) } -// ClearWidth clears the value of the "width" field. -func (u *ImageUpsertBulk) ClearWidth() *ImageUpsertBulk { - return u.Update(func(s *ImageUpsert) { - s.ClearWidth() - }) -} - // SetHeight sets the "height" field. func (u *ImageUpsertBulk) SetHeight(v int) *ImageUpsertBulk { return u.Update(func(s *ImageUpsert) { diff --git a/internal/database/ent/image_update.go b/internal/database/ent/image_update.go index 28f5b3f..4e33db1 100644 --- a/internal/database/ent/image_update.go +++ b/internal/database/ent/image_update.go @@ -50,12 +50,6 @@ func (iu *ImageUpdate) AddWidth(i int) *ImageUpdate { return iu } -// ClearWidth clears the value of the "width" field. -func (iu *ImageUpdate) ClearWidth() *ImageUpdate { - iu.mutation.ClearWidth() - return iu -} - // SetHeight sets the "height" field. func (iu *ImageUpdate) SetHeight(i int) *ImageUpdate { iu.mutation.ResetHeight() @@ -162,9 +156,6 @@ func (iu *ImageUpdate) sqlSave(ctx context.Context) (n int, err error) { if value, ok := iu.mutation.AddedWidth(); ok { _spec.AddField(image.FieldWidth, field.TypeInt, value) } - if iu.mutation.WidthCleared() { - _spec.ClearField(image.FieldWidth, field.TypeInt) - } if value, ok := iu.mutation.Height(); ok { _spec.SetField(image.FieldHeight, field.TypeInt, value) } @@ -241,12 +232,6 @@ func (iuo *ImageUpdateOne) AddWidth(i int) *ImageUpdateOne { return iuo } -// ClearWidth clears the value of the "width" field. -func (iuo *ImageUpdateOne) ClearWidth() *ImageUpdateOne { - iuo.mutation.ClearWidth() - return iuo -} - // SetHeight sets the "height" field. func (iuo *ImageUpdateOne) SetHeight(i int) *ImageUpdateOne { iuo.mutation.ResetHeight() @@ -383,9 +368,6 @@ func (iuo *ImageUpdateOne) sqlSave(ctx context.Context) (_node *Image, err error if value, ok := iuo.mutation.AddedWidth(); ok { _spec.AddField(image.FieldWidth, field.TypeInt, value) } - if iuo.mutation.WidthCleared() { - _spec.ClearField(image.FieldWidth, field.TypeInt) - } if value, ok := iuo.mutation.Height(); ok { _spec.SetField(image.FieldHeight, field.TypeInt, value) } diff --git a/internal/database/ent/migrate/schema.go b/internal/database/ent/migrate/schema.go index 4fb4936..488282a 100644 --- a/internal/database/ent/migrate/schema.go +++ b/internal/database/ent/migrate/schema.go @@ -44,7 +44,7 @@ var ( // ImageColumns holds the columns for the "image" table. ImageColumns = []*schema.Column{ {Name: "id", Type: field.TypeUUID}, - {Name: "width", Type: field.TypeInt, Nullable: true}, + {Name: "width", Type: field.TypeInt}, {Name: "height", Type: field.TypeInt}, {Name: "review", Type: field.TypeUUID}, } diff --git a/internal/database/ent/mutation.go b/internal/database/ent/mutation.go index d055420..bfe0652 100644 --- a/internal/database/ent/mutation.go +++ b/internal/database/ent/mutation.go @@ -1249,7 +1249,7 @@ func (m *ImageMutation) Width() (r int, exists bool) { // OldWidth returns the old "width" field's value of the Image entity. // If the Image object wasn't provided to the builder, the object is fetched from the database. // An error is returned if the mutation operation is not UpdateOne, or the database query fails. -func (m *ImageMutation) OldWidth(ctx context.Context) (v *int, err error) { +func (m *ImageMutation) OldWidth(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { return v, errors.New("OldWidth is only allowed on UpdateOne operations") } @@ -1281,24 +1281,10 @@ func (m *ImageMutation) AddedWidth() (r int, exists bool) { return *v, true } -// ClearWidth clears the value of the "width" field. -func (m *ImageMutation) ClearWidth() { - m.width = nil - m.addwidth = nil - m.clearedFields[image.FieldWidth] = struct{}{} -} - -// WidthCleared returns if the "width" field was cleared in this mutation. -func (m *ImageMutation) WidthCleared() bool { - _, ok := m.clearedFields[image.FieldWidth] - return ok -} - // ResetWidth resets all changes to the "width" field. func (m *ImageMutation) ResetWidth() { m.width = nil m.addwidth = nil - delete(m.clearedFields, image.FieldWidth) } // SetHeight sets the "height" field. @@ -1319,7 +1305,7 @@ func (m *ImageMutation) Height() (r int, exists bool) { // OldHeight returns the old "height" field's value of the Image entity. // If the Image object wasn't provided to the builder, the object is fetched from the database. // An error is returned if the mutation operation is not UpdateOne, or the database query fails. -func (m *ImageMutation) OldHeight(ctx context.Context) (v *int, err error) { +func (m *ImageMutation) OldHeight(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { return v, errors.New("OldHeight is only allowed on UpdateOne operations") } @@ -1541,11 +1527,7 @@ func (m *ImageMutation) AddField(name string, value ent.Value) error { // ClearedFields returns all nullable fields that were cleared during this // mutation. func (m *ImageMutation) ClearedFields() []string { - var fields []string - if m.FieldCleared(image.FieldWidth) { - fields = append(fields, image.FieldWidth) - } - return fields + return nil } // FieldCleared returns a boolean indicating if a field with the given name was @@ -1558,11 +1540,6 @@ func (m *ImageMutation) FieldCleared(name string) bool { // ClearField clears the value of the field with the given name. It returns an // error if the field is not defined in the schema. func (m *ImageMutation) ClearField(name string) error { - switch name { - case image.FieldWidth: - m.ClearWidth() - return nil - } return fmt.Errorf("unknown Image nullable field %s", name) } diff --git a/internal/database/migrations/20240614062517_add_image_dimensions.sql b/internal/database/migrations/20240614062517_add_image_dimensions.sql deleted file mode 100644 index 1fa4c32..0000000 --- a/internal/database/migrations/20240614062517_add_image_dimensions.sql +++ /dev/null @@ -1,2 +0,0 @@ --- Modify "image" table -ALTER TABLE "image" ADD COLUMN "width" bigint NULL, ADD COLUMN "height" bigint NOT NULL; diff --git a/internal/database/migrations/20240614195816_add_image_dimensions.sql b/internal/database/migrations/20240614195816_add_image_dimensions.sql new file mode 100644 index 0000000..f44fa82 --- /dev/null +++ b/internal/database/migrations/20240614195816_add_image_dimensions.sql @@ -0,0 +1,2 @@ +-- Modify "image" table +ALTER TABLE "image" ADD COLUMN "width" bigint NOT NULL, ADD COLUMN "height" bigint NOT NULL; diff --git a/internal/database/migrations/atlas.sum b/internal/database/migrations/atlas.sum index ea475b8..2a999d2 100644 --- a/internal/database/migrations/atlas.sum +++ b/internal/database/migrations/atlas.sum @@ -1,4 +1,4 @@ -h1:0SJpACBJf7MC5ntSNQ2uId9ycjBwoqpLK5SzG5A7pag= +h1:P9gL4/8NzeCC8A9H8HGlZuvWEzhqZou8KrMgxwtjukQ= 20231103235442_initial_database.sql h1:rXmCB2eMDP8LJVllwOgeBpNNXC5UyUBDx0T/gUx2Oyo= 20240202195658_remove_image_hash.sql h1:NKp3hBH792xsGZvlkinuBnr44L9Bi9a0RS5CO+tIDE4= -20240614062517_add_image_dimensions.sql h1:DvSf1670+WnBYu+3fa7KSb6UEHs3Swx715B1wmPOhoU= +20240614195816_add_image_dimensions.sql h1:CErHJ7Umz3qO3v0/EyxkvasRIp2zV042F/yaxozzh3w= diff --git a/internal/database/schema/image.go b/internal/database/schema/image.go index 47f0463..741f86e 100644 --- a/internal/database/schema/image.go +++ b/internal/database/schema/image.go @@ -25,8 +25,8 @@ func (Image) Annotations() []schema.Annotation { func (Image) Fields() []ent.Field { return []ent.Field{ field.UUID("id", uuid.UUID{}).Default(uuid.New).Immutable(), - field.Int("width").Positive().Optional().Nillable(), - field.Int("height").Positive().Nillable(), + field.Int("width").Positive(), + field.Int("height").Positive(), } } diff --git a/internal/graphql/gqlserver/generated.go b/internal/graphql/gqlserver/generated.go index 98abd43..c66009e 100644 --- a/internal/graphql/gqlserver/generated.go +++ b/internal/graphql/gqlserver/generated.go @@ -1445,8 +1445,8 @@ input DeleteImagesFromReviewInput { input UpdateDimensionsInput { id: UUID! - width: Int - height: Int + width: Int! + height: Int! } @@ -1644,8 +1644,8 @@ type Review { type Image { id: UUID! review: Review! - width: Int - height: Int + width: Int! + height: Int! } type User { @@ -2611,11 +2611,14 @@ func (ec *executionContext) _Image_width(ctx context.Context, field graphql.Coll return graphql.Null } if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } return graphql.Null } - res := resTmp.(*int) + res := resTmp.(int) fc.Result = res - return ec.marshalOInt2ᚖint(ctx, field.Selections, res) + return ec.marshalNInt2int(ctx, field.Selections, res) } func (ec *executionContext) fieldContext_Image_width(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { @@ -2652,11 +2655,14 @@ func (ec *executionContext) _Image_height(ctx context.Context, field graphql.Col return graphql.Null } if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } return graphql.Null } - res := resTmp.(*int) + res := resTmp.(int) fc.Result = res - return ec.marshalOInt2ᚖint(ctx, field.Selections, res) + return ec.marshalNInt2int(ctx, field.Selections, res) } func (ec *executionContext) fieldContext_Image_height(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { @@ -10515,14 +10521,14 @@ func (ec *executionContext) unmarshalInputUpdateDimensionsInput(ctx context.Cont it.ID = data case "width": ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("width")) - data, err := ec.unmarshalOInt2ᚖint(ctx, v) + data, err := ec.unmarshalNInt2int(ctx, v) if err != nil { return it, err } it.Width = data case "height": ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("height")) - data, err := ec.unmarshalOInt2ᚖint(ctx, v) + data, err := ec.unmarshalNInt2int(ctx, v) if err != nil { return it, err } @@ -11052,8 +11058,14 @@ func (ec *executionContext) _Image(ctx context.Context, sel ast.SelectionSet, ob out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) }) case "width": out.Values[i] = ec._Image_width(ctx, field, obj) + if out.Values[i] == graphql.Null { + atomic.AddUint32(&out.Invalids, 1) + } case "height": out.Values[i] = ec._Image_height(ctx, field, obj) + if out.Values[i] == graphql.Null { + atomic.AddUint32(&out.Invalids, 1) + } default: panic("unknown field " + strconv.Quote(field.Name)) } diff --git a/internal/graphql/models/generated.go b/internal/graphql/models/generated.go index 689e0b6..9680482 100644 --- a/internal/graphql/models/generated.go +++ b/internal/graphql/models/generated.go @@ -172,8 +172,8 @@ type Subscription struct { type UpdateDimensionsInput struct { ID uuid.UUID `json:"id"` - Width *int `json:"width,omitempty"` - Height *int `json:"height,omitempty"` + Width int `json:"width"` + Height int `json:"height"` } type UpdateDishInput struct { diff --git a/internal/graphql/resolvers/mutations.resolvers.go b/internal/graphql/resolvers/mutations.resolvers.go index 0cbfb34..8bef9b4 100644 --- a/internal/graphql/resolvers/mutations.resolvers.go +++ b/internal/graphql/resolvers/mutations.resolvers.go @@ -562,6 +562,8 @@ func (r *mutationResolver) AddImagesToReview(ctx context.Context, input models.A _, err := r.Database.Image.Create(). SetID(image). SetReviewID(review.ID). + SetWidth(0). // todo: read from image service + SetHeight(0). // todo: read from image service Save(ctx) if err != nil { continue // if one image fails to store, allow the remaining images to be stored @@ -605,15 +607,7 @@ func (r *mutationResolver) UpdateDimensions(ctx context.Context, input models.Up return nil, err } - queryBuilder := r.Database.Image.UpdateOne(image) - - if input.Width != nil { - queryBuilder = queryBuilder.SetWidth(*input.Width) - } - - if input.Height != nil { - queryBuilder = queryBuilder.SetHeight(*input.Height) - } + queryBuilder := r.Database.Image.UpdateOne(image).SetHeight(input.Height).SetWidth(input.Width) return queryBuilder.Save(ctx) } diff --git a/internal/graphql/schema/inputs.graphql b/internal/graphql/schema/inputs.graphql index df51ca8..0ecc99c 100644 --- a/internal/graphql/schema/inputs.graphql +++ b/internal/graphql/schema/inputs.graphql @@ -175,8 +175,8 @@ input DeleteImagesFromReviewInput { input UpdateDimensionsInput { id: UUID! - width: Int - height: Int + width: Int! + height: Int! } diff --git a/internal/graphql/schema/types.graphql b/internal/graphql/schema/types.graphql index 3a8b82f..8d303c9 100644 --- a/internal/graphql/schema/types.graphql +++ b/internal/graphql/schema/types.graphql @@ -105,8 +105,8 @@ type Review { type Image { id: UUID! review: Review! - width: Int - height: Int + width: Int! + height: Int! } type User {