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

fix: fix ForceDelete on live/undeleted rows #516

Merged
merged 1 commit into from Apr 20, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions internal/dbtest/soft_delete_test.go
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
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