Last active
July 10, 2024 00:00
-
-
Save BK1031/efea0165f6489e4e5027ca06a9486e50 to your computer and use it in GitHub Desktop.
racecar_analytics db connection function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"gorm.io/driver/mysql" | |
"gorm.io/gorm" | |
) | |
var DB *gorm.DB | |
var DatabaseHost = "" | |
var DatabasePort = "" | |
var DatabaseUser = "" | |
var DatabasePassword = "" | |
var DatabaseName = "" | |
func ConnectDB() { | |
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=UTC", DatabaseUser, DatabasePassword, DatabaseHost, DatabasePort, DatabaseName) | |
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) | |
if err != nil { | |
log.Fatalf("Failed to connect to database: %v", err) | |
} | |
DB = db | |
log.Println("Connected to database") | |
db.AutoMigrate(ECU{}, Battery{}) | |
log.Println("AutoMigration completed") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment