12
This commit is contained in:
29
internal/initialize/db.go
Normal file
29
internal/initialize/db.go
Normal file
@ -0,0 +1,29 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"unR2/internal/config"
|
||||
"unR2/internal/ent"
|
||||
"unR2/internal/global"
|
||||
)
|
||||
|
||||
func InitDB() {
|
||||
mysql := config.Conf.MySQL
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=True",
|
||||
mysql.User, mysql.Password, mysql.Host, mysql.Port, mysql.Database,
|
||||
)
|
||||
|
||||
client, err := ent.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
log.Fatalf("❌ 数据库连接失败: %v", err)
|
||||
}
|
||||
|
||||
if err := client.Schema.Create(context.Background()); err != nil {
|
||||
log.Fatalf("❌ 建表失败: %v", err)
|
||||
}
|
||||
|
||||
global.SetDB(client)
|
||||
log.Println("✅ 数据库初始化完成")
|
||||
}
|
||||
Reference in New Issue
Block a user