2023-08-07 03:32:46 +08:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2023-09-08 18:37:11 +08:00
|
|
|
"embed"
|
2023-08-16 20:48:32 +08:00
|
|
|
"log"
|
|
|
|
"os"
|
2023-09-20 03:20:44 +08:00
|
|
|
"path/filepath"
|
2023-08-07 03:32:46 +08:00
|
|
|
"time"
|
|
|
|
|
2023-12-17 21:05:36 +08:00
|
|
|
"github.com/divyam234/teldrive/config"
|
2023-12-03 03:47:23 +08:00
|
|
|
"github.com/divyam234/teldrive/internal/kv"
|
2023-08-18 05:25:22 +08:00
|
|
|
"github.com/pressly/goose/v3"
|
2023-09-20 03:20:44 +08:00
|
|
|
"go.etcd.io/bbolt"
|
2023-08-07 03:32:46 +08:00
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/gorm"
|
2023-08-16 20:48:32 +08:00
|
|
|
"gorm.io/gorm/logger"
|
2023-08-07 03:32:46 +08:00
|
|
|
"gorm.io/gorm/schema"
|
|
|
|
)
|
|
|
|
|
2023-09-08 18:37:11 +08:00
|
|
|
//go:embed migrations/*.sql
|
|
|
|
var embedMigrations embed.FS
|
2023-08-07 03:32:46 +08:00
|
|
|
var DB *gorm.DB
|
2023-09-20 03:20:44 +08:00
|
|
|
var KV kv.KV
|
2023-08-07 03:32:46 +08:00
|
|
|
|
|
|
|
func InitDB() {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
2023-12-17 21:05:36 +08:00
|
|
|
logLevel := logger.Silent
|
|
|
|
|
|
|
|
if config.GetConfig().LogSql {
|
|
|
|
logLevel = logger.Info
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:48:32 +08:00
|
|
|
newLogger := logger.New(
|
|
|
|
log.New(os.Stdout, "\r\n", log.LstdFlags),
|
|
|
|
logger.Config{
|
2023-12-30 23:17:40 +08:00
|
|
|
SlowThreshold: time.Second,
|
|
|
|
LogLevel: logLevel,
|
|
|
|
ParameterizedQueries: true,
|
|
|
|
Colorful: true,
|
|
|
|
IgnoreRecordNotFoundError: true,
|
2023-08-16 20:48:32 +08:00
|
|
|
},
|
|
|
|
)
|
2023-08-12 19:21:42 +08:00
|
|
|
|
2023-12-17 21:05:36 +08:00
|
|
|
DB, err = gorm.Open(postgres.Open(config.GetConfig().DatabaseUrl), &gorm.Config{
|
2023-08-07 03:32:46 +08:00
|
|
|
NamingStrategy: schema.NamingStrategy{
|
|
|
|
TablePrefix: "teldrive.",
|
|
|
|
SingularTable: false,
|
|
|
|
},
|
|
|
|
PrepareStmt: false,
|
|
|
|
NowFunc: func() time.Time {
|
|
|
|
return time.Now().UTC()
|
|
|
|
},
|
2023-08-16 20:48:32 +08:00
|
|
|
Logger: newLogger,
|
2023-08-07 03:32:46 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlDB, err := DB.DB()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-12-03 03:47:23 +08:00
|
|
|
sqlDB.SetMaxIdleConns(25)
|
|
|
|
sqlDB.SetMaxOpenConns(25)
|
2023-12-03 05:23:06 +08:00
|
|
|
sqlDB.SetConnMaxIdleTime(10 * time.Minute)
|
2023-08-07 03:32:46 +08:00
|
|
|
|
|
|
|
go func() {
|
2023-12-17 21:05:36 +08:00
|
|
|
if config.GetConfig().RunMigrations {
|
2023-08-21 00:35:12 +08:00
|
|
|
migrate()
|
|
|
|
}
|
2023-08-07 03:32:46 +08:00
|
|
|
}()
|
|
|
|
|
2023-12-17 21:05:36 +08:00
|
|
|
boltDB, err := bbolt.Open(filepath.Join(config.GetConfig().ExecDir, "teldrive.db"), 0666, &bbolt.Options{
|
2023-09-20 03:20:44 +08:00
|
|
|
Timeout: time.Second,
|
|
|
|
NoGrowSync: false,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-12-03 05:46:53 +08:00
|
|
|
KV, err = kv.New(kv.Options{Bucket: "teldrive", DB: boltDB})
|
2023-09-20 03:20:44 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-08-07 03:32:46 +08:00
|
|
|
}
|
2023-08-18 03:43:36 +08:00
|
|
|
|
|
|
|
func migrate() {
|
|
|
|
|
2023-09-08 18:37:11 +08:00
|
|
|
goose.SetBaseFS(embedMigrations)
|
2023-08-26 01:32:05 +08:00
|
|
|
|
2023-08-18 03:43:36 +08:00
|
|
|
if err := goose.SetDialect("postgres"); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
db, _ := DB.DB()
|
2023-09-08 18:37:11 +08:00
|
|
|
if err := goose.Up(db, "migrations"); err != nil {
|
2023-08-18 03:43:36 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|