Skip to content

Commit

Permalink
feat: support postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
qloog committed Dec 15, 2023
1 parent 8e06c0c commit 7a9cdad
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
4 changes: 2 additions & 2 deletions config/local/database.yaml
Original file line number Diff line number Diff line change
@@ -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日志
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func main() {
// -------------- init resource -------------
logger.Init()
// init db
// model.Init()
model.Init()
// init redis
// nolint: errcheck
// redis.Init()
Expand Down
37 changes: 20 additions & 17 deletions pkg/storage/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -42,28 +39,34 @@ 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)
// 用于设置闲置的连接数.设置闲置的连接数则当开启的一个连接使用完成后可以放在池里等候下一次使用。
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
Expand Down

0 comments on commit 7a9cdad

Please sign in to comment.