2023-09-17 03:52:37 -04:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2023-09-17 04:14:31 -04:00
|
|
|
"github.com/glebarez/sqlite"
|
2023-09-17 03:52:37 -04:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"sub2clash/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
var DB *gorm.DB
|
|
|
|
|
|
|
|
func ConnectDB() error {
|
2023-09-17 04:14:31 -04:00
|
|
|
// 用上面的数据库连接初始化 gorm
|
2023-09-17 03:52:37 -04:00
|
|
|
db, err := gorm.Open(sqlite.Open("sub2clash.db"), &gorm.Config{})
|
2023-09-17 04:14:31 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-09-17 03:52:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
DB = db
|
|
|
|
err = db.AutoMigrate(&model.ShortLink{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|