8000 dialect/sql/sqlgraph: support bulk-upadte for edge-schemas by a8m · Pull Request #2897 · ent/ent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

dialect/sql/sqlgraph: support bulk-upadte for edge-schemas #2897

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

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion dialect/sql/sqlgraph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,10 +741,27 @@ func (u *updater) nodes(ctx context.Context, drv dialect.Driver) (int, error) {
clearEdges = EdgeSpecs(u.Edges.Clear).GroupRel()
multiple = hasExternalEdges(addEdges, clearEdges)
update = u.builder.Update(u.Node.Table).Schema(u.Node.Schema)
selector = u.builder.Select(u.Node.ID.Column).
selector = u.builder.Select().
From(u.builder.Table(u.Node.Table).Schema(u.Node.Schema)).
WithContext(ctx)
)
switch {
// In case it is not an edge schema, the id holds the PK of
// the returned nodes are used for updating external tables.
case u.Node.ID != nil:
selector.Select(u.Node.ID.Column)
case len(u.Node.CompositeID) == 2:
// Other edge-schemas (M2M tables) cannot be updated by this operation.
// Also, in case there is a need to update an external foreign-key, it must
// be a single value and the user should use the "update by id" API instead.
if multiple {
return 0, fmt.Errorf("sql/sqlgraph: update edge schema table %q cannot update external tables", u.Node.Table)
}
case len(u.Node.CompositeID) != 2:
return 0, fmt.Errorf("sql/sqlgraph: invalid composite id for update table %q", u.Node.Table)
default:
return 0, fmt.Errorf("sql/sqlgraph: missing node id for update table %q", u.Node.Table)
}
if err := u.setTableColumns(update, addEdges, clearEdges); err != nil {
return 0, err
}
Expand Down
23 changes: 23 additions & 0 deletions dialect/sql/sqlgraph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2122,6 +2122,29 @@ func TestUpdateNodes(t *testing.T) {
},
wantAffected: 2,
},
{
name: "m2m_edge_schema",
spec: &UpdateSpec{
Node: &NodeSpec{
Table: "users",
CompositeID: []*FieldSpec{{Column: "user_id", Type: field.TypeInt}, {Column: "group_id", Type: field.TypeInt}},
},
Predicate: func(s *sql.Selector) {
s.Where(sql.EQ("version", 1))
},
Fields: FieldMut{
Add: []*FieldSpec{
{Column: "version", Type: field.TypeInt, Value: 1},
},
},
},
prepare: func(mock sqlmock.Sqlmock) {
mock.ExpectExec(escape("UPDATE `users` SET `version` = COALESCE(`users`.`version`, 0) + ? WHERE `version` = ?")).
WithArgs(1, 1).
WillReturnResult(sqlmock.NewResult(0, 4))
},
wantAffected: 4,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions entc/integration/edgeschema/edgeschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ func TestEdgeSchemaCompositeID(t *testing.T) {
require.Equal(t, likes[3].UserID, nat.ID)
require.Equal(t, likes[3].TweetID, tweets[2].ID)
require.NotZero(t, likes[3].LikedAt)

affected, err := client.TweetLike.Update().SetLikedAt(time.Now()).Save(ctx)
require.NoError(t, err)
require.Equal(t, client.TweetLike.Query().CountX(ctx), affected, "should update all edges (table rows)")
}

func TestEdgeSchemaDefaultID(t *testing.T) {
Expand Down
0