diff --git a/go.mod b/go.mod index 4d088623..39231f91 100644 --- a/go.mod +++ b/go.mod @@ -30,8 +30,8 @@ require ( golang.org/x/text v0.6.0 gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gorm.io/datatypes v1.1.0 - gorm.io/driver/mysql v1.4.5 - gorm.io/driver/postgres v1.4.6 + gorm.io/driver/mysql v1.4.6 + gorm.io/driver/postgres v1.4.7 gorm.io/gen v0.3.21 gorm.io/gorm v1.24.5 xorm.io/xorm v1.3.2 diff --git a/go.sum b/go.sum index 91a0f3fd..92093bd2 100644 --- a/go.sum +++ b/go.sum @@ -737,10 +737,10 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/datatypes v1.1.0 h1:EVp1Z28N4ACpYFK1nHboEIJGIFfjY7vLeieDk8jSHJA= gorm.io/datatypes v1.1.0/go.mod h1:SH2K9R+2RMjuX1CkCONrPwoe9JzVv2hkQvEu4bXGojE= gorm.io/driver/mysql v1.4.3/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c= -gorm.io/driver/mysql v1.4.5 h1:u1lytId4+o9dDaNcPCFzNv7h6wvmc92UjNk3z8enSBU= -gorm.io/driver/mysql v1.4.5/go.mod h1:SxzItlnT1cb6e1e4ZRpgJN2VYtcqJgqnHxWr4wsP8oc= -gorm.io/driver/postgres v1.4.6 h1:1FPESNXqIKG5JmraaH2bfCVlMQ7paLoCreFxDtqzwdc= -gorm.io/driver/postgres v1.4.6/go.mod h1:UJChCNLFKeBqQRE+HrkFUbKbq9idPXmTOk2u4Wok8S4= +gorm.io/driver/mysql v1.4.6 h1:5zS3vIKcyb46byXZNcYxaT9EWNIhXzu0gPuvvVrwZ8s= +gorm.io/driver/mysql v1.4.6/go.mod h1:SxzItlnT1cb6e1e4ZRpgJN2VYtcqJgqnHxWr4wsP8oc= +gorm.io/driver/postgres v1.4.7 h1:J06jXZCNq7Pdf7LIPn8tZn9LsWjd81BRSKveKNr0ZfA= +gorm.io/driver/postgres v1.4.7/go.mod h1:UJChCNLFKeBqQRE+HrkFUbKbq9idPXmTOk2u4Wok8S4= gorm.io/driver/sqlite v1.4.2/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI= gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU= gorm.io/driver/sqlserver v1.4.1 h1:t4r4r6Jam5E6ejqP7N82qAJIJAht27EGT41HyPfXRw0= diff --git a/library.go b/library.go index 1bfb1aac..4701a59e 100644 --- a/library.go +++ b/library.go @@ -1,5 +1,5 @@ package go_library func Version() string { - return "1.0.80" + return "1.0.81" } diff --git a/utils/gojobs/jobs_gorm_model/task.go b/utils/gojobs/jobs_gorm_model/task.go index 04815cee..4a5f39c7 100644 --- a/utils/gojobs/jobs_gorm_model/task.go +++ b/utils/gojobs/jobs_gorm_model/task.go @@ -10,7 +10,6 @@ type Task struct { Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号 Status string `gorm:"index;comment:状态码" json:"status"` // 状态码 Params string `gorm:"comment:参数" json:"params"` // 参数 - ParamsType string `gorm:"comment:参数类型" json:"params_type"` // 参数类型 StatusDesc string `gorm:"comment:状态描述" json:"status_desc"` // 状态描述 Frequency int64 `gorm:"index;comment:频率(秒单位)" json:"frequency"` // 频率(秒单位) Number int64 `gorm:"comment:当前次数" json:"number"` // 当前次数 diff --git a/utils/gorequest/config.go b/utils/gorequest/config.go new file mode 100644 index 00000000..64ea772e --- /dev/null +++ b/utils/gorequest/config.go @@ -0,0 +1,40 @@ +package gorequest + +import ( + "github.com/dtapps/go-library" + "github.com/shirou/gopsutil/host" + "log" + "runtime" +) + +type systemResult struct { + SystemOs string // 系统类型 + SystemKernel string // 系统内核 +} + +// 获取系统信息 +func getSystem() (result systemResult) { + + hInfo, err := host.Info() + if err != nil { + log.Printf("getSystem.host.Info:%s\n", err) + } + + result.SystemOs = hInfo.OS + result.SystemKernel = hInfo.KernelArch + + return result +} + +// 设置配置信息 +func (app *App) setConfig() { + + info := getSystem() + + app.config.systemOs = info.SystemOs + app.config.systemKernel = info.SystemKernel + + app.config.sdkVersion = go_library.Version() + app.config.goVersion = runtime.Version() + +} diff --git a/utils/gorequest/http.go b/utils/gorequest/http.go index 56c145ce..99f7e40e 100644 --- a/utils/gorequest/http.go +++ b/utils/gorequest/http.go @@ -7,7 +7,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/dtapps/go-library" "github.com/dtapps/go-library/utils/gostring" "github.com/dtapps/go-library/utils/gotime" "github.com/dtapps/go-library/utils/gotrace_id" @@ -16,7 +15,6 @@ import ( "log" "net/http" "net/url" - "runtime" "strings" "time" ) @@ -50,14 +48,22 @@ type App struct { debug bool // 是否开启调试模式 p12Cert *tls.Certificate // p12证书内容 afferentSdkUserVersion string // 传入SDk版本 + config struct { + systemOs string // 系统类型 + systemKernel string // 系统内核 + goVersion string // go版本 + sdkVersion string // sdk版本 + } } // NewHttp 实例化 func NewHttp() *App { - return &App{ + app := &App{ httpHeader: NewHeaders(), httpParams: NewParams(), } + app.setConfig() + return app } // SetDebug 设置调试模式 @@ -204,9 +210,9 @@ func request(app *App, ctx context.Context) (httpResponse Response, err error) { // SDK版本 if app.afferentSdkUserVersion == "" { - httpResponse.RequestHeader.Set("Sdk-User-Agent", fmt.Sprintf(userAgentFormat, runtime.GOOS, runtime.GOARCH, runtime.Version(), go_library.Version())) + httpResponse.RequestHeader.Set("Sdk-User-Agent", fmt.Sprintf(userAgentFormat, app.config.systemOs, app.config.systemKernel, app.config.goVersion, app.config.sdkVersion)) } else { - httpResponse.RequestHeader.Set("Sdk-User-Agent", fmt.Sprintf(userAgentFormat, runtime.GOOS, runtime.GOARCH, runtime.Version(), go_library.Version())+"/"+app.afferentSdkUserVersion) + httpResponse.RequestHeader.Set("Sdk-User-Agent", fmt.Sprintf(userAgentFormat, app.config.systemOs, app.config.systemKernel, app.config.goVersion, app.config.sdkVersion)+"/"+app.afferentSdkUserVersion) } // 请求类型 diff --git a/vendor/gorm.io/driver/mysql/migrator.go b/vendor/gorm.io/driver/mysql/migrator.go index a581720e..f9967a42 100644 --- a/vendor/gorm.io/driver/mysql/migrator.go +++ b/vendor/gorm.io/driver/mysql/migrator.go @@ -3,6 +3,7 @@ package mysql import ( "database/sql" "fmt" + "strconv" "strings" "gorm.io/gorm" @@ -49,15 +50,50 @@ func (m Migrator) FullDataTypeOf(field *schema.Field) clause.Expr { func (m Migrator) AlterColumn(value interface{}, field string) error { return m.RunWithValue(value, func(stmt *gorm.Statement) error { if field := stmt.Schema.LookUpField(field); field != nil { + fullDataType := m.FullDataTypeOf(field) + if m.Dialector.DontSupportRenameColumnUnique { + fullDataType.SQL = strings.Replace(fullDataType.SQL, " UNIQUE ", " ", 1) + } + return m.DB.Exec( "ALTER TABLE ? MODIFY COLUMN ? ?", - clause.Table{Name: stmt.Table}, clause.Column{Name: field.DBName}, m.FullDataTypeOf(field), + clause.Table{Name: stmt.Table}, clause.Column{Name: field.DBName}, fullDataType, ).Error } return fmt.Errorf("failed to look up field with name: %s", field) }) } +func (m Migrator) TiDBVersion() (isTiDB bool, major, minor, patch int, err error) { + // TiDB version string looks like: + // "5.7.25-TiDB-v6.5.0" or "5.7.25-TiDB-v6.4.0-serverless" + tidbVersionArray := strings.Split(m.Dialector.ServerVersion, "-") + if len(tidbVersionArray) < 3 || tidbVersionArray[1] != "TiDB" { + // It isn't TiDB + return + } + + rawVersion := strings.TrimPrefix(tidbVersionArray[2], "v") + realVersionArray := strings.Split(rawVersion, ".") + if major, err = strconv.Atoi(realVersionArray[0]); err != nil { + err = fmt.Errorf("failed to parse the version of TiDB, the major version is: %s", realVersionArray[0]) + return + } + + if minor, err = strconv.Atoi(realVersionArray[1]); err != nil { + err = fmt.Errorf("failed to parse the version of TiDB, the minor version is: %s", realVersionArray[0]) + return + } + + if patch, err = strconv.Atoi(realVersionArray[2]); err != nil { + err = fmt.Errorf("failed to parse the version of TiDB, the patch version is: %s", realVersionArray[0]) + return + } + + isTiDB = true + return +} + func (m Migrator) RenameColumn(value interface{}, oldName, newName string) error { return m.RunWithValue(value, func(stmt *gorm.Statement) error { if !m.Dialector.DontSupportRenameColumn { @@ -173,11 +209,11 @@ func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) { } rawColumnTypes, err := rows.ColumnTypes() - + if err != nil { return err } - + if err := rows.Close(); err != nil { return err } diff --git a/vendor/gorm.io/driver/mysql/mysql.go b/vendor/gorm.io/driver/mysql/mysql.go index 8617eb67..205b4829 100644 --- a/vendor/gorm.io/driver/mysql/mysql.go +++ b/vendor/gorm.io/driver/mysql/mysql.go @@ -21,6 +21,10 @@ import ( "gorm.io/gorm/utils" ) +const ( + AutoRandomTag = "auto_random()" // Treated as an auto_random field for tidb +) + type Config struct { DriverName string ServerVersion string @@ -36,6 +40,7 @@ type Config struct { DontSupportRenameColumn bool DontSupportForShareClause bool DontSupportNullAsDefaultValue bool + DontSupportRenameColumnUnique bool } type Dialector struct { @@ -134,6 +139,10 @@ func (dialector Dialector) Initialize(db *gorm.DB) (err error) { dialector.Config.DontSupportRenameColumn = true dialector.Config.DontSupportForShareClause = true } + + if strings.Contains(dialector.ServerVersion, "TiDB") { + dialector.Config.DontSupportRenameColumnUnique = true + } } // register callbacks @@ -417,7 +426,36 @@ func (dialector Dialector) getSchemaBytesType(field *schema.Field) string { return "longblob" } +// autoRandomType +// field.DataType MUST be `schema.Int` or `schema.Uint` +// Judgement logic: +// 1. Is PrimaryKey; +// 2. Has default value; +// 3. Default value is "auto_random()"; +// 4. IGNORE the field.Size, it MUST be bigint; +// 5. CLEAR the default tag, and return true; +// 6. Otherwise, return false. +func autoRandomType(field *schema.Field) (bool, string) { + if field.PrimaryKey && field.HasDefaultValue && + strings.ToLower(strings.TrimSpace(field.DefaultValue)) == AutoRandomTag { + field.DefaultValue = "" + + sqlType := "bigint" + if field.DataType == schema.Uint { + sqlType += " unsigned" + } + sqlType += " auto_random" + return true, sqlType + } + + return false, "" +} + func (dialector Dialector) getSchemaIntAndUnitType(field *schema.Field) string { + if autoRandom, typeString := autoRandomType(field); autoRandom { + return typeString + } + constraint := func(sqlType string) string { if field.DataType == schema.Uint { sqlType += " unsigned" diff --git a/vendor/gorm.io/driver/postgres/migrator.go b/vendor/gorm.io/driver/postgres/migrator.go index e8f658fa..3dec4a7b 100644 --- a/vendor/gorm.io/driver/postgres/migrator.go +++ b/vendor/gorm.io/driver/postgres/migrator.go @@ -336,8 +336,11 @@ func (m Migrator) AlterColumn(value interface{}, field string) error { if uniq, _ := fieldColumnType.Unique(); !uniq && field.Unique { idxName := clause.Column{Name: m.DB.Config.NamingStrategy.IndexName(stmt.Table, field.DBName)} - if err := m.DB.Exec("ALTER TABLE ? ADD CONSTRAINT ? UNIQUE(?)", m.CurrentTable(stmt), idxName, clause.Column{Name: field.DBName}).Error; err != nil { - return err + // Not a unique constraint but a unique index + if !m.HasIndex(stmt.Table, idxName.Name) { + if err := m.DB.Exec("ALTER TABLE ? ADD CONSTRAINT ? UNIQUE(?)", m.CurrentTable(stmt), idxName, clause.Column{Name: field.DBName}).Error; err != nil { + return err + } } } diff --git a/vendor/modules.txt b/vendor/modules.txt index 1bcac365..9761f1d4 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -431,10 +431,10 @@ gopkg.in/yaml.v2 # gorm.io/datatypes v1.1.0 ## explicit; go 1.18 gorm.io/datatypes -# gorm.io/driver/mysql v1.4.5 +# gorm.io/driver/mysql v1.4.6 ## explicit; go 1.14 gorm.io/driver/mysql -# gorm.io/driver/postgres v1.4.6 +# gorm.io/driver/postgres v1.4.7 ## explicit; go 1.14 gorm.io/driver/postgres # gorm.io/gen v0.3.21