feat: optional MySQL/MariaDB database driver for deployments that cannot use local SQLite

This commit is contained in:
fsylvain committed 2026-10-06 04:08:33 +02:00
1 parent 81e299f09a
commit d752a61e52
35 files changed
+2896 -76

No files matched your search

@@ -8,9 +8,16 @@ import (
"gorm.io/gorm"
)
type PoolSettings struct {
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime time.Duration
}
type Config struct {
Driver string
Source string
Pool *PoolSettings
}
type Driver interface {
@@ -20,6 +27,7 @@ type Driver interface {
var drivers = map[string]Driver{
DriverSQLite: sqliteDriver{},
DriverMySQL: mysqlDriver{},
}
type Connection struct {
@@ -36,9 +44,25 @@ func Connect(config Config) (*Connection, error) {
if err != nil {
return nil, err
}
if config.Pool != nil {
if err := applyPool(db, *config.Pool); err != nil {
return nil, err
}
}
return &Connection{DB: db, driver: driver}, nil
}
func applyPool(db *gorm.DB, pool PoolSettings) error {
sqlDb, err := db.DB()
if err != nil {
return err
}
sqlDb.SetMaxOpenConns(pool.MaxOpenConns)
sqlDb.SetMaxIdleConns(pool.MaxIdleConns)
sqlDb.SetConnMaxLifetime(pool.ConnMaxLifetime)
return nil
}
func (c *Connection) NewBackup(directory string, now func() time.Time) domainUpdate.DatabaseBackup {
return c.driver.NewBackup(c.DB, directory, now)
}