mirror of
https://github.com/ZSCNetSupportDept/scheduler.git
synced 2025-10-28 20:45:05 +08:00
43 lines
690 B
Go
43 lines
690 B
Go
package db
|
|
|
|
import (
|
|
"fmt"
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
"os"
|
|
"zsxyww.com/scheduler/config"
|
|
"zsxyww.com/scheduler/model"
|
|
)
|
|
|
|
var err error
|
|
|
|
func Connect() {
|
|
switch config.DB.Type {
|
|
case "SQLite":
|
|
connectSQLite()
|
|
case "PostgreSQL":
|
|
connectPGSQL()
|
|
default:
|
|
panic("DBType error")
|
|
}
|
|
Main.AutoMigrate(&model.Member{}, &model.Tweak{})
|
|
}
|
|
|
|
func connectSQLite() {
|
|
Main, err = gorm.Open(sqlite.Open(config.DB.Path), &gorm.Config{})
|
|
if err != nil {
|
|
fmt.Printf("error in connecting to SQLite:")
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
|
|
}
|
|
}
|
|
|
|
func connectPGSQL() {
|
|
Main, err = gorm.Open(postgres.Open(config.DB.Path))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|