fix(common): metadata update key

This commit is contained in:
ioito
2022-01-19 19:08:27 +08:00
parent 88823c092f
commit cb9bcd9586
12 changed files with 59 additions and 28 deletions

4
go.mod
View File

@@ -152,11 +152,11 @@ require (
k8s.io/client-go v0.19.3
k8s.io/cluster-bootstrap v0.19.3
yunion.io/x/executor v0.0.0-20211018100936-39a2cd966656
yunion.io/x/jsonutils v0.0.0-20211213041607-f6733e38783e
yunion.io/x/jsonutils v0.0.0-20220106020632-953b71a4c3a8
yunion.io/x/log v0.0.0-20201210064738-43181789dc74
yunion.io/x/ovsdb v0.0.0-20200526071744-27bf0940cbc7
yunion.io/x/pkg v0.0.0-20211116020154-6a76ba2f7e97
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e
yunion.io/x/sqlchemy v0.0.0-20211226083340-1ba177bc58be
yunion.io/x/sqlchemy v0.0.0-20220119131341-fadec84c0638
yunion.io/x/structarg v0.0.0-20200720093445-9f850fa222ce
)

8
go.sum
View File

@@ -942,8 +942,8 @@ sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
yunion.io/x/executor v0.0.0-20211018100936-39a2cd966656 h1:0zlZD5uhZoIHgLVAWCz2aHaYk2ZrNsACCYD7R6EIBII=
yunion.io/x/executor v0.0.0-20211018100936-39a2cd966656/go.mod h1:Uxuou9WQIeJXNpy7t2fPLL0BYLvLiMvGQwY7Qc6aSws=
yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051/go.mod h1:4N0/RVzsYL3kH3WE/H1BjUQdFiWu50JGCFQuuy+Z634=
yunion.io/x/jsonutils v0.0.0-20211213041607-f6733e38783e h1:zOVxZ0HrDOHLTm5YxkDKUm1x5jMVKbQ/iBSAyn5l1+Q=
yunion.io/x/jsonutils v0.0.0-20211213041607-f6733e38783e/go.mod h1:p0nyMqGA/apTxxyLIU/o1k4V7Vujl2O6ey30L594sYE=
yunion.io/x/jsonutils v0.0.0-20220106020632-953b71a4c3a8 h1:yFJVMnUoolC8mlUxa+LMTObYmqJ2H7iyxGIaXxT1fOc=
yunion.io/x/jsonutils v0.0.0-20220106020632-953b71a4c3a8/go.mod h1:p0nyMqGA/apTxxyLIU/o1k4V7Vujl2O6ey30L594sYE=
yunion.io/x/log v0.0.0-20190514041436-04ce53b17c6b/go.mod h1:+gauLs73omeJAPlsXcevLsJLKixV+sR/E7WSYTSx1fE=
yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d/go.mod h1:LC6f/4FozL0iaAbnFt2eDX9jlsyo3WiOUPm03d7+U4U=
yunion.io/x/log v0.0.0-20201210064738-43181789dc74 h1:7D+sQ/XaUTUEm+NCrKXOhXBKlzEd0RyS2qZ4vBGjx2o=
@@ -957,7 +957,7 @@ yunion.io/x/pkg v0.0.0-20211116020154-6a76ba2f7e97 h1:lY+5NlWwO2aIkfNnVOGKHmZvMt
yunion.io/x/pkg v0.0.0-20211116020154-6a76ba2f7e97/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E=
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e h1:v+EzIadodSwkdZ/7bremd7J8J50Cise/HCylsOJngmo=
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e/go.mod h1:0iFKpOs1y4lbCxeOmq3Xx/0AcQoewVPwj62eRluioEo=
yunion.io/x/sqlchemy v0.0.0-20211226083340-1ba177bc58be h1:LCmMlLgRLr6EmnMQtzbsys6QGY2wfFk+1IOgb2tG/7E=
yunion.io/x/sqlchemy v0.0.0-20211226083340-1ba177bc58be/go.mod h1:FTdwPdGhMgh4E+UFXc9klI1Ok34fMuybTT+jLhOaIjI=
yunion.io/x/sqlchemy v0.0.0-20220119131341-fadec84c0638 h1:Tc7bMp/mahI1Zua5dxBNrXSKl4hCUSA7CPBcqBnK8Uw=
yunion.io/x/sqlchemy v0.0.0-20220119131341-fadec84c0638/go.mod h1:FTdwPdGhMgh4E+UFXc9klI1Ok34fMuybTT+jLhOaIjI=
yunion.io/x/structarg v0.0.0-20200720093445-9f850fa222ce h1:kU8xE7O5uZ1GSJVMZHoJ+jrNL7csUQHYGyAPW9QfNpE=
yunion.io/x/structarg v0.0.0-20200720093445-9f850fa222ce/go.mod h1:EP6NSv2C0zzqBDTKumv8hPWLb3XvgMZDHQRfyuOrQng=

View File

@@ -576,6 +576,7 @@ func (manager *SMetadataManager) rawSetValues(ctx context.Context, objType strin
rV, rD := record.Value, record.Deleted
_, err = Update(&record, func() error {
record.Value = newRecord.Value
record.Key = key
record.Deleted = newRecord.Deleted
return nil
})
@@ -675,7 +676,7 @@ func (manager *SMetadataManager) rawGetAll(objType, objId string, keys []string,
ret := make(map[string]string)
for _, rec := range records {
if len(rec.Value) > 0 || strings.HasPrefix(rec.Key, USER_TAG_PREFIX) || strings.HasPrefix(rec.Key, CLOUD_TAG_PREFIX) {
ret[strings.ToLower(rec.Key)] = rec.Value
ret[rec.Key] = rec.Value
}
}
return ret, nil

View File

@@ -20,6 +20,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"
"yunion.io/x/onecloud/pkg/cloudprovider"
)
@@ -97,28 +98,45 @@ func (self *SRegion) UntagResource(serviceType string, resourceType string, resI
}
func (self *SRegion) SetResourceTags(serviceType string, resourceType string, resId string, tags map[string]string, replace bool) error {
err := self.TagResource(serviceType, resourceType, resId, tags)
if err != nil {
return errors.Wrapf(err, "TagResource")
}
if !replace || len(tags) == 0 {
return nil
}
_, _tags, err := self.ListSysAndUserTags(serviceType, resourceType, resId)
if err != nil {
return errors.Wrapf(err, "ListTags")
}
tagMaps := map[string]string{}
for k, v := range tags {
tagMaps[strings.ToLower(k)] = v
keys, upperKeys := []string{}, []string{}
for k := range tags {
keys = append(keys, k)
upperKeys = append(upperKeys, strings.ToUpper(k))
}
keys := []string{}
for k := range _tags {
if _, ok := tagMaps[strings.ToLower(k)]; !ok {
keys = append(keys, k)
if replace {
if len(tags) > 0 {
removeKeys := []string{}
for k := range _tags {
if !utils.IsInStringArray(k, keys) {
removeKeys = append(removeKeys, k)
}
}
if len(removeKeys) > 0 {
err := self.UntagResource(serviceType, resourceType, resId, removeKeys)
if err != nil {
return errors.Wrapf(err, "UntagResource")
}
}
}
} else {
removeKeys := []string{}
for k := range _tags {
if !utils.IsInStringArray(k, keys) && utils.IsInStringArray(strings.ToUpper(k), upperKeys) {
removeKeys = append(removeKeys, k)
}
}
if len(removeKeys) > 0 {
err := self.UntagResource(serviceType, resourceType, resId, removeKeys)
if err != nil {
return errors.Wrapf(err, "UntagResource")
}
}
}
return self.UntagResource(serviceType, resourceType, resId, keys)
return self.TagResource(serviceType, resourceType, resId, tags)
}
func (self *SRegion) TagResource(serviceType string, resourceType string, resourceId string, tags map[string]string) error {

4
vendor/modules.txt vendored
View File

@@ -1186,7 +1186,7 @@ sigs.k8s.io/yaml
yunion.io/x/executor/apis
yunion.io/x/executor/client
yunion.io/x/executor/server
# yunion.io/x/jsonutils v0.0.0-20211213041607-f6733e38783e
# yunion.io/x/jsonutils v0.0.0-20220106020632-953b71a4c3a8
yunion.io/x/jsonutils
# yunion.io/x/log v0.0.0-20201210064738-43181789dc74
yunion.io/x/log
@@ -1229,7 +1229,7 @@ yunion.io/x/pkg/util/workqueue
yunion.io/x/pkg/utils
# yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e
yunion.io/x/s3cli
# yunion.io/x/sqlchemy v0.0.0-20211226083340-1ba177bc58be
# yunion.io/x/sqlchemy v0.0.0-20220119131341-fadec84c0638
yunion.io/x/sqlchemy
yunion.io/x/sqlchemy/backends
yunion.io/x/sqlchemy/backends/clickhouse

View File

@@ -112,7 +112,7 @@ func (this *JSONDict) Add(o JSONObject, keys ...string) error {
obj.Set(keys[i], o)
} else {
o, ok := obj.data.Get(keys[i])
if !ok {
if !ok || o == JSONNull {
obj.Set(keys[i], NewDict())
o, ok = obj.data.Get(keys[i])
}

View File

@@ -77,7 +77,7 @@ func (clickhouse *SClickhouseBackend) CommitTableChangeSQL(ts sqlchemy.ITableSpe
/* IGNORE DROP STATEMENT */
for _, col := range changes.RemoveColumns {
sql := fmt.Sprintf("DROP COLUMN `%s`", col.Name())
log.Infof("ALTER TABLE %s %s;", ts.Name(), sql)
log.Debugf("skip ALTER TABLE %s %s;", ts.Name(), sql)
// alters = append(alters, sql)
// ignore drop statement
// if the column is auto_increment integer column,

View File

@@ -61,7 +61,7 @@ func (mysql *SMySQLBackend) CommitTableChangeSQL(ts sqlchemy.ITableSpec, changes
/* IGNORE DROP STATEMENT */
for _, col := range changes.RemoveColumns {
sql := fmt.Sprintf("DROP COLUMN `%s`", col.Name())
log.Infof("ALTER TABLE %s %s;", ts.Name(), sql)
log.Debugf("skip ALTER TABLE %s %s;", ts.Name(), sql)
// alters = append(alters, sql)
// ignore drop statement
// if the column is auto_increment integer column,

View File

@@ -61,7 +61,7 @@ func (sqlite *SSqliteBackend) CommitTableChangeSQL(ts sqlchemy.ITableSpec, chang
/* IGNORE DROP STATEMENT */
for _, col := range changes.RemoveColumns {
sql := fmt.Sprintf("DROP COLUMN `%s`", col.Name())
log.Infof("ALTER TABLE %s %s;", ts.Name(), sql)
log.Debugf("skip ALTER TABLE %s %s;", ts.Name(), sql)
// alters = append(alters, sql)
// ignore drop statement
// if the column is auto_increment integer column,

View File

@@ -51,6 +51,10 @@ func (c *sMockColumn) IsNumeric() bool {
return false
}
func (c *sMockColumn) IsText() bool {
return c.sqlType == "string"
}
func (c *sMockColumn) IsZero(val interface{}) bool {
return reflect.ValueOf(val).IsZero()
}

View File

@@ -202,6 +202,7 @@ func (ts *STableSpec) Sync() error {
sqls := ts.SyncSQL()
if sqls != nil {
for _, sql := range sqls {
log.Infof(sql)
_, err := ts.Database().Exec(sql)
if err != nil {
log.Errorf("exec sql error %s: %s", sql, err)

View File

@@ -128,6 +128,13 @@ func (us *SUpdateSession) saveUpdateSql(dt interface{}) (*sUpdateSQLResult, erro
nf, _ := fields.GetInterface(k)
if c.IsPrimary() {
if !gotypes.IsNil(of) && !c.IsZero(of) {
if c.IsText() {
ov, _ := of.(string)
nv, _ := nf.(string)
if ov != nv && strings.EqualFold(ov, nv) {
setters[k] = SUpdateDiff{old: of, new: nf, col: c}
}
}
primaries[k] = c.ConvertFromValue(of)
} else if c.IsText() {
primaries[k] = ""