MCPcopy
hub / github.com/gourouting/singo / Database

Function Database

model/init.go:18–52  ·  view source on GitHub ↗

Database initializes the MySQL connection.

(connString string)

Source from the content-addressed store, hash-verified

16
17// Database initializes the MySQL connection.
18func Database(connString string) {
19 // Initialize the GORM logger configuration.
20 newLogger := logger.New(
21 log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
22 logger.Config{
23 SlowThreshold: time.Second, // Slow SQL threshold
24 LogLevel: logger.Info, // Log level. Adjust as needed.
25 IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
26 Colorful: false, // Disable color
27 },
28 )
29
30 db, err := gorm.Open(mysql.Open(connString), &gorm.Config{
31 Logger: newLogger,
32 })
33 // Error
34 if connString == "" || err != nil {
35 util.Log().Error("mysql lost: %v", err)
36 panic(err)
37 }
38 sqlDB, err := db.DB()
39 if err != nil {
40 util.Log().Error("mysql lost: %v", err)
41 panic(err)
42 }
43
44 // Set the connection pool.
45 // Idle connections.
46 sqlDB.SetMaxIdleConns(10)
47 // Open connections.
48 sqlDB.SetMaxOpenConns(20)
49 DB = db
50
51 migration()
52}

Callers 2

InitFunction · 0.92
confInitFunction · 0.92

Calls 3

LogFunction · 0.92
migrationFunction · 0.85
ErrorMethod · 0.80

Tested by

no test coverage detected