feat 数据库改为pgsql
Some checks failed
Build image / build (push) Failing after 15s

This commit is contained in:
timerzz 2024-05-12 16:35:46 +08:00
parent dcebdb399e
commit dcd21a0cd9
2 changed files with 8 additions and 9 deletions

View File

@ -2,16 +2,12 @@ package database
import ( import (
"fmt" "fmt"
"github.com/glebarez/sqlite" "gorm.io/driver/postgres"
"gorm.io/gorm" "gorm.io/gorm"
"haitao_watcher/pkg/options" "haitao_watcher/pkg/options"
) )
func InitDatabase(opt *options.DBOption) (*gorm.DB, error) { func InitDatabase(opt *options.DBOption) (*gorm.DB, error) {
if opt.Timeout == 0 { dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai", opt.Host, opt.User, opt.Password, opt.DBName, opt.Port)
opt.Timeout = 3000 return gorm.Open(postgres.Open(dsn), &gorm.Config{})
}
dialector := sqlite.Open(fmt.Sprintf("%s?_pragma=busy_timeout(%d)", opt.Path, opt.Timeout))
return gorm.Open(dialector, &gorm.Config{})
} }

View File

@ -1,6 +1,9 @@
package options package options
type DBOption struct { type DBOption struct {
Path string `json:"path"` Host string `yaml:"host"`
Timeout int `json:"timeout"` User string `yaml:"user"`
Password string `yaml:"password"`
Port string `yaml:"port"`
DBName string `yaml:"dbname"`
} }