-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathschema_test.go
More file actions
147 lines (121 loc) · 3.73 KB
/
schema_test.go
File metadata and controls
147 lines (121 loc) · 3.73 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package pgkit_test
import (
"fmt"
"strings"
"time"
"github.com/goware/pgkit/v2/dbtype"
)
type Account struct {
ID int64 `db:"id,omitempty"`
Name string `db:"name"`
Disabled bool `db:"disabled"`
CreatedAt time.Time `db:"created_at,omitempty"` // ,omitempty will rely on postgres DEFAULT
UpdatedAt time.Time `db:"updated_at,omitempty"` // ,omitempty will rely on postgres DEFAULT
}
func (a *Account) DBTableName() string { return "accounts" }
func (a *Account) GetID() int64 { return a.ID }
func (a *Account) SetUpdatedAt(t time.Time) { a.UpdatedAt = t }
func (a *Account) Validate() error {
if a.Name == "" {
return fmt.Errorf("name is required")
}
return nil
}
type Article struct {
ID uint64 `db:"id,omitempty"`
Author string `db:"author"`
Alias *string `db:"alias"`
Content Content `db:"content"` // using JSONB postgres datatype
AccountID int64 `db:"account_id"`
CreatedAt time.Time `db:"created_at,omitempty"` // ,omitempty will rely on postgres DEFAULT
UpdatedAt time.Time `db:"updated_at,omitempty"` // ,omitempty will rely on postgres DEFAULT
DeletedAt *time.Time `db:"deleted_at"`
}
func (a *Article) GetID() uint64 { return a.ID }
func (a *Article) SetUpdatedAt(t time.Time) { a.UpdatedAt = t }
func (a *Article) SetDeletedAt(t time.Time) {
if t.IsZero() {
a.DeletedAt = nil
return
}
a.DeletedAt = &t
}
func (a *Article) Validate() error {
if a.Author == "" {
return fmt.Errorf("author is required")
}
return nil
}
type Content struct {
Title string `json:"title"`
Body string `json:"body"`
Views int64 `json:"views"`
}
type Review struct {
ID uint64 `db:"id,omitempty"`
Comment string `db:"comment"`
Status ReviewStatus `db:"status"`
Sentiment int64 `db:"sentiment"`
AccountID int64 `db:"account_id"`
ArticleID uint64 `db:"article_id"`
ProcessedAt *time.Time `db:"processed_at"`
CreatedAt time.Time `db:"created_at,omitempty"` // ,omitempty will rely on postgres DEFAULT
UpdatedAt time.Time `db:"updated_at,omitempty"` // ,omitempty will rely on postgres DEFAULT
DeletedAt *time.Time `db:"deleted_at"`
}
func (r *Review) GetID() uint64 { return r.ID }
func (r *Review) SetUpdatedAt(t time.Time) { r.UpdatedAt = t }
func (r *Review) SetDeletedAt(t time.Time) {
if t.IsZero() {
r.DeletedAt = nil
return
}
r.DeletedAt = &t
}
func (r *Review) Validate() error {
if len(r.Comment) < 3 {
return fmt.Errorf("comment too short")
}
return nil
}
type ReviewStatus int64
const (
ReviewStatusPending ReviewStatus = iota
ReviewStatusProcessing
ReviewStatusApproved
ReviewStatusRejected
ReviewStatusFailed
)
type Log struct {
ID int64 `db:"id,omitempty"`
Message string `db:"message"`
// RawData []byte `db:"raw_data"`
RawData dbtype.HexBytes `db:"raw_data"`
Etc map[string]interface{} `db:"etc"` // using JSONB postgres datatype
}
type Stat struct {
ID int64 `db:"id,omitempty"`
Key string `db:"key"`
Num dbtype.BigInt `db:"big_num"` // using NUMERIC(78,0) postgres datatype
Rating dbtype.BigInt `db:"rating"` // using NUMERIC(78,0) postgres datatype
}
type AccountWithHook struct {
Account
UpperName string `db:"-"`
}
func (a *AccountWithHook) AfterScan() error {
a.UpperName = strings.ToUpper(a.Name)
return nil
}
func (a *AccountWithHook) DBTableName() string { return "accounts" }
var errAfterScanBoom = fmt.Errorf("after scan boom")
type AccountWithFailingHook struct {
Account
}
func (a *AccountWithFailingHook) AfterScan() error {
if a.Name == "fail" {
return errAfterScanBoom
}
return nil
}
func (a *AccountWithFailingHook) DBTableName() string { return "accounts" }