mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-06-08 23:09:33 +00:00
c0061b99e3
- [x] Implement GORM V2 metadata store with SQLite, PostgreSQL, and MySQL support - [x] Add database migration system using gormigrate for schema versioning - [x] Create migration CLI tool with support for migrate, rollback, and status commands - [x] Add Docker support for migration container (Dockerfile.migrate) - [x] Implement automatic partition management for PostgreSQL time-series tables - [x] Add background aggregation worker for download statistics - [x] Support connection pooling configuration (max_open_conns, max_idle_conns, conn_max_lifetime) - [x] Add blocking mechanism based on vulnerability thresholds in stats and handlers - [x] Update Helm charts with migration init containers and multi-database configuration - [x] Replace deprecated SQLite store with optimized GORM implementation - [x] Add comprehensive integration tests for MySQL and PostgreSQL - [x] Update frontend to display blocked packages and storage utilization - [x] Add goreleaser configuration for migrate binary and container image - [x] Update configuration examples with database backend options and recommendations
79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
package gormstore
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/lukaszraczylo/gohoarder/pkg/errors"
|
|
)
|
|
|
|
// Config holds GORM store configuration
|
|
type Config struct {
|
|
// Database connection
|
|
Driver string // "sqlite", "postgres", "mysql"
|
|
DSN string // Data Source Name
|
|
|
|
// Connection pool
|
|
MaxOpenConns int
|
|
MaxIdleConns int
|
|
ConnMaxLifetime time.Duration
|
|
|
|
// GORM settings
|
|
LogLevel string // "silent", "error", "warn", "info"
|
|
}
|
|
|
|
// Validate validates the configuration
|
|
func (c *Config) Validate() error {
|
|
if c.Driver == "" {
|
|
return errors.New(errors.ErrCodeInvalidConfig, "driver is required")
|
|
}
|
|
if c.DSN == "" {
|
|
return errors.New(errors.ErrCodeInvalidConfig, "DSN is required")
|
|
}
|
|
|
|
// Set defaults
|
|
if c.MaxOpenConns == 0 {
|
|
c.MaxOpenConns = 25
|
|
}
|
|
if c.MaxIdleConns == 0 {
|
|
c.MaxIdleConns = 5
|
|
}
|
|
if c.ConnMaxLifetime == 0 {
|
|
c.ConnMaxLifetime = time.Hour
|
|
}
|
|
if c.LogLevel == "" {
|
|
c.LogLevel = "warn"
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// BuildPostgresDSN builds PostgreSQL DSN from structured config
|
|
func BuildPostgresDSN(host string, port int, user, password, database, sslmode string) string {
|
|
if sslmode == "" {
|
|
sslmode = "disable"
|
|
}
|
|
return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
|
|
host, port, user, password, database, sslmode)
|
|
}
|
|
|
|
// BuildMySQLDSN builds MySQL/MariaDB DSN from structured config
|
|
func BuildMySQLDSN(host string, port int, user, password, database, charset string) string {
|
|
if charset == "" {
|
|
charset = "utf8mb4"
|
|
}
|
|
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local",
|
|
user, password, host, port, database, charset)
|
|
}
|
|
|
|
// BuildSQLiteDSN builds SQLite DSN with pragmas
|
|
func BuildSQLiteDSN(path string, walMode bool) string {
|
|
if path == "" {
|
|
path = "gohoarder.db"
|
|
}
|
|
if walMode {
|
|
return fmt.Sprintf("%s?_journal_mode=WAL&_busy_timeout=5000&_synchronous=NORMAL&_cache_size=2000", path)
|
|
}
|
|
return fmt.Sprintf("%s?_journal_mode=DELETE&_busy_timeout=5000&_synchronous=NORMAL", path)
|
|
}
|