Skip to content

Commit

Permalink
dialect/sql/sqlgraph: allow update single node without select
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Apr 14, 2021
1 parent 82277ac commit 712b6a5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dialect/sql/sqlgraph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,11 @@ func (u *updater) node(ctx context.Context, tx dialect.ExecQuerier) error {
if err := u.setExternalEdges(ctx, []driver.Value{id}, addEdges, clearEdges); err != nil {
return err
}
// Ignore querying the database when there's nothing
// to scan into it.
if u.ScanValues == nil {
return nil
}
selector := u.builder.Select(u.Node.Columns...).
From(u.builder.Table(u.Node.Table).Schema(u.Node.Schema)).
Where(sql.EQ(u.Node.ID.Column, u.Node.ID.Value))
Expand Down
24 changes: 24 additions & 0 deletions dialect/sql/sqlgraph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,30 @@ func TestUpdateNode(t *testing.T) {
}
}

func TestExecUpdateNode(t *testing.T) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
mock.ExpectBegin()
mock.ExpectExec(escape("UPDATE `users` SET `age` = ?, `name` = ? WHERE `id` = ?")).
WithArgs(30, "Ariel", 1).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
err = UpdateNode(context.Background(), sql.OpenDB("", db), &UpdateSpec{
Node: &NodeSpec{
Table: "users",
Columns: []string{"id", "name", "age"},
ID: &FieldSpec{Column: "id", Type: field.TypeInt, Value: 1},
},
Fields: FieldMut{
Set: []*FieldSpec{
{Column: "age", Type: field.TypeInt, Value: 30},
{Column: "name", Type: field.TypeString, Value: "Ariel"},
},
},
})
require.NoError(t, err)
}

func TestUpdateNodes(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 712b6a5

Please sign in to comment.