Skip to content

Commit

Permalink
Fix genUpdateColumns() should not ignore soft delete column when unsc…
Browse files Browse the repository at this point in the history
…oped (go-xorm#1051)
  • Loading branch information
elvinchan authored and lunny committed Jul 26, 2018
1 parent 545f30f commit 4b224e8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion session_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ func (session *Session) genUpdateColumns(bean interface{}) ([]string, []interfac
}
}

if col.IsDeleted || col.IsCreated {
if (col.IsDeleted && !session.statement.unscoped) || col.IsCreated {
continue
}

Expand Down
48 changes: 48 additions & 0 deletions session_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,54 @@ func TestCreatedUpdated2(t *testing.T) {
assert.True(t, s2.UpdateAt.Unix() > s2.CreateAt.Unix())
}

func TestDeletedUpdate(t *testing.T) {
assert.NoError(t, prepareEngine())

type DeletedUpdatedStruct struct {
Id int64
Name string
DeletedAt time.Time `xorm:"deleted"`
}

assertSync(t, new(DeletedUpdatedStruct))

var s = DeletedUpdatedStruct{
Name: "test",
}
cnt, err := testEngine.Insert(&s)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

cnt, err = testEngine.ID(s.Id).Delete(&DeletedUpdatedStruct{})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

s.DeletedAt = time.Time{}
cnt, err = testEngine.Unscoped().Nullable("deleted_at").Update(&s)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

var s1 DeletedUpdatedStruct
has, err := testEngine.ID(s.Id).Get(&s1)
assert.EqualValues(t, true, has)

cnt, err = testEngine.ID(s.Id).Delete(&DeletedUpdatedStruct{})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

cnt, err = testEngine.ID(s.Id).Cols("deleted_at").Update(&DeletedUpdatedStruct{})
assert.EqualValues(t, "No content found to be updated", err.Error())
assert.EqualValues(t, 0, cnt)

cnt, err = testEngine.ID(s.Id).Unscoped().Cols("deleted_at").Update(&DeletedUpdatedStruct{})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

var s2 DeletedUpdatedStruct
has, err = testEngine.ID(s.Id).Get(&s2)
assert.EqualValues(t, true, has)
}

func TestUpdateMapCondition(t *testing.T) {
assert.NoError(t, prepareEngine())

Expand Down

0 comments on commit 4b224e8

Please sign in to comment.