Skip to content

Commit

Permalink
fix: fix ForceDelete on live/undeleted rows
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Apr 20, 2022
1 parent 289cc63 commit 1a33250
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
21 changes: 21 additions & 0 deletions internal/dbtest/soft_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestSoftDelete(t *testing.T) {
{run: testSoftDeleteNilModel},
{run: testSoftDeleteAPI},
{run: testSoftDeleteBulk},
{run: testSoftDeleteForce},
}
testEachDB(t, func(t *testing.T, dbName string, db *bun.DB) {
for _, test := range tests {
Expand Down Expand Up @@ -104,6 +105,26 @@ func testSoftDeleteAPI(t *testing.T, db *bun.DB) {
require.Equal(t, 0, count)
}

func testSoftDeleteForce(t *testing.T, db *bun.DB) {
ctx := context.Background()

err := db.ResetModel(ctx, (*Video)(nil))
require.NoError(t, err)

video1 := &Video{
ID: 1,
}
_, err = db.NewInsert().Model(video1).Exec(ctx)
require.NoError(t, err)

_, err = db.NewDelete().Model(video1).WherePK().ForceDelete().Exec(ctx)
require.NoError(t, err)

count, err := db.NewSelect().Model((*Video)(nil)).WhereAllWithDeleted().Count(ctx)
require.NoError(t, err)
require.Equal(t, 0, count)
}

func testSoftDeleteBulk(t *testing.T, db *bun.DB) {
ctx := context.Background()

Expand Down
7 changes: 4 additions & 3 deletions query_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,14 @@ func (q *baseQuery) whereAllWithDeleted() {
q.setErr(err)
return
}
q.flags = q.flags.Set(allWithDeletedFlag)
q.flags = q.flags.Remove(deletedFlag)
q.flags = q.flags.Set(allWithDeletedFlag).Remove(deletedFlag)
}

func (q *baseQuery) isSoftDelete() bool {
if q.table != nil {
return q.table.SoftDeleteField != nil && !q.flags.Has(allWithDeletedFlag)
return q.table.SoftDeleteField != nil &&
!q.flags.Has(allWithDeletedFlag) &&
!q.flags.Has(forceDeleteFlag)
}
return false
}
Expand Down

0 comments on commit 1a33250

Please sign in to comment.