diff --git a/go.mod b/go.mod index c75a127726..c256b2db8f 100644 --- a/go.mod +++ b/go.mod @@ -157,6 +157,6 @@ require ( 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-20211129153017-8c872e6092b2 + yunion.io/x/sqlchemy v0.0.0-20211130070008-dcf6791bb0e7 yunion.io/x/structarg v0.0.0-20200720093445-9f850fa222ce ) diff --git a/go.sum b/go.sum index 24a594dcfc..4911a9260b 100644 --- a/go.sum +++ b/go.sum @@ -955,7 +955,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-20211129153017-8c872e6092b2 h1:Tb/vUuYlfGryDfoR5fAZE2SekSucxltqydKbT5+izHc= -yunion.io/x/sqlchemy v0.0.0-20211129153017-8c872e6092b2/go.mod h1:FTdwPdGhMgh4E+UFXc9klI1Ok34fMuybTT+jLhOaIjI= +yunion.io/x/sqlchemy v0.0.0-20211130070008-dcf6791bb0e7 h1:su9cE26uGZYRtIYRThbx7CXVX5fnyyM+FAsKHhZVS6Y= +yunion.io/x/sqlchemy v0.0.0-20211130070008-dcf6791bb0e7/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= diff --git a/vendor/modules.txt b/vendor/modules.txt index 16a64c789d..a6785d5b54 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1223,7 +1223,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-20211129153017-8c872e6092b2 +# yunion.io/x/sqlchemy v0.0.0-20211130070008-dcf6791bb0e7 yunion.io/x/sqlchemy yunion.io/x/sqlchemy/backends yunion.io/x/sqlchemy/backends/clickhouse diff --git a/vendor/yunion.io/x/sqlchemy/backends/mysql/sync.go b/vendor/yunion.io/x/sqlchemy/backends/mysql/sync.go index 5c2d684f0e..bf2d50c8a6 100644 --- a/vendor/yunion.io/x/sqlchemy/backends/mysql/sync.go +++ b/vendor/yunion.io/x/sqlchemy/backends/mysql/sync.go @@ -25,7 +25,7 @@ import ( func (mysql *SMySQLBackend) CommitTableChangeSQL(ts sqlchemy.ITableSpec, changes sqlchemy.STableChanges) []string { ret := make([]string, 0) - for _, idx := range changes.RemvoeIndexes { + for _, idx := range changes.RemoveIndexes { sql := fmt.Sprintf("DROP INDEX `%s` ON `%s`", idx.Name(), ts.Name()) ret = append(ret, sql) log.Infof("%s;", sql) diff --git a/vendor/yunion.io/x/sqlchemy/backends/sqlite/sync.go b/vendor/yunion.io/x/sqlchemy/backends/sqlite/sync.go index 989ae1b16f..b7f3cc0904 100644 --- a/vendor/yunion.io/x/sqlchemy/backends/sqlite/sync.go +++ b/vendor/yunion.io/x/sqlchemy/backends/sqlite/sync.go @@ -25,8 +25,8 @@ import ( func (sqlite *SSqliteBackend) CommitTableChangeSQL(ts sqlchemy.ITableSpec, changes sqlchemy.STableChanges) []string { ret := make([]string, 0) - for _, idx := range changes.RemvoeIndexes { - sql := fmt.Sprintf("DROP INDEX IF EXISTS `%s`.`%s`", idx.Name(), ts.Name()) + for _, idx := range changes.RemoveIndexes { + sql := fmt.Sprintf("DROP INDEX IF EXISTS `%s`.`%s`", ts.Name(), idx.Name()) ret = append(ret, sql) log.Infof("%s;", sql) } @@ -102,9 +102,9 @@ func (sqlite *SSqliteBackend) CommitTableChangeSQL(ts sqlchemy.ITableSpec, chang sql := fmt.Sprintf("INSERT INTO `%s` SELECT %s FROM `%s`", newTableName, strings.Join(colNames, ", "), ts.Name()) ret = append(ret, sql) // change name - sql = fmt.Sprintf("ALERT TABLE `%s` RENAME TO `%s`", ts.Name(), oldTableName) + sql = fmt.Sprintf("ALTER TABLE `%s` RENAME TO `%s`", ts.Name(), oldTableName) ret = append(ret, sql) - sql = fmt.Sprintf("ALERT TABLE `%s` RENAME TO `%s`", newTableName, ts.Name()) + sql = fmt.Sprintf("ALTER TABLE `%s` RENAME TO `%s`", newTableName, ts.Name()) ret = append(ret, sql) } diff --git a/vendor/yunion.io/x/sqlchemy/reflect.go b/vendor/yunion.io/x/sqlchemy/reflect.go index 368ec478d8..53d60f4cd6 100644 --- a/vendor/yunion.io/x/sqlchemy/reflect.go +++ b/vendor/yunion.io/x/sqlchemy/reflect.go @@ -98,10 +98,10 @@ func setValueBySQLString(value reflect.Value, val string) error { } switch value.Type() { case tristate.TriStateType: - if val == "0" { - value.Set(tristate.TriStateFalseValue) - } else if val == "1" { + if val == "1" { value.Set(tristate.TriStateTrueValue) + } else if val == "0" { + value.Set(tristate.TriStateFalseValue) } else { value.Set(tristate.TriStateNoneValue) } @@ -149,12 +149,46 @@ func setValueBySQLString(value reflect.Value, val string) error { value.SetString(val) return nil case reflect.Slice: - elemValue := reflect.New(value.Type().Elem()).Elem() - err := setValueBySQLString(elemValue, val) + jsonV, err := jsonutils.ParseString(val) if err != nil { - return errors.Wrap(err, "reflect.Slice") + return errors.Wrapf(err, "jsonutils.ParseString %s", val) + } + jsonA, err := jsonV.GetArray() + if err != nil { + return errors.Wrap(err, "jsonV.GetArray") + } + for i := range jsonA { + elemValue := reflect.New(value.Type().Elem()).Elem() + jsonStr, _ := jsonA[i].GetString() + err := setValueBySQLString(elemValue, jsonStr) + if err != nil { + return errors.Wrapf(err, "TestSetValueBySQLString %s", jsonA[i].String()) + } + value.Set(reflect.Append(value, elemValue)) + } + return nil + case reflect.Map: + jsonV, err := jsonutils.ParseString(val) + if err != nil { + return errors.Wrapf(err, "jsonutils.ParseString %s", val) + } + jsonM, err := jsonV.GetMap() + if err != nil { + return errors.Wrapf(err, "jsonV.GetMap") + } + for k, jsonV := range jsonM { + elemValue := reflect.New(value.Type().Elem()).Elem() + jsonStr, _ := jsonV.GetString() + err := setValueBySQLString(elemValue, jsonStr) + if err != nil { + return errors.Wrapf(err, "TestSetValueBySQLString %s", jsonV.String()) + } + if value.IsNil() { + mapValue := reflect.MakeMap(value.Type()) + value.Set(mapValue) + } + value.SetMapIndex(reflect.ValueOf(k), elemValue) } - value.Set(reflect.Append(value, elemValue)) return nil default: if valueType := value.Type(); valueType.Implements(gotypes.ISerializableType) { @@ -170,7 +204,18 @@ func setValueBySQLString(value reflect.Value, val string) error { } return setValueBySQLString(value.Elem(), val) } else { - return errors.Wrapf(ErrNotSupported, "not supported type: %s", valueType) + jsonV, err := jsonutils.ParseString(val) + if err != nil { + return errors.Wrapf(err, "%s not a json string: %s", val, err) + } + newVal := reflect.New(value.Type()) + err = jsonV.Unmarshal(newVal.Interface()) + if err != nil { + return errors.Wrap(err, "Unmarshal fail") + } + value.Set(reflect.Indirect(newVal)) + return nil + // return errors.Wrapf(ErrNotSupported, "not supported type: %s", valueType) } } } diff --git a/vendor/yunion.io/x/sqlchemy/sync.go b/vendor/yunion.io/x/sqlchemy/sync.go index 0f7be87e2c..35760e46c2 100644 --- a/vendor/yunion.io/x/sqlchemy/sync.go +++ b/vendor/yunion.io/x/sqlchemy/sync.go @@ -146,7 +146,7 @@ func (ts *STableSpec) Exists() bool { type STableChanges struct { // indexes - RemvoeIndexes []STableIndex + RemoveIndexes []STableIndex AddIndexes []STableIndex // Columns @@ -185,7 +185,7 @@ func (ts *STableSpec) SyncSQL() []string { remove, update, add := DiffCols(ts.name, cols, ts.Columns()) return ts.Database().backend.CommitTableChangeSQL(ts, STableChanges{ - RemvoeIndexes: removeIndexes, + RemoveIndexes: removeIndexes, AddIndexes: addIndexes, RemoveColumns: remove, UpdatedColumns: update,