Skip to content

Commit 005900b

Browse files
lunnylafriks
authored andcommitted
Use created & updated instead BeforeInsert & BeforeUpdate (#2482)
* use created & updated instead BeforeInsert & BeforeUpdate * fix vendor checksum * only show generated SQL when development mode * remove extra update column updated_unix * remove trace config
1 parent 4c2b1be commit 005900b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+481
-679
lines changed

models/action.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,7 @@ type Action struct {
8686
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
8787
Content string `xorm:"TEXT"`
8888
Created time.Time `xorm:"-"`
89-
CreatedUnix int64 `xorm:"INDEX"`
90-
}
91-
92-
// BeforeInsert will be invoked by XORM before inserting a record
93-
// representing this object.
94-
func (a *Action) BeforeInsert() {
95-
a.CreatedUnix = time.Now().Unix()
89+
CreatedUnix int64 `xorm:"INDEX created"`
9690
}
9791

9892
// AfterSet updates the webhook object upon setting a column.

models/admin.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ type Notice struct {
2929
Type NoticeType
3030
Description string `xorm:"TEXT"`
3131
Created time.Time `xorm:"-"`
32-
CreatedUnix int64 `xorm:"INDEX"`
33-
}
34-
35-
// BeforeInsert is invoked from XORM before inserting an object of this type.
36-
func (n *Notice) BeforeInsert() {
37-
n.CreatedUnix = time.Now().Unix()
32+
CreatedUnix int64 `xorm:"INDEX created"`
3833
}
3934

4035
// AfterSet is invoked from XORM after setting the value of a field of this object.

models/attachment.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,7 @@ type Attachment struct {
2828
Name string
2929
DownloadCount int64 `xorm:"DEFAULT 0"`
3030
Created time.Time `xorm:"-"`
31-
CreatedUnix int64
32-
}
33-
34-
// BeforeInsert is invoked from XORM before inserting an object of this type.
35-
func (a *Attachment) BeforeInsert() {
36-
a.CreatedUnix = time.Now().Unix()
31+
CreatedUnix int64 `xorm:"created"`
3732
}
3833

3934
// AfterSet is invoked from XORM after setting the value of a field of

models/branches.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,9 @@ type ProtectedBranch struct {
2222
BranchName string `xorm:"UNIQUE(s)"`
2323
CanPush bool
2424
Created time.Time `xorm:"-"`
25-
CreatedUnix int64
25+
CreatedUnix int64 `xorm:"created"`
2626
Updated time.Time `xorm:"-"`
27-
UpdatedUnix int64
28-
}
29-
30-
// BeforeInsert before protected branch insert create and update time
31-
func (protectBranch *ProtectedBranch) BeforeInsert() {
32-
protectBranch.CreatedUnix = time.Now().Unix()
33-
protectBranch.UpdatedUnix = protectBranch.CreatedUnix
34-
}
35-
36-
// BeforeUpdate before protected branch update time
37-
func (protectBranch *ProtectedBranch) BeforeUpdate() {
38-
protectBranch.UpdatedUnix = time.Now().Unix()
27+
UpdatedUnix int64 `xorm:"updated"`
3928
}
4029

4130
// GetProtectedBranchByRepoID getting protected branch by repo ID

models/issue.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,16 @@ type Issue struct {
5454
Deadline time.Time `xorm:"-"`
5555
DeadlineUnix int64 `xorm:"INDEX"`
5656
Created time.Time `xorm:"-"`
57-
CreatedUnix int64 `xorm:"INDEX"`
57+
CreatedUnix int64 `xorm:"INDEX created"`
5858
Updated time.Time `xorm:"-"`
59-
UpdatedUnix int64 `xorm:"INDEX"`
59+
UpdatedUnix int64 `xorm:"INDEX updated"`
6060

6161
Attachments []*Attachment `xorm:"-"`
6262
Comments []*Comment `xorm:"-"`
6363
}
6464

65-
// BeforeInsert is invoked from XORM before inserting an object of this type.
66-
func (issue *Issue) BeforeInsert() {
67-
issue.CreatedUnix = time.Now().Unix()
68-
issue.UpdatedUnix = issue.CreatedUnix
69-
}
70-
7165
// BeforeUpdate is invoked from XORM before updating this object.
7266
func (issue *Issue) BeforeUpdate() {
73-
issue.UpdatedUnix = time.Now().Unix()
7467
issue.DeadlineUnix = issue.Deadline.Unix()
7568
}
7669

@@ -581,7 +574,6 @@ func (issue *Issue) ReadBy(userID int64) error {
581574
}
582575

583576
func updateIssueCols(e Engine, issue *Issue, cols ...string) error {
584-
cols = append(cols, "updated_unix")
585577
if _, err := e.Id(issue.ID).Cols(cols...).Update(issue); err != nil {
586578
return err
587579
}

models/issue_comment.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ type Comment struct {
9999
RenderedContent string `xorm:"-"`
100100

101101
Created time.Time `xorm:"-"`
102-
CreatedUnix int64 `xorm:"INDEX"`
102+
CreatedUnix int64 `xorm:"INDEX created"`
103103
Updated time.Time `xorm:"-"`
104-
UpdatedUnix int64 `xorm:"INDEX"`
104+
UpdatedUnix int64 `xorm:"INDEX updated"`
105105

106106
// Reference issue in commit message
107107
CommitSHA string `xorm:"VARCHAR(40)"`
@@ -112,18 +112,6 @@ type Comment struct {
112112
ShowTag CommentTag `xorm:"-"`
113113
}
114114

115-
// BeforeInsert will be invoked by XORM before inserting a record
116-
// representing this object.
117-
func (c *Comment) BeforeInsert() {
118-
c.CreatedUnix = time.Now().Unix()
119-
c.UpdatedUnix = c.CreatedUnix
120-
}
121-
122-
// BeforeUpdate is invoked from XORM before updating this object.
123-
func (c *Comment) BeforeUpdate() {
124-
c.UpdatedUnix = time.Now().Unix()
125-
}
126-
127115
// AfterSet is invoked from XORM after setting the value of a field of this object.
128116
func (c *Comment) AfterSet(colName string, _ xorm.Cell) {
129117
var err error

models/lfs.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package models
22

33
import (
44
"errors"
5-
"github.com/go-xorm/xorm"
65
"time"
6+
7+
"github.com/go-xorm/xorm"
78
)
89

910
// LFSMetaObject stores metadata for LFS tracked files.
@@ -14,7 +15,7 @@ type LFSMetaObject struct {
1415
RepositoryID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
1516
Existing bool `xorm:"-"`
1617
Created time.Time `xorm:"-"`
17-
CreatedUnix int64
18+
CreatedUnix int64 `xorm:"created"`
1819
}
1920

2021
// LFSTokenResponse defines the JSON structure in which the JWT token is stored.
@@ -108,11 +109,6 @@ func RemoveLFSMetaObjectByOid(oid string) error {
108109
return sess.Commit()
109110
}
110111

111-
// BeforeInsert sets the time at which the LFSMetaObject was created.
112-
func (m *LFSMetaObject) BeforeInsert() {
113-
m.CreatedUnix = time.Now().Unix()
114-
}
115-
116112
// AfterSet stores the LFSMetaObject creation time in the database as local time.
117113
func (m *LFSMetaObject) AfterSet(colName string, _ xorm.Cell) {
118114
switch colName {

models/login_source.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,9 @@ type LoginSource struct {
148148
Cfg core.Conversion `xorm:"TEXT"`
149149

150150
Created time.Time `xorm:"-"`
151-
CreatedUnix int64 `xorm:"INDEX"`
151+
CreatedUnix int64 `xorm:"INDEX created"`
152152
Updated time.Time `xorm:"-"`
153-
UpdatedUnix int64 `xorm:"INDEX"`
154-
}
155-
156-
// BeforeInsert is invoked from XORM before inserting an object of this type.
157-
func (source *LoginSource) BeforeInsert() {
158-
source.CreatedUnix = time.Now().Unix()
159-
source.UpdatedUnix = source.CreatedUnix
160-
}
161-
162-
// BeforeUpdate is invoked from XORM before updating this object.
163-
func (source *LoginSource) BeforeUpdate() {
164-
source.UpdatedUnix = time.Now().Unix()
153+
UpdatedUnix int64 `xorm:"INDEX updated"`
165154
}
166155

167156
// Cell2Int64 converts a xorm.Cell type to int64,

models/models.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ func NewTestEngine(x *xorm.Engine) (err error) {
241241

242242
x.SetMapper(core.GonicMapper{})
243243
x.SetLogger(log.XORMLogger)
244+
x.ShowSQL(!setting.ProdMode)
244245
return x.StoreEngine("InnoDB").Sync2(tables...)
245246
}
246247

models/repo.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,20 +211,9 @@ type Repository struct {
211211
Size int64 `xorm:"NOT NULL DEFAULT 0"`
212212

213213
Created time.Time `xorm:"-"`
214-
CreatedUnix int64 `xorm:"INDEX"`
214+
CreatedUnix int64 `xorm:"INDEX created"`
215215
Updated time.Time `xorm:"-"`
216-
UpdatedUnix int64 `xorm:"INDEX"`
217-
}
218-
219-
// BeforeInsert is invoked from XORM before inserting an object of this type.
220-
func (repo *Repository) BeforeInsert() {
221-
repo.CreatedUnix = time.Now().Unix()
222-
repo.UpdatedUnix = repo.CreatedUnix
223-
}
224-
225-
// BeforeUpdate is invoked from XORM before updating this object.
226-
func (repo *Repository) BeforeUpdate() {
227-
repo.UpdatedUnix = time.Now().Unix()
216+
UpdatedUnix int64 `xorm:"INDEX updated"`
228217
}
229218

230219
// AfterSet is invoked from XORM after setting the value of a field of this object.

models/repo_mirror.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,26 @@ type Mirror struct {
4040

4141
// BeforeInsert will be invoked by XORM before inserting a record
4242
func (m *Mirror) BeforeInsert() {
43-
m.UpdatedUnix = time.Now().Unix()
44-
m.NextUpdateUnix = m.NextUpdate.Unix()
43+
if m != nil {
44+
m.UpdatedUnix = time.Now().Unix()
45+
m.NextUpdateUnix = m.NextUpdate.Unix()
46+
}
4547
}
4648

4749
// BeforeUpdate is invoked from XORM before updating this object.
4850
func (m *Mirror) BeforeUpdate() {
49-
m.UpdatedUnix = time.Now().Unix()
50-
m.NextUpdateUnix = m.NextUpdate.Unix()
51+
if m != nil {
52+
m.UpdatedUnix = time.Now().Unix()
53+
m.NextUpdateUnix = m.NextUpdate.Unix()
54+
}
5155
}
5256

5357
// AfterSet is invoked from XORM after setting the value of a field of this object.
5458
func (m *Mirror) AfterSet(colName string, _ xorm.Cell) {
59+
if m == nil {
60+
return
61+
}
62+
5563
var err error
5664
switch colName {
5765
case "repo_id":

models/ssh_key.go

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,11 @@ type PublicKey struct {
5555
Type KeyType `xorm:"NOT NULL DEFAULT 1"`
5656

5757
Created time.Time `xorm:"-"`
58-
CreatedUnix int64
58+
CreatedUnix int64 `xorm:"created"`
5959
Updated time.Time `xorm:"-"` // Note: Updated must below Created for AfterSet.
60-
UpdatedUnix int64
61-
HasRecentActivity bool `xorm:"-"`
62-
HasUsed bool `xorm:"-"`
63-
}
64-
65-
// BeforeInsert will be invoked by XORM before inserting a record
66-
func (key *PublicKey) BeforeInsert() {
67-
key.CreatedUnix = time.Now().Unix()
68-
}
69-
70-
// BeforeUpdate is invoked from XORM before updating this object.
71-
func (key *PublicKey) BeforeUpdate() {
72-
key.UpdatedUnix = time.Now().Unix()
60+
UpdatedUnix int64 `xorm:"updated"`
61+
HasRecentActivity bool `xorm:"-"`
62+
HasUsed bool `xorm:"-"`
7363
}
7464

7565
// AfterSet is invoked from XORM after setting the value of a field of this object.
@@ -633,21 +623,11 @@ type DeployKey struct {
633623
Content string `xorm:"-"`
634624

635625
Created time.Time `xorm:"-"`
636-
CreatedUnix int64
626+
CreatedUnix int64 `xorm:"created"`
637627
Updated time.Time `xorm:"-"` // Note: Updated must below Created for AfterSet.
638-
UpdatedUnix int64
639-
HasRecentActivity bool `xorm:"-"`
640-
HasUsed bool `xorm:"-"`
641-
}
642-
643-
// BeforeInsert will be invoked by XORM before inserting a record
644-
func (key *DeployKey) BeforeInsert() {
645-
key.CreatedUnix = time.Now().Unix()
646-
}
647-
648-
// BeforeUpdate is invoked from XORM before updating this object.
649-
func (key *DeployKey) BeforeUpdate() {
650-
key.UpdatedUnix = time.Now().Unix()
628+
UpdatedUnix int64 `xorm:"updated"`
629+
HasRecentActivity bool `xorm:"-"`
630+
HasUsed bool `xorm:"-"`
651631
}
652632

653633
// AfterSet is invoked from XORM after setting the value of a field of this object.

models/status.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,9 @@ type CommitStatus struct {
6666
CreatorID int64
6767

6868
Created time.Time `xorm:"-"`
69-
CreatedUnix int64 `xorm:"INDEX"`
69+
CreatedUnix int64 `xorm:"INDEX created"`
7070
Updated time.Time `xorm:"-"`
71-
UpdatedUnix int64 `xorm:"INDEX"`
72-
}
73-
74-
// BeforeInsert is invoked from XORM before inserting an object of this type.
75-
func (status *CommitStatus) BeforeInsert() {
76-
status.CreatedUnix = time.Now().Unix()
77-
status.UpdatedUnix = status.CreatedUnix
78-
}
79-
80-
// BeforeUpdate is invoked from XORM before updating this object.
81-
func (status *CommitStatus) BeforeUpdate() {
82-
status.UpdatedUnix = time.Now().Unix()
71+
UpdatedUnix int64 `xorm:"INDEX updated"`
8372
}
8473

8574
// AfterSet is invoked from XORM after setting the value of a field of

models/token.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,13 @@ type AccessToken struct {
2121
Sha1 string `xorm:"UNIQUE VARCHAR(40)"`
2222

2323
Created time.Time `xorm:"-"`
24-
CreatedUnix int64 `xorm:"INDEX"`
24+
CreatedUnix int64 `xorm:"INDEX created"`
2525
Updated time.Time `xorm:"-"` // Note: Updated must below Created for AfterSet.
26-
UpdatedUnix int64 `xorm:"INDEX"`
26+
UpdatedUnix int64 `xorm:"INDEX updated"`
2727
HasRecentActivity bool `xorm:"-"`
2828
HasUsed bool `xorm:"-"`
2929
}
3030

31-
// BeforeInsert will be invoked by XORM before inserting a record representing this object.
32-
func (t *AccessToken) BeforeInsert() {
33-
t.CreatedUnix = time.Now().Unix()
34-
}
35-
36-
// BeforeUpdate is invoked from XORM before updating this object.
37-
func (t *AccessToken) BeforeUpdate() {
38-
t.UpdatedUnix = time.Now().Unix()
39-
}
40-
4131
// AfterSet is invoked from XORM after setting the value of a field of this object.
4232
func (t *AccessToken) AfterSet(colName string, _ xorm.Cell) {
4333
switch colName {

models/twofactor.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,9 @@ type TwoFactor struct {
2626
ScratchToken string
2727

2828
Created time.Time `xorm:"-"`
29-
CreatedUnix int64 `xorm:"INDEX"`
29+
CreatedUnix int64 `xorm:"INDEX created"`
3030
Updated time.Time `xorm:"-"` // Note: Updated must below Created for AfterSet.
31-
UpdatedUnix int64 `xorm:"INDEX"`
32-
}
33-
34-
// BeforeInsert will be invoked by XORM before inserting a record representing this object.
35-
func (t *TwoFactor) BeforeInsert() {
36-
t.CreatedUnix = time.Now().Unix()
37-
}
38-
39-
// BeforeUpdate is invoked from XORM before updating this object.
40-
func (t *TwoFactor) BeforeUpdate() {
41-
t.UpdatedUnix = time.Now().Unix()
31+
UpdatedUnix int64 `xorm:"INDEX updated"`
4232
}
4333

4434
// AfterSet is invoked from XORM after setting the value of a field of this object.

models/unit_tests.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func CreateTestEngine(fixturesDir string) error {
2828
if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
2929
return err
3030
}
31+
x.ShowSQL(true)
3132

3233
return InitFixtures(&testfixtures.SQLite{}, fixturesDir)
3334
}

0 commit comments

Comments
 (0)