From 0fb387879eff1b914afcedf0c7c2c6fa04590718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Mon, 19 Sep 2022 13:51:25 +0800 Subject: [PATCH] - update config --- .gitignore | 1 + api.go | 28 +++++++++------------- api_cofing.go | 18 ++++++++++++++ api_gorm.go | 22 ++++++----------- const.go | 2 +- gin.go | 28 +++++++++------------- gin_cofing.go | 18 ++++++++++++++ gin_gorm.go | 23 +++++++----------- go.mod | 2 +- go.sum | 8 ++----- vendor/gorm.io/driver/postgres/migrator.go | 23 ++++++++++++++---- vendor/modules.txt | 2 +- 12 files changed, 97 insertions(+), 78 deletions(-) create mode 100644 api_cofing.go create mode 100644 gin_cofing.go diff --git a/.gitignore b/.gitignore index c1823b5..5c75b7d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ *.log gomod.sh *_test.go +/vendor/ diff --git a/api.go b/api.go index e50560f..01b6f57 100644 --- a/api.go +++ b/api.go @@ -6,8 +6,6 @@ import ( "go.dtapp.net/dorm" "go.dtapp.net/goip" "go.dtapp.net/gorequest" - "os" - "runtime" ) // ApiClientFun *ApiClient 驱动 @@ -25,14 +23,16 @@ type ApiClient struct { currentIp string // 当前ip gormConfig struct { tableName string // 表名 - insideIp string // 内网ip - hostname string // 主机名 - goVersion string // go版本 } config struct { - os string // 系统类型 - arch string // 系统架构 - jsonStatus bool // json状态 + systemHostName string // 主机名 + systemInsideIp string // 内网ip + systemOs string // 系统类型 + systemArch string // 系统架构 + goVersion string // go版本 + sdkVersion string // sdk版本 + systemOutsideIp string // 外网ip + jsonStatus bool // json状态 } } @@ -65,17 +65,12 @@ func NewApiClient(config *ApiClientConfig) (*ApiClient, error) { c.currentIp = config.CurrentIp } - c.config.os = runtime.GOOS - c.config.arch = runtime.GOARCH - gormClient, gormTableName := config.GormClientFun() if gormClient == nil || gormClient.Db == nil { return nil, errors.New("没有设置驱动") } - hostname, _ := os.Hostname() - if gormClient != nil || gormClient.Db != nil { c.gormClient = gormClient @@ -90,12 +85,11 @@ func NewApiClient(config *ApiClientConfig) (*ApiClient, error) { return nil, errors.New("创建表失败:" + err.Error()) } - c.gormConfig.hostname = hostname - c.gormConfig.insideIp = goip.GetInsideIp(ctx) - c.gormConfig.goVersion = runtime.Version() - } + // 配置信息 + c.setConfig(ctx) + return c, nil } diff --git a/api_cofing.go b/api_cofing.go new file mode 100644 index 0000000..8dc6c17 --- /dev/null +++ b/api_cofing.go @@ -0,0 +1,18 @@ +package golog + +import ( + "context" + "go.dtapp.net/goip" + "os" + "runtime" +) + +func (c *ApiClient) setConfig(ctx context.Context) { + c.config.sdkVersion = Version + c.config.systemOs = runtime.GOOS + c.config.systemArch = runtime.GOARCH + c.config.goVersion = runtime.Version() + c.config.systemInsideIp = goip.GetInsideIp(ctx) + hostname, _ := os.Hostname() + c.config.systemHostName = hostname +} diff --git a/api_gorm.go b/api_gorm.go index 084a7b9..0cc3fde 100644 --- a/api_gorm.go +++ b/api_gorm.go @@ -10,8 +10,6 @@ import ( "go.dtapp.net/gotrace_id" "go.dtapp.net/gourl" "gorm.io/datatypes" - "os" - "runtime" "unicode/utf8" ) @@ -62,11 +60,8 @@ func NewApiGormClient(config *ApiGormClientConfig) (*ApiClient, error) { return nil, errors.New("创建表失败:" + err.Error()) } - hostname, _ := os.Hostname() - - c.gormConfig.hostname = hostname - c.gormConfig.insideIp = goip.GetInsideIp(ctx) - c.gormConfig.goVersion = runtime.Version() + // 配置信息 + c.setConfig(ctx) return c, nil } @@ -94,16 +89,13 @@ func (c *ApiClient) gormRecord(ctx context.Context, data apiPostgresqlLogString) data.ResponseBody = "" } - data.SystemHostName = c.gormConfig.hostname - data.SystemInsideIp = c.gormConfig.insideIp - data.GoVersion = c.gormConfig.goVersion - + data.SystemHostName = c.config.systemHostName + data.SystemInsideIp = c.config.systemInsideIp + data.GoVersion = c.config.goVersion data.TraceId = gotrace_id.GetTraceIdContext(ctx) - data.RequestIp = c.currentIp - - data.SystemOs = c.config.os - data.SystemArch = c.config.arch + data.SystemOs = c.config.systemOs + data.SystemArch = c.config.systemArch if c.config.jsonStatus { err = c.gormClient.Db.Table(c.gormConfig.tableName).Create(&apiPostgresqlLogJson{ diff --git a/const.go b/const.go index d1f9d5f..04cb69b 100644 --- a/const.go +++ b/const.go @@ -1,5 +1,5 @@ package golog const ( - Version = "1.0.82" + Version = "1.0.83" ) diff --git a/gin.go b/gin.go index 390d77f..c55e50b 100644 --- a/gin.go +++ b/gin.go @@ -13,8 +13,6 @@ import ( "go.dtapp.net/gotrace_id" "io/ioutil" "net" - "os" - "runtime" ) // GinClientFun *GinClient 驱动 @@ -32,14 +30,16 @@ type GinClient struct { logDebug bool // 日志开关 gormConfig struct { tableName string // 表名 - insideIp string // 内网ip - hostname string // 主机名 - goVersion string // go版本 } config struct { - os string // 系统类型 - arch string // 系统架构 - jsonStatus bool // json状态 + systemHostName string // 主机名 + systemInsideIp string // 内网ip + systemOs string // 系统类型 + systemArch string // 系统架构 + goVersion string // go版本 + sdkVersion string // sdk版本 + systemOutsideIp string // 外网ip + jsonStatus bool // json状态 } } @@ -68,17 +68,12 @@ func NewGinClient(config *GinClientConfig) (*GinClient, error) { c.config.jsonStatus = config.JsonStatus - c.config.os = runtime.GOOS - c.config.arch = runtime.GOARCH - gormClient, gormTableName := config.GormClientFun() if gormClient == nil || gormClient.Db == nil { return nil, errors.New("没有设置驱动") } - hostname, _ := os.Hostname() - if gormClient != nil || gormClient.Db != nil { c.gormClient = gormClient @@ -95,12 +90,11 @@ func NewGinClient(config *GinClientConfig) (*GinClient, error) { return nil, errors.New("创建表失败:" + err.Error()) } - c.gormConfig.hostname = hostname - c.gormConfig.insideIp = goip.GetInsideIp(ctx) - c.gormConfig.goVersion = runtime.Version() - } + // 配置信息 + c.setConfig(ctx) + return c, nil } diff --git a/gin_cofing.go b/gin_cofing.go new file mode 100644 index 0000000..fd91196 --- /dev/null +++ b/gin_cofing.go @@ -0,0 +1,18 @@ +package golog + +import ( + "context" + "go.dtapp.net/goip" + "os" + "runtime" +) + +func (c *GinClient) setConfig(ctx context.Context) { + c.config.sdkVersion = Version + c.config.systemOs = runtime.GOOS + c.config.systemArch = runtime.GOARCH + c.config.goVersion = runtime.Version() + c.config.systemInsideIp = goip.GetInsideIp(ctx) + hostname, _ := os.Hostname() + c.config.systemHostName = hostname +} diff --git a/gin_gorm.go b/gin_gorm.go index e667ac0..514c1f9 100644 --- a/gin_gorm.go +++ b/gin_gorm.go @@ -15,8 +15,6 @@ import ( "gorm.io/datatypes" "io/ioutil" "net" - "os" - "runtime" "time" ) @@ -65,11 +63,8 @@ func NewGinGormClient(config *GinGormClientConfig) (*GinClient, error) { return nil, errors.New("创建表失败:" + err.Error()) } - hostname, _ := os.Hostname() - - c.gormConfig.hostname = hostname - c.gormConfig.insideIp = goip.GetInsideIp(ctx) - c.gormConfig.goVersion = runtime.Version() + // 配置信息 + c.setConfig(ctx) return c, nil } @@ -93,14 +88,12 @@ func (c *GinClient) gormAutoMigrate() (err error) { // gormRecord 记录日志 func (c *GinClient) gormRecord(data ginPostgresqlLogString) (err error) { - data.SystemHostName = c.gormConfig.hostname - data.SystemInsideIp = c.gormConfig.insideIp - data.GoVersion = c.gormConfig.goVersion - - data.SdkVersion = Version - - data.SystemOs = c.config.os - data.SystemArch = c.config.arch + data.SystemHostName = c.config.systemHostName + data.SystemInsideIp = c.config.systemInsideIp + data.GoVersion = c.config.goVersion + data.SdkVersion = c.config.sdkVersion + data.SystemOs = c.config.systemOs + data.SystemArch = c.config.systemArch if c.config.jsonStatus { err = c.gormClient.Db.Table(c.gormConfig.tableName).Create(&ginPostgresqlLogJson{ diff --git a/go.mod b/go.mod index de261ad..4f9e61c 100644 --- a/go.mod +++ b/go.mod @@ -86,7 +86,7 @@ require ( gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gorm.io/driver/mysql v1.3.6 // indirect - gorm.io/driver/postgres v1.3.9 // indirect + gorm.io/driver/postgres v1.3.10 // indirect gorm.io/gorm v1.23.9 // indirect mellium.im/sasl v0.3.0 // indirect modernc.org/libc v1.16.19 // indirect diff --git a/go.sum b/go.sum index f7a0182..f09abce 100644 --- a/go.sum +++ b/go.sum @@ -203,7 +203,6 @@ github.com/jackc/pgconn v1.8.1/go.mod h1:JV6m6b6jhjdmzchES0drzCcYcAHS1OPD5xu3OZ/ github.com/jackc/pgconn v1.9.0/go.mod h1:YctiPyvzfU11JFxoXokUOOKQXQmDMoJL9vJzHH8/2JY= github.com/jackc/pgconn v1.9.1-0.20210724152538-d89c8390a530/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI= github.com/jackc/pgconn v1.11.0/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI= -github.com/jackc/pgconn v1.12.1/go.mod h1:ZkhRC59Llhrq3oSfrikvwQ5NaxYExr6twkdkMLaKono= github.com/jackc/pgconn v1.13.0 h1:3L1XMNV2Zvca/8BYhzcRFS70Lr0WlDg16Di6SFGAbys= github.com/jackc/pgconn v1.13.0/go.mod h1:AnowpAqO4CMIIJNZl2VJp+KrkAZciAkhEl0W0JIobpI= github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= @@ -223,7 +222,6 @@ github.com/jackc/pgproto3/v2 v2.0.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwX github.com/jackc/pgproto3/v2 v2.0.6/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= github.com/jackc/pgproto3/v2 v2.1.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= github.com/jackc/pgproto3/v2 v2.2.0/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= -github.com/jackc/pgproto3/v2 v2.3.0/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= github.com/jackc/pgproto3/v2 v2.3.1 h1:nwj7qwf0S+Q7ISFfBndqeLwSwxs+4DPsbRFjECT1Y4Y= github.com/jackc/pgproto3/v2 v2.3.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= github.com/jackc/pgservicefile v0.0.0-20200307190119-3430c5407db8/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= @@ -239,7 +237,6 @@ github.com/jackc/pgtype v1.7.0/go.mod h1:ZnHF+rMePVqDKaOfJVI4Q8IVvAQMryDlDkZnKOI github.com/jackc/pgtype v1.8.0/go.mod h1:PqDKcEBtllAtk/2p6z6SHdXW5UB+MhE75tUol2OKexE= github.com/jackc/pgtype v1.8.1-0.20210724151600-32e20a603178/go.mod h1:C516IlIV9NKqfsMCXTdChteoXmwgUceqaLfjg2e3NlM= github.com/jackc/pgtype v1.10.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4= -github.com/jackc/pgtype v1.11.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4= github.com/jackc/pgtype v1.12.0 h1:Dlq8Qvcch7kiehm8wPGIW0W3KsCCHJnRacKW0UM8n5w= github.com/jackc/pgtype v1.12.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4= github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y= @@ -252,7 +249,6 @@ github.com/jackc/pgx/v4 v4.11.0/go.mod h1:i62xJgdrtVDsnL3U8ekyrQXEwGNTRoG7/8r+CI github.com/jackc/pgx/v4 v4.12.0/go.mod h1:fE547h6VulLPA3kySjfnSG/e2D861g/50JlVUa/ub60= github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs= github.com/jackc/pgx/v4 v4.15.0/go.mod h1:D/zyOyXiaM1TmVWnOM18p0xdDtdakRBa0RsVGI3U3bw= -github.com/jackc/pgx/v4 v4.16.1/go.mod h1:SIhx0D5hoADaiXZVyv+3gSm3LCIIINTVO0PficsvWGQ= github.com/jackc/pgx/v4 v4.17.2 h1:0Ut0rpeKwvIVbMQ1KbMBU4h6wxehBI535LK6Flheh8E= github.com/jackc/pgx/v4 v4.17.2/go.mod h1:lcxIZN44yMIrWI78a5CpucdD14hX0SBDbNRvjDBItsw= github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= @@ -751,8 +747,8 @@ gorm.io/driver/mysql v1.3.2/go.mod h1:ChK6AHbHgDCFZyJp0F+BmVGb06PSIoh9uVYKAlRbb2 gorm.io/driver/mysql v1.3.6 h1:BhX1Y/RyALb+T9bZ3t07wLnPZBukt+IRkMn8UZSNbGM= gorm.io/driver/mysql v1.3.6/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c= gorm.io/driver/postgres v1.3.4/go.mod h1:y0vEuInFKJtijuSGu9e5bs5hzzSzPK+LancpKpvbRBw= -gorm.io/driver/postgres v1.3.9 h1:lWGiVt5CijhQAg0PWB7Od1RNcBw/jS4d2cAScBcSDXg= -gorm.io/driver/postgres v1.3.9/go.mod h1:qw/FeqjxmYqW5dBcYNBsnhQULIApQdk7YuuDPktVi1U= +gorm.io/driver/postgres v1.3.10 h1:Fsd+pQpFMGlGxxVMUPJhNo8gG8B1lKtk8QQ4/VZZAJw= +gorm.io/driver/postgres v1.3.10/go.mod h1:whNfh5WhhHs96honoLjBAMwJGYEuA3m1hvgUbNXhPCw= gorm.io/driver/sqlite v1.3.1 h1:bwfE+zTEWklBYoEodIOIBwuWHpnx52Z9zJFW5F33WLk= gorm.io/driver/sqlite v1.3.1/go.mod h1:wJx0hJspfycZ6myN38x1O/AqLtNS6c5o9TndewFbELg= gorm.io/driver/sqlserver v1.3.1 h1:F5t6ScMzOgy1zukRTIZgLZwKahgt3q1woAILVolKpOI= diff --git a/vendor/gorm.io/driver/postgres/migrator.go b/vendor/gorm.io/driver/postgres/migrator.go index 5eb0acc..9877d7c 100644 --- a/vendor/gorm.io/driver/postgres/migrator.go +++ b/vendor/gorm.io/driver/postgres/migrator.go @@ -436,14 +436,25 @@ func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType, // check primary, unique field { - columnTypeRows, err := m.DB.Raw("SELECT c.column_name, constraint_type FROM information_schema.table_constraints tc JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name) JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema AND tc.table_name = c.table_name AND ccu.column_name = c.column_name WHERE constraint_type IN ('PRIMARY KEY', 'UNIQUE') AND c.table_catalog = ? AND c.table_schema = ? AND c.table_name = ?", currentDatabase, currentSchema, table).Rows() + columnTypeRows, err := m.DB.Raw("SELECT constraint_name FROM information_schema.table_constraints tc JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name) JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema AND tc.table_name = c.table_name AND ccu.column_name = c.column_name WHERE constraint_type IN ('PRIMARY KEY', 'UNIQUE') AND c.table_catalog = ? AND c.table_schema = ? AND c.table_name = ? AND constraint_type = ?", currentDatabase, currentSchema, table, "UNIQUE").Rows() if err != nil { return err } + uniqueContraints := map[string]int{} + for columnTypeRows.Next() { + var constraintName string + columnTypeRows.Scan(&constraintName) + uniqueContraints[constraintName]++ + } + columnTypeRows.Close() + columnTypeRows, err = m.DB.Raw("SELECT c.column_name, constraint_name, constraint_type FROM information_schema.table_constraints tc JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name) JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema AND tc.table_name = c.table_name AND ccu.column_name = c.column_name WHERE constraint_type IN ('PRIMARY KEY', 'UNIQUE') AND c.table_catalog = ? AND c.table_schema = ? AND c.table_name = ?", currentDatabase, currentSchema, table).Rows() + if err != nil { + return err + } for columnTypeRows.Next() { - var name, columnType string - columnTypeRows.Scan(&name, &columnType) + var name, constraintName, columnType string + columnTypeRows.Scan(&name, &constraintName, &columnType) for _, c := range columnTypes { mc := c.(*migrator.ColumnType) if mc.NameValue.String == name { @@ -451,7 +462,9 @@ func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType, case "PRIMARY KEY": mc.PrimaryKeyValue = sql.NullBool{Bool: true, Valid: true} case "UNIQUE": - mc.UniqueValue = sql.NullBool{Bool: true, Valid: true} + if uniqueContraints[constraintName] == 1 { + mc.UniqueValue = sql.NullBool{Bool: true, Valid: true} + } } break } @@ -463,7 +476,7 @@ func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType, // check column type { dataTypeRows, err := m.DB.Raw(`SELECT a.attname as column_name, format_type(a.atttypid, a.atttypmod) AS data_type - FROM pg_attribute a JOIN pg_class b ON a.attrelid = b.relfilenode AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = ?) + FROM pg_attribute a JOIN pg_class b ON a.attrelid = b.oid AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = ?) WHERE a.attnum > 0 -- hide internal columns AND NOT a.attisdropped -- hide deleted columns AND b.relname = ?`, currentSchema, table).Rows() diff --git a/vendor/modules.txt b/vendor/modules.txt index f35cfc3..729e550 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -407,7 +407,7 @@ gorm.io/datatypes # gorm.io/driver/mysql v1.3.6 ## explicit; go 1.14 gorm.io/driver/mysql -# gorm.io/driver/postgres v1.3.9 +# gorm.io/driver/postgres v1.3.10 ## explicit; go 1.14 gorm.io/driver/postgres # gorm.io/gorm v1.23.9