diff --git a/config/local/database.yaml b/config/local/database.yaml index cc230800f..50be4d5d2 100644 --- a/config/local/database.yaml +++ b/config/local/database.yaml @@ -1,6 +1,6 @@ -Driver: mysql # 驱动名称,目前支持 mysql,postgres,默认: mysql +Driver: mysql # 驱动名称,目前支持: mysql,postgres,默认: mysql Name: eagle # 数据库名称 -Addr: localhost:3306 # 如果是 docker,可以替换为 对应的服务名称,eg: db:3306 +Addr: localhost:3306 # 如果是 docker,可以替换为 对应的服务名称,eg: db:3306, pg:5432 UserName: root Password: 123456 ShowLog: true # 是否打印所有SQL日志 diff --git a/go.mod b/go.mod index 91079c35e..a50742fed 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/go-playground/locales v0.14.1 github.com/go-playground/universal-translator v0.18.1 github.com/go-playground/validator/v10 v10.11.2 - github.com/go-sql-driver/mysql v1.5.0 + github.com/go-sql-driver/mysql v1.7.0 github.com/golang-jwt/jwt/v4 v4.4.2 github.com/golang/protobuf v1.5.2 github.com/golang/snappy v0.0.3 @@ -70,7 +70,7 @@ require ( golang.org/x/sync v0.1.0 google.golang.org/grpc v1.43.0 google.golang.org/protobuf v1.28.1 - gorm.io/driver/mysql v1.0.4 + gorm.io/driver/mysql v1.5.2 gorm.io/driver/postgres v1.5.4 gorm.io/gorm v1.25.5 ) diff --git a/go.sum b/go.sum index 668d8b56a..83bda9f48 100644 --- a/go.sum +++ b/go.sum @@ -247,6 +247,8 @@ github.com/go-redis/redis/v8 v8.11.2 h1:WqlSpAwz8mxDSMCvbyz1Mkiqe0LE5OY4j3lgkvu1 github.com/go-redis/redis/v8 v8.11.2/go.mod h1:DLomh7y2e3ggQXQLd1YgmvIfecPJoFl7WU5SOQ/r06M= github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0= @@ -1210,12 +1212,15 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/mysql v1.0.4 h1:TATTzt+kR+IV0+h3iUB3dHUe8omCvQ0rOkmfCsUBohk= gorm.io/driver/mysql v1.0.4/go.mod h1:MEgp8tk2n60cSBCq5iTcPDw3ns8Gs+zOva9EUhkknTs= +gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs= +gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8= gorm.io/driver/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo= gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0= gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM= gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw= gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw= gorm.io/gorm v1.20.12/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw= +gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls= gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/main.go b/main.go index 644551176..5882a3ac8 100644 --- a/main.go +++ b/main.go @@ -70,7 +70,7 @@ func main() { // -------------- init resource ------------- logger.Init() // init db - // model.Init() + model.Init() // init redis // nolint: errcheck // redis.Init() diff --git a/pkg/storage/orm/orm.go b/pkg/storage/orm/orm.go index 7fdf7e414..5ab72bcf3 100644 --- a/pkg/storage/orm/orm.go +++ b/pkg/storage/orm/orm.go @@ -5,14 +5,11 @@ import ( "fmt" "log" "os" - "strings" "time" - "gorm.io/driver/postgres" - - "gorm.io/driver/mysql" - otelgorm "github.com/1024casts/gorm-opentelemetry" + "gorm.io/driver/mysql" + "gorm.io/driver/postgres" // GORM MySQL "gorm.io/gorm" @@ -42,11 +39,27 @@ type Config struct { // NewMySQL connect to database and create a db instance func NewMySQL(c *Config) (db *gorm.DB) { - // create database conn - sqlDB, err := sql.Open(strings.ToLower(c.Driver), getDSN(c)) + var ( + err error + sqlDB *sql.DB + ) + dsn := getDSN(c) + switch c.Driver { + case DriverMySQL: + db, err = gorm.Open(mysql.Open(dsn), gormConfig(c)) + case DriverPostgres: + db, err = gorm.Open(postgres.Open(dsn), gormConfig(c)) + default: + db, err = gorm.Open(mysql.Open(dsn), gormConfig(c)) + } if err != nil { log.Panicf("open mysql failed. driver: %s, database name: %s, err: %+v", c.Driver, c.Name, err) } + + sqlDB, err = db.DB() + if err != nil { + log.Panicf("database connection failed. database name: %s, err: %+v", c.Name, err) + } // set for db connection // 用于设置最大打开的连接数,默认值为0表示不限制.设置最大的连接数,可以避免并发太高导致连接mysql出现too many connections的错误。 sqlDB.SetMaxOpenConns(c.MaxOpenConn) @@ -54,16 +67,6 @@ func NewMySQL(c *Config) (db *gorm.DB) { sqlDB.SetMaxIdleConns(c.MaxIdleConn) sqlDB.SetConnMaxLifetime(c.ConnMaxLifeTime) - switch c.Driver { - case DriverMySQL: - db, err = gorm.Open(mysql.New(mysql.Config{Conn: sqlDB}), gormConfig(c)) - case DriverPostgres: - db, err = gorm.Open(postgres.New(postgres.Config{Conn: sqlDB}), gormConfig(c)) - } - - if err != nil { - log.Panicf("database connection failed. database name: %s, err: %+v", c.Name, err) - } db.Set("gorm:table_options", "CHARSET=utf8mb4") // Initialize otel plugin with options