33 lines
915 B
Go
33 lines
915 B
Go
package database
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
|
|
"oci-portal/internal/model"
|
|
)
|
|
|
|
// Open 打开 SQLite 数据库并自动迁移全部模型。
|
|
func Open(path string) (*gorm.DB, error) {
|
|
db, err := gorm.Open(sqlite.Open(path), &gorm.Config{
|
|
Logger: logger.Default.LogMode(logger.Warn),
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("open sqlite %s: %w", path, err)
|
|
}
|
|
if err := db.AutoMigrate(
|
|
&model.User{}, &model.UserIdentity{}, &model.OciConfig{}, &model.Task{}, &model.TaskLog{},
|
|
&model.CheckSnapshot{}, &model.CostSnapshot{},
|
|
&model.RegionCache{}, &model.CompartmentCache{}, &model.Setting{},
|
|
&model.SystemLog{}, &model.LogEvent{}, &model.Proxy{},
|
|
&model.AiKey{}, &model.AiChannel{}, &model.AiModelCache{}, &model.AiCallLog{},
|
|
&model.AiContentLog{},
|
|
); err != nil {
|
|
return nil, fmt.Errorf("auto migrate: %w", err)
|
|
}
|
|
return db, nil
|
|
}
|