databag/net/server/internal/store/alloc.go

56 lines
1.3 KiB
Go
Raw Normal View History

2022-01-12 07:04:27 +00:00
package store
import (
"fmt"
2022-01-12 07:04:27 +00:00
"gorm.io/gorm"
"gorm.io/gorm/logger"
2024-01-12 21:21:51 +00:00
"gorm.io/gorm/clause"
2024-01-30 20:56:13 +00:00
"gorm.io/driver/sqlite"
2022-01-12 07:04:27 +00:00
)
var DB *gorm.DB;
func SetPath(storePath string, transformPath string) {
db, err := gorm.Open(sqlite.Open(storePath + "/databag.db"), &gorm.Config{
2022-11-15 06:25:15 +00:00
Logger: logger.Default.LogMode(logger.Silent),
2022-01-12 07:04:27 +00:00
})
if err != nil {
fmt.Println(err);
2022-01-12 07:04:27 +00:00
panic("failed to connect database")
}
AutoMigrate(db)
2024-01-12 21:21:51 +00:00
// upsert asset path
2024-01-12 21:21:51 +00:00
err = db.Transaction(func(tx *gorm.DB) error {
if res := tx.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "config_id"}},
DoUpdates: clause.AssignmentColumns([]string{"str_value"}),
2024-01-15 18:10:28 +00:00
}).Create(&Config{ConfigID: "asset_path", StrValue: storePath}).Error; res != nil {
return res
}
return nil
})
if err != nil {
fmt.Println(err);
panic("failed to set database path")
}
// upsert script path
err = db.Transaction(func(tx *gorm.DB) error {
if res := tx.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "config_id"}},
DoUpdates: clause.AssignmentColumns([]string{"str_value"}),
}).Create(&Config{ConfigID: "script_path", StrValue: transformPath}).Error; res != nil {
2024-01-12 21:21:51 +00:00
return res
}
return nil
})
if err != nil {
fmt.Println(err);
panic("failed to set database path")
}
2022-01-12 07:04:27 +00:00
DB = db
}