2023-09-02 22:30:57 +00:00
|
|
|
package db
|
2023-03-14 15:22:52 +00:00
|
|
|
|
|
|
|
import (
|
2023-04-17 12:25:39 +00:00
|
|
|
"errors"
|
2024-01-04 04:11:46 +00:00
|
|
|
"slices"
|
2023-06-16 16:08:33 +00:00
|
|
|
"strings"
|
|
|
|
|
2023-06-16 16:13:25 +00:00
|
|
|
msqlite "github.com/glebarez/go-sqlite"
|
2023-06-16 16:08:33 +00:00
|
|
|
"github.com/glebarez/sqlite"
|
2023-06-09 13:25:41 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/thomiceli/opengist/internal/config"
|
2023-03-14 15:22:52 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
var db *gorm.DB
|
|
|
|
|
2023-09-16 22:59:47 +00:00
|
|
|
func Setup(dbPath string, sharedCache bool) error {
|
2023-03-14 15:22:52 +00:00
|
|
|
var err error
|
2023-06-09 13:25:41 +00:00
|
|
|
journalMode := strings.ToUpper(config.C.SqliteJournalMode)
|
2023-03-14 15:22:52 +00:00
|
|
|
|
2024-01-04 04:11:46 +00:00
|
|
|
if !slices.Contains([]string{"DELETE", "TRUNCATE", "PERSIST", "MEMORY", "WAL", "OFF"}, journalMode) {
|
2023-06-09 13:25:41 +00:00
|
|
|
log.Warn().Msg("Invalid SQLite journal mode: " + journalMode)
|
|
|
|
}
|
|
|
|
|
2023-09-16 22:59:47 +00:00
|
|
|
sharedCacheStr := ""
|
|
|
|
if sharedCache {
|
|
|
|
sharedCacheStr = "&cache=shared"
|
|
|
|
}
|
|
|
|
|
|
|
|
if db, err = gorm.Open(sqlite.Open(dbPath+"?_fk=true&_journal_mode="+journalMode+sharedCacheStr), &gorm.Config{
|
2023-03-14 15:22:52 +00:00
|
|
|
Logger: logger.Default.LogMode(logger.Silent),
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-21 16:19:17 +00:00
|
|
|
if err = db.SetupJoinTable(&Gist{}, "Likes", &Like{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = db.SetupJoinTable(&User{}, "Liked", &Like{}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = db.AutoMigrate(&User{}, &Gist{}, &SSHKey{}, &AdminSetting{}); err != nil {
|
2023-03-14 15:22:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-17 00:55:17 +00:00
|
|
|
if err = ApplyMigrations(db); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-18 00:33:19 +00:00
|
|
|
|
2023-04-16 22:17:06 +00:00
|
|
|
// Default admin setting values
|
|
|
|
return initAdminSettings(map[string]string{
|
2023-05-12 10:06:55 +00:00
|
|
|
SettingDisableSignup: "0",
|
|
|
|
SettingRequireLogin: "0",
|
|
|
|
SettingDisableLoginForm: "0",
|
2023-05-26 07:15:37 +00:00
|
|
|
SettingDisableGravatar: "0",
|
2023-04-16 22:17:06 +00:00
|
|
|
})
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
2023-09-16 22:59:47 +00:00
|
|
|
func Close() error {
|
|
|
|
sqlDB, err := db.DB()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return sqlDB.Close()
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
func CountAll(table interface{}) (int64, error) {
|
|
|
|
var count int64
|
|
|
|
err := db.Model(table).Count(&count).Error
|
|
|
|
return count, err
|
|
|
|
}
|
2023-04-17 12:25:39 +00:00
|
|
|
|
|
|
|
func IsUniqueConstraintViolation(err error) bool {
|
2023-06-16 16:08:33 +00:00
|
|
|
var sqliteErr *msqlite.Error
|
|
|
|
if errors.As(err, &sqliteErr) && sqliteErr.Code() == 2067 {
|
2023-04-17 12:25:39 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2023-12-16 00:27:00 +00:00
|
|
|
|
|
|
|
func Ping() error {
|
|
|
|
sql, err := db.DB()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sql.Ping()
|
|
|
|
}
|