// Package mysql is a library wrapped on top of gorm.io/gorm, with added features such as link tracing, paging queries, etc. package mysql import ( "database/sql" "fmt" "log" "os" "github.com/uptrace/opentelemetry-go-extra/otelgorm" mysqlDriver "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" "gorm.io/gorm/schema" ) // Init mysql func Init(dns string, opts ...Option) (*gorm.DB, error) { o := defaultOptions() o.apply(opts...) sqlDB, err := sql.Open("mysql", dns) if err != nil { return nil, err } sqlDB.SetMaxIdleConns(o.maxIdleConns) // set the maximum number of connections in the idle connection pool sqlDB.SetMaxOpenConns(o.maxOpenConns) // set the maximum number of open database connections sqlDB.SetConnMaxLifetime(o.connMaxLifetime) // set the maximum time a connection can be reused db, err := gorm.Open(mysqlDriver.New(mysqlDriver.Config{Conn: sqlDB}), gormConfig(o)) if err != nil { return nil, fmt.Errorf("gorm.Open error, err: %v", err) } db.Set("gorm:table_options", "CHARSET=utf8mb4") // automatic appending of table suffixes when creating tables if o.enableTrace { err = db.Use(otelgorm.NewPlugin()) if err != nil { return nil, fmt.Errorf("using gorm opentelemetry, err: %v", err) } } return db, nil } // gorm setting func gormConfig(o *options) *gorm.Config { config := &gorm.Config{ // disable foreign key constraints, not recommended for production environments DisableForeignKeyConstraintWhenMigrating: o.disableForeignKey, // removing the plural of an epithet NamingStrategy: schema.NamingStrategy{SingularTable: true}, } // print all SQL if o.isLog { config.Logger = logger.Default.LogMode(logger.Info) } else { config.Logger = logger.Default.LogMode(logger.Silent) } // print only slow queries if o.slowThreshold > 0 { config.Logger = logger.New( log.New(os.Stdout, "\r\n", log.LstdFlags), // use the standard output asWriter logger.Config{ SlowThreshold: o.slowThreshold, Colorful: true, LogLevel: logger.Warn, // set the logging level, only above the specified level will output the slow query log }, ) } return config }