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
This commit is contained in:
Lunny Xiao 2017-09-13 13:18:22 +08:00 committed by Lauris BH
parent 4c2b1be3a4
commit 005900baea
48 changed files with 519 additions and 717 deletions

View file

@ -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.

View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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()
}
@ -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
}

View file

@ -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)"`
@ -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

View file

@ -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.
@ -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.
@ -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 {

View file

@ -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,

View file

@ -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...)
}

View file

@ -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.

View file

@ -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":

View file

@ -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.
@ -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.

View file

@ -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

View file

@ -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 {

View file

@ -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.

View file

@ -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)
}

View file

@ -94,9 +94,9 @@ type User struct {
Salt string `xorm:"VARCHAR(10)"`
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"`
LastLogin time.Time `xorm:"-"`
LastLoginUnix int64 `xorm:"INDEX"`
@ -135,18 +135,11 @@ type User struct {
DiffViewStyle string `xorm:"NOT NULL DEFAULT ''"`
}
// BeforeInsert is invoked from XORM before inserting an object of this type.
func (u *User) BeforeInsert() {
u.CreatedUnix = time.Now().Unix()
u.UpdatedUnix = u.CreatedUnix
}
// BeforeUpdate is invoked from XORM before updating this object.
func (u *User) BeforeUpdate() {
if u.MaxRepoCreation < -1 {
u.MaxRepoCreation = -1
}
u.UpdatedUnix = time.Now().Unix()
}
// SetLastLogin set time to last login
@ -897,7 +890,6 @@ func UpdateUserCols(u *User, cols ...string) error {
u.Website = base.TruncateString(u.Website, 255)
u.Description = base.TruncateString(u.Description, 255)
cols = append(cols, "updated_unix")
_, err := x.Id(u.ID).Cols(cols...).Update(u)
return err
}

View file

@ -107,22 +107,9 @@ type Webhook struct {
LastStatus HookStatus // Last delivery status
Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"INDEX"`
CreatedUnix int64 `xorm:"INDEX created"`
Updated time.Time `xorm:"-"`
UpdatedUnix int64 `xorm:"INDEX"`
}
// BeforeInsert will be invoked by XORM before inserting a record
// representing this object
func (w *Webhook) BeforeInsert() {
w.CreatedUnix = time.Now().Unix()
w.UpdatedUnix = w.CreatedUnix
}
// BeforeUpdate will be invoked by XORM before updating a record
// representing this object
func (w *Webhook) BeforeUpdate() {
w.UpdatedUnix = time.Now().Unix()
UpdatedUnix int64 `xorm:"INDEX updated"`
}
// AfterSet updates the webhook object upon setting a column