From 703b686bd13c9e6edf43f444d4da3e6175e424be Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Fri, 7 Aug 2026 16:09:15 +0800 Subject: [PATCH] fix: allow force update version of table --- debug.go | 3 ++- debug_test.go | 2 +- update.go | 8 +++++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/debug.go b/debug.go index 2bc3d9c..272ad6d 100644 --- a/debug.go +++ b/debug.go @@ -20,6 +20,7 @@ import ( "time" "yunion.io/x/log" + "yunion.io/x/pkg/util/timeutils" ) var ( @@ -47,7 +48,7 @@ func SQLPrintf(sqlstr string, variables []interface{}) string { case string: sqlstr = strings.Replace(sqlstr, "?", fmt.Sprintf("'%s'", v), 1) case time.Time: - sqlstr = strings.Replace(sqlstr, "?", fmt.Sprintf("'%s'", vv.UTC().Format(time.DateTime)), 1) + sqlstr = strings.Replace(sqlstr, "?", fmt.Sprintf("'%s'", timeutils.MysqlTime(vv.UTC())), 1) default: sqlstr = strings.Replace(sqlstr, "?", fmt.Sprintf("'%v'", v), 1) } diff --git a/debug_test.go b/debug_test.go index 89cbe79..e849838 100644 --- a/debug_test.go +++ b/debug_test.go @@ -34,7 +34,7 @@ func TestSqlDebug(t *testing.T) { 123, tm, }, - want: `SET a = 'name', b = 123, c = '2021-11-01 12:00:00 +0000 UTC'`, + want: `SET a = 'name', b = 123, c = '2021-11-01 12:00:00'`, }, } for _, c := range cases { diff --git a/update.go b/update.go index 727fd36..29820c7 100644 --- a/update.go +++ b/update.go @@ -130,6 +130,7 @@ func (us *SUpdateSession) SaveUpdateSql(dt interface{}) (*SUpdateSQLResult, erro updatedFields := make([]string, 0) primaries := make([]sPrimaryKeyValue, 0) setters := make([]SUpdateDiff, 0) + forceUpdate := false for _, c := range us.tableSpec.Columns() { k := c.Name() of, _ := ofields.GetInterface(k) @@ -159,14 +160,15 @@ func (us *SUpdateSession) SaveUpdateSql(dt interface{}) (*SUpdateSQLResult, erro } if c.IsAutoVersion() { versionFields = append(versionFields, k) - continue } if c.IsUpdatedAt() { updatedFields = append(updatedFields, k) - continue } if reflect.DeepEqual(of, nf) { continue + } else if c.IsAutoVersion() || c.IsUpdatedAt() { + forceUpdate = true + continue } if of != nil && nf != nil { ofJsonStr := jsonutils.Marshal(of).String() @@ -184,7 +186,7 @@ func (us *SUpdateSession) SaveUpdateSql(dt interface{}) (*SUpdateSQLResult, erro setters = append(setters, SUpdateDiff{old: of, new: nf, col: c}) } - if len(setters) == 0 { + if len(setters) == 0 && !forceUpdate { return nil, ErrNoDataToUpdate }