-
-
Notifications
You must be signed in to change notification settings - Fork 404
/
example_soft_delete_test.go
67 lines (52 loc) · 1.34 KB
/
example_soft_delete_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package pg_test
import (
"fmt"
"time"
"github.com/go-pg/pg/v10/types"
)
type CustomTime struct {
Time time.Time
}
var _ types.ValueScanner = (*CustomTime)(nil)
func (tm *CustomTime) ScanValue(rd types.Reader, n int) error {
var err error
tm.Time, err = types.ScanTime(rd, n)
return err
}
var _ types.ValueAppender = (*CustomTime)(nil)
func (tm *CustomTime) AppendValue(b []byte, flags int) ([]byte, error) {
return types.AppendTime(b, tm.Time, flags), nil
}
type Video struct {
Id int
Name string
DeletedAt CustomTime `pg:",soft_delete"`
}
func ExampleDB_Model_softDeleteCustom() {
video1 := &Video{
Id: 1,
}
_, err := pgdb.Model(video1).Insert()
panicIf(err)
// Soft delete.
_, err = pgdb.Model(video1).WherePK().Delete()
panicIf(err)
// Count visible videos.
count, err := pgdb.Model((*Video)(nil)).Count()
panicIf(err)
fmt.Println("count", count)
// Count soft deleted videos.
deletedCount, err := pgdb.Model((*Video)(nil)).Deleted().Count()
panicIf(err)
fmt.Println("deleted count", deletedCount)
// Actually delete the video.
_, err = pgdb.Model(video1).WherePK().ForceDelete()
panicIf(err)
// Count soft deleted videos.
deletedCount, err = pgdb.Model((*Video)(nil)).Deleted().Count()
panicIf(err)
fmt.Println("deleted count", deletedCount)
// Output: count 0
// deleted count 1
// deleted count 0
}