Skip to content

Commit

Permalink
Use created & updated instead BeforeInsert & BeforeUpdate (go-gitea#2482
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
lunny authored and lafriks committed Sep 13, 2017
1 parent 4c2b1be commit 005900b
Show file tree
Hide file tree
Showing 48 changed files with 481 additions and 679 deletions.
8 changes: 1 addition & 7 deletions models/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,7 @@ type Action struct {
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
Content string `xorm:"TEXT"`
Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"INDEX"`
}

// BeforeInsert will be invoked by XORM before inserting a record
// representing this object.
func (a *Action) BeforeInsert() {
a.CreatedUnix = time.Now().Unix()
CreatedUnix int64 `xorm:"INDEX created"`
}

// AfterSet updates the webhook object upon setting a column.
Expand Down
7 changes: 1 addition & 6 deletions models/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ type Notice struct {
Type NoticeType
Description string `xorm:"TEXT"`
Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"INDEX"`
}

// BeforeInsert is invoked from XORM before inserting an object of this type.
func (n *Notice) BeforeInsert() {
n.CreatedUnix = time.Now().Unix()
CreatedUnix int64 `xorm:"INDEX created"`
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
Expand Down
7 changes: 1 addition & 6 deletions models/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ type Attachment struct {
Name string
DownloadCount int64 `xorm:"DEFAULT 0"`
Created time.Time `xorm:"-"`
CreatedUnix int64
}

// BeforeInsert is invoked from XORM before inserting an object of this type.
func (a *Attachment) BeforeInsert() {
a.CreatedUnix = time.Now().Unix()
CreatedUnix int64 `xorm:"created"`
}

// AfterSet is invoked from XORM after setting the value of a field of
Expand Down
15 changes: 2 additions & 13 deletions models/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,9 @@ type ProtectedBranch struct {
BranchName string `xorm:"UNIQUE(s)"`
CanPush bool
Created time.Time `xorm:"-"`
CreatedUnix int64
CreatedUnix int64 `xorm:"created"`
Updated time.Time `xorm:"-"`
UpdatedUnix int64
}

// BeforeInsert before protected branch insert create and update time
func (protectBranch *ProtectedBranch) BeforeInsert() {
protectBranch.CreatedUnix = time.Now().Unix()
protectBranch.UpdatedUnix = protectBranch.CreatedUnix
}

// BeforeUpdate before protected branch update time
func (protectBranch *ProtectedBranch) BeforeUpdate() {
protectBranch.UpdatedUnix = time.Now().Unix()
UpdatedUnix int64 `xorm:"updated"`
}

// GetProtectedBranchByRepoID getting protected branch by repo ID
Expand Down
12 changes: 2 additions & 10 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,16 @@ type Issue struct {
Deadline time.Time `xorm:"-"`
DeadlineUnix int64 `xorm:"INDEX"`
Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"INDEX"`
CreatedUnix int64 `xorm:"INDEX created"`
Updated time.Time `xorm:"-"`
UpdatedUnix int64 `xorm:"INDEX"`
UpdatedUnix int64 `xorm:"INDEX updated"`

Attachments []*Attachment `xorm:"-"`
Comments []*Comment `xorm:"-"`
}

// BeforeInsert is invoked from XORM before inserting an object of this type.
func (issue *Issue) BeforeInsert() {
issue.CreatedUnix = time.Now().Unix()
issue.UpdatedUnix = issue.CreatedUnix
}

// BeforeUpdate is invoked from XORM before updating this object.
func (issue *Issue) BeforeUpdate() {
issue.UpdatedUnix = time.Now().Unix()
issue.DeadlineUnix = issue.Deadline.Unix()
}

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

func updateIssueCols(e Engine, issue *Issue, cols ...string) error {
cols = append(cols, "updated_unix")
if _, err := e.Id(issue.ID).Cols(cols...).Update(issue); err != nil {
return err
}
Expand Down
16 changes: 2 additions & 14 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ type Comment struct {
RenderedContent string `xorm:"-"`

Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"INDEX"`
CreatedUnix int64 `xorm:"INDEX created"`
Updated time.Time `xorm:"-"`
UpdatedUnix int64 `xorm:"INDEX"`
UpdatedUnix int64 `xorm:"INDEX updated"`

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

// BeforeInsert will be invoked by XORM before inserting a record
// representing this object.
func (c *Comment) BeforeInsert() {
c.CreatedUnix = time.Now().Unix()
c.UpdatedUnix = c.CreatedUnix
}

// BeforeUpdate is invoked from XORM before updating this object.
func (c *Comment) BeforeUpdate() {
c.UpdatedUnix = time.Now().Unix()
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
func (c *Comment) AfterSet(colName string, _ xorm.Cell) {
var err error
Expand Down
10 changes: 3 additions & 7 deletions models/lfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package models

import (
"errors"
"github.com/go-xorm/xorm"
"time"

"github.com/go-xorm/xorm"
)

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

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

// BeforeInsert sets the time at which the LFSMetaObject was created.
func (m *LFSMetaObject) BeforeInsert() {
m.CreatedUnix = time.Now().Unix()
}

// AfterSet stores the LFSMetaObject creation time in the database as local time.
func (m *LFSMetaObject) AfterSet(colName string, _ xorm.Cell) {
switch colName {
Expand Down
15 changes: 2 additions & 13 deletions models/login_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,9 @@ type LoginSource struct {
Cfg core.Conversion `xorm:"TEXT"`

Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"INDEX"`
CreatedUnix int64 `xorm:"INDEX created"`
Updated time.Time `xorm:"-"`
UpdatedUnix int64 `xorm:"INDEX"`
}

// BeforeInsert is invoked from XORM before inserting an object of this type.
func (source *LoginSource) BeforeInsert() {
source.CreatedUnix = time.Now().Unix()
source.UpdatedUnix = source.CreatedUnix
}

// BeforeUpdate is invoked from XORM before updating this object.
func (source *LoginSource) BeforeUpdate() {
source.UpdatedUnix = time.Now().Unix()
UpdatedUnix int64 `xorm:"INDEX updated"`
}

// Cell2Int64 converts a xorm.Cell type to int64,
Expand Down
1 change: 1 addition & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ func NewTestEngine(x *xorm.Engine) (err error) {

x.SetMapper(core.GonicMapper{})
x.SetLogger(log.XORMLogger)
x.ShowSQL(!setting.ProdMode)
return x.StoreEngine("InnoDB").Sync2(tables...)
}

Expand Down
15 changes: 2 additions & 13 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,9 @@ type Repository struct {
Size int64 `xorm:"NOT NULL DEFAULT 0"`

Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"INDEX"`
CreatedUnix int64 `xorm:"INDEX created"`
Updated time.Time `xorm:"-"`
UpdatedUnix int64 `xorm:"INDEX"`
}

// BeforeInsert is invoked from XORM before inserting an object of this type.
func (repo *Repository) BeforeInsert() {
repo.CreatedUnix = time.Now().Unix()
repo.UpdatedUnix = repo.CreatedUnix
}

// BeforeUpdate is invoked from XORM before updating this object.
func (repo *Repository) BeforeUpdate() {
repo.UpdatedUnix = time.Now().Unix()
UpdatedUnix int64 `xorm:"INDEX updated"`
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
Expand Down
16 changes: 12 additions & 4 deletions models/repo_mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,26 @@ type Mirror struct {

// BeforeInsert will be invoked by XORM before inserting a record
func (m *Mirror) BeforeInsert() {
m.UpdatedUnix = time.Now().Unix()
m.NextUpdateUnix = m.NextUpdate.Unix()
if m != nil {
m.UpdatedUnix = time.Now().Unix()
m.NextUpdateUnix = m.NextUpdate.Unix()
}
}

// BeforeUpdate is invoked from XORM before updating this object.
func (m *Mirror) BeforeUpdate() {
m.UpdatedUnix = time.Now().Unix()
m.NextUpdateUnix = m.NextUpdate.Unix()
if m != nil {
m.UpdatedUnix = time.Now().Unix()
m.NextUpdateUnix = m.NextUpdate.Unix()
}
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
func (m *Mirror) AfterSet(colName string, _ xorm.Cell) {
if m == nil {
return
}

var err error
switch colName {
case "repo_id":
Expand Down
36 changes: 8 additions & 28 deletions models/ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,11 @@ type PublicKey struct {
Type KeyType `xorm:"NOT NULL DEFAULT 1"`

Created time.Time `xorm:"-"`
CreatedUnix int64
CreatedUnix int64 `xorm:"created"`
Updated time.Time `xorm:"-"` // Note: Updated must below Created for AfterSet.
UpdatedUnix int64
HasRecentActivity bool `xorm:"-"`
HasUsed bool `xorm:"-"`
}

// BeforeInsert will be invoked by XORM before inserting a record
func (key *PublicKey) BeforeInsert() {
key.CreatedUnix = time.Now().Unix()
}

// BeforeUpdate is invoked from XORM before updating this object.
func (key *PublicKey) BeforeUpdate() {
key.UpdatedUnix = time.Now().Unix()
UpdatedUnix int64 `xorm:"updated"`
HasRecentActivity bool `xorm:"-"`
HasUsed bool `xorm:"-"`
}

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

Created time.Time `xorm:"-"`
CreatedUnix int64
CreatedUnix int64 `xorm:"created"`
Updated time.Time `xorm:"-"` // Note: Updated must below Created for AfterSet.
UpdatedUnix int64
HasRecentActivity bool `xorm:"-"`
HasUsed bool `xorm:"-"`
}

// BeforeInsert will be invoked by XORM before inserting a record
func (key *DeployKey) BeforeInsert() {
key.CreatedUnix = time.Now().Unix()
}

// BeforeUpdate is invoked from XORM before updating this object.
func (key *DeployKey) BeforeUpdate() {
key.UpdatedUnix = time.Now().Unix()
UpdatedUnix int64 `xorm:"updated"`
HasRecentActivity bool `xorm:"-"`
HasUsed bool `xorm:"-"`
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
Expand Down
15 changes: 2 additions & 13 deletions models/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,9 @@ type CommitStatus struct {
CreatorID int64

Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"INDEX"`
CreatedUnix int64 `xorm:"INDEX created"`
Updated time.Time `xorm:"-"`
UpdatedUnix int64 `xorm:"INDEX"`
}

// BeforeInsert is invoked from XORM before inserting an object of this type.
func (status *CommitStatus) BeforeInsert() {
status.CreatedUnix = time.Now().Unix()
status.UpdatedUnix = status.CreatedUnix
}

// BeforeUpdate is invoked from XORM before updating this object.
func (status *CommitStatus) BeforeUpdate() {
status.UpdatedUnix = time.Now().Unix()
UpdatedUnix int64 `xorm:"INDEX updated"`
}

// AfterSet is invoked from XORM after setting the value of a field of
Expand Down
14 changes: 2 additions & 12 deletions models/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,13 @@ type AccessToken struct {
Sha1 string `xorm:"UNIQUE VARCHAR(40)"`

Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"INDEX"`
CreatedUnix int64 `xorm:"INDEX created"`
Updated time.Time `xorm:"-"` // Note: Updated must below Created for AfterSet.
UpdatedUnix int64 `xorm:"INDEX"`
UpdatedUnix int64 `xorm:"INDEX updated"`
HasRecentActivity bool `xorm:"-"`
HasUsed bool `xorm:"-"`
}

// BeforeInsert will be invoked by XORM before inserting a record representing this object.
func (t *AccessToken) BeforeInsert() {
t.CreatedUnix = time.Now().Unix()
}

// BeforeUpdate is invoked from XORM before updating this object.
func (t *AccessToken) BeforeUpdate() {
t.UpdatedUnix = time.Now().Unix()
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
func (t *AccessToken) AfterSet(colName string, _ xorm.Cell) {
switch colName {
Expand Down
14 changes: 2 additions & 12 deletions models/twofactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,9 @@ type TwoFactor struct {
ScratchToken string

Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"INDEX"`
CreatedUnix int64 `xorm:"INDEX created"`
Updated time.Time `xorm:"-"` // Note: Updated must below Created for AfterSet.
UpdatedUnix int64 `xorm:"INDEX"`
}

// BeforeInsert will be invoked by XORM before inserting a record representing this object.
func (t *TwoFactor) BeforeInsert() {
t.CreatedUnix = time.Now().Unix()
}

// BeforeUpdate is invoked from XORM before updating this object.
func (t *TwoFactor) BeforeUpdate() {
t.UpdatedUnix = time.Now().Unix()
UpdatedUnix int64 `xorm:"INDEX updated"`
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
Expand Down
1 change: 1 addition & 0 deletions models/unit_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func CreateTestEngine(fixturesDir string) error {
if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
return err
}
x.ShowSQL(true)

return InitFixtures(&testfixtures.SQLite{}, fixturesDir)
}
Expand Down
Loading

0 comments on commit 005900b

Please sign in to comment.