Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downstream OpCreate model propagation #1

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion bug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

"entgo.io/bug/ent"
"entgo.io/bug/ent/enttest"
_ "entgo.io/bug/ent/runtime"
"entgo.io/bug/ent/schema"
)

func TestBugSQLite(t *testing.T) {
Expand Down Expand Up @@ -57,8 +59,18 @@ func TestBugMaria(t *testing.T) {
func test(t *testing.T, client *ent.Client) {
ctx := context.Background()
client.User.Delete().ExecX(ctx)
client.User.Create().SetName("Ariel").SetAge(30).ExecX(ctx)
u := client.User.Create().SetName("Ariel").SetAge(30).SaveX(ctx)

if n := client.User.Query().CountX(ctx); n != 1 {
t.Errorf("unexpected number of users: %d", n)
}

expHashID, err := schema.HashUserID(u.ID)
if err != nil {
t.Errorf("Failed getting expected hash id for id %d: %s", u.ID, err)
}

if expHashID != u.HashID {
t.Errorf(`expected hash ID "%s" but got "%s" for id %d`, expHashID, u.HashID, u.ID)
}
}
3 changes: 2 additions & 1 deletion ent/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ent/migrate/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 78 additions & 2 deletions ent/mutation.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions ent/runtime.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion ent/runtime/runtime.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions ent/schema/user.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package schema

import (
"context"

"entgo.io/bug/ent/hook"
"entgo.io/ent"
"entgo.io/ent/schema/field"
"github.com/speps/go-hashids/v2"

gen "entgo.io/bug/ent"
)

// User holds the schema definition for the User entity.
Expand All @@ -13,6 +19,7 @@ type User struct {
// Fields of the User.
func (User) Fields() []ent.Field {
return []ent.Field{
field.String("hash_id").Optional(),
field.Int("age"),
field.String("name"),
}
Expand All @@ -22,3 +29,57 @@ func (User) Fields() []ent.Field {
func (User) Edges() []ent.Edge {
return nil
}

// Hooks of the User.
func (User) Hooks() []ent.Hook {
return []ent.Hook{
hook.On(func(next ent.Mutator) ent.Mutator {
// This hook hashes the new insert ID of the user and sets that as the "public ID" column.
return hook.UserFunc(func(ctx context.Context, m *gen.UserMutation) (ent.Value, error) {
// Because we rely on the database to set the ID, we cannot must allow the mutations to continue first
// so that the sql insert happens. After that, we can then hash the ID and update the newly created
// row.
v, err := next.Mutate(ctx, m)
if err != nil {
return nil, err
}

if id, ok := m.ID(); ok {
hashID, err := HashUserID(id)
if err != nil {
return nil, err
}

newUser, err := m.Client().User.UpdateOneID(id).SetHashID(hashID).Save(ctx)
if err != nil {
return nil, err
}

// We've updated the user and should return the new value of that user so that the call-site of
// client.User.Create()....Save(ctx) receives this updated value. The bug is that since we're doing a
// downstream mutation (e.g. mutating after the call to next.Mutate instead of before), this updated
// User value is not propagated to the call-site.
return newUser, nil
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 this is the bit that expresses this bug. Any value returned after calling next.Mutate is ignored.

}

return v, nil
})
}, ent.OpCreate),
}
}

func HashUserID(id int) (string, error) {
return hashID(id, "salt is so salty")
}

func hashID(id int, salt string) (string, error) {
hd := hashids.NewData()
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📓 I've added this dependency to make this bug example follow a real world use case instead of something contrived.

hd.Salt = salt
hd.MinLength = 30
h, err := hashids.NewWithData(hd)
if err != nil {
return "", err
}

return h.Encode([]int{id})
}
12 changes: 11 additions & 1 deletion ent/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions ent/user/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.