2022-09-10 11:39:07 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2022-10-22 22:08:02 +00:00
|
|
|
"database/sql"
|
2024-01-01 16:52:18 +00:00
|
|
|
"fmt"
|
2022-10-22 22:08:02 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2023-04-01 13:34:27 +00:00
|
|
|
"time"
|
2022-09-10 11:39:07 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2022-10-22 22:08:02 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/glebarez/sqlite"
|
2022-10-29 13:01:01 +00:00
|
|
|
mysqlDriver "github.com/go-sql-driver/mysql"
|
2022-10-22 22:08:02 +00:00
|
|
|
"gorm.io/driver/mysql"
|
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/logger"
|
2022-09-10 11:39:07 +00:00
|
|
|
)
|
|
|
|
|
2023-04-01 13:34:27 +00:00
|
|
|
const (
|
|
|
|
mysqlMaxIdleConnections = 2 // Default as of Go 1.20
|
|
|
|
mysqlMaxOpenConnections = 10
|
|
|
|
)
|
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
type (
|
|
|
|
connector struct {
|
2022-10-22 22:08:02 +00:00
|
|
|
db *gorm.DB
|
2022-09-10 11:39:07 +00:00
|
|
|
encryptionSecret string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
// ErrCoreMetaNotFound is the error thrown when reading a non-existent
|
|
|
|
// core_kv key
|
|
|
|
var ErrCoreMetaNotFound = errors.New("core meta entry not found")
|
2022-09-10 11:39:07 +00:00
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
// New creates a new Connector with the given driver and database
|
2024-01-01 16:52:18 +00:00
|
|
|
func New(driverName, connString, encryptionSecret string) (c Connector, err error) {
|
2022-10-22 22:08:02 +00:00
|
|
|
var (
|
|
|
|
dbTuner func(*sql.DB, error) error
|
|
|
|
innerDB gorm.Dialector
|
|
|
|
)
|
2022-09-10 11:39:07 +00:00
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
switch driverName {
|
|
|
|
case "mysql":
|
2024-01-01 16:52:18 +00:00
|
|
|
if err = mysqlDriver.SetLogger(NewLogrusLogWriterWithLevel(logrus.StandardLogger(), logrus.ErrorLevel, driverName)); err != nil {
|
|
|
|
return nil, fmt.Errorf("setting logger on mysql driver: %w", err)
|
|
|
|
}
|
2022-10-22 22:08:02 +00:00
|
|
|
innerDB = mysql.Open(connString)
|
2023-04-01 13:34:27 +00:00
|
|
|
dbTuner = tuneMySQLDatabase
|
2022-09-10 11:39:07 +00:00
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
case "postgres":
|
|
|
|
innerDB = postgres.Open(connString)
|
|
|
|
|
|
|
|
case "sqlite":
|
|
|
|
var err error
|
|
|
|
if connString, err = patchSQLiteConnString(connString); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "patching connection string")
|
|
|
|
}
|
|
|
|
innerDB = sqlite.Open(connString)
|
|
|
|
dbTuner = tuneSQLiteDatabase
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unknown database driver %s", driverName)
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := gorm.Open(innerDB, &gorm.Config{
|
2023-07-14 14:15:58 +00:00
|
|
|
DisableForeignKeyConstraintWhenMigrating: true,
|
2023-11-25 19:23:34 +00:00
|
|
|
Logger: logger.New(NewLogrusLogWriterWithLevel(logrus.StandardLogger(), logrus.TraceLevel, driverName), logger.Config{
|
|
|
|
SlowThreshold: time.Second,
|
|
|
|
Colorful: false,
|
|
|
|
IgnoreRecordNotFoundError: false,
|
|
|
|
ParameterizedQueries: false,
|
|
|
|
LogLevel: logger.Info,
|
|
|
|
}),
|
2022-10-22 22:08:02 +00:00
|
|
|
})
|
2022-09-10 11:39:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "connecting database")
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
if dbTuner != nil {
|
|
|
|
if err = dbTuner(db.DB()); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "tuning database")
|
|
|
|
}
|
|
|
|
}
|
2022-09-10 11:39:07 +00:00
|
|
|
|
|
|
|
conn := &connector{
|
|
|
|
db: db,
|
|
|
|
encryptionSecret: encryptionSecret,
|
|
|
|
}
|
|
|
|
return conn, errors.Wrap(conn.applyCoreSchema(), "applying core schema")
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (connector) Close() error {
|
2022-10-22 22:08:02 +00:00
|
|
|
return nil
|
2022-09-10 11:39:07 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
func (connector) CopyDatabase(src, target *gorm.DB) error {
|
2023-11-25 19:23:34 +00:00
|
|
|
return CopyObjects(src, target, &coreKV{})
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
func (c connector) DB() *gorm.DB {
|
2022-09-10 11:39:07 +00:00
|
|
|
return c.db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c connector) applyCoreSchema() error {
|
2022-10-22 22:08:02 +00:00
|
|
|
return errors.Wrap(c.db.AutoMigrate(&coreKV{}), "applying coreKV schema")
|
|
|
|
}
|
|
|
|
|
|
|
|
func patchSQLiteConnString(connString string) (string, error) {
|
|
|
|
u, err := url.Parse(connString)
|
2022-09-10 11:39:07 +00:00
|
|
|
if err != nil {
|
2022-10-22 22:08:02 +00:00
|
|
|
return connString, errors.Wrap(err, "parsing connString")
|
2022-09-10 11:39:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
q := u.Query()
|
|
|
|
|
|
|
|
q.Add("_pragma", "locking_mode(EXCLUSIVE)")
|
|
|
|
q.Add("_pragma", "synchronous(FULL)")
|
|
|
|
|
|
|
|
u.RawQuery = strings.NewReplacer(
|
|
|
|
"%28", "(",
|
|
|
|
"%29", ")",
|
|
|
|
).Replace(q.Encode())
|
|
|
|
|
|
|
|
return u.String(), nil
|
|
|
|
}
|
|
|
|
|
2023-04-01 13:34:27 +00:00
|
|
|
func tuneMySQLDatabase(db *sql.DB, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting database")
|
|
|
|
}
|
|
|
|
|
|
|
|
// By default the package allows unlimited connections and the
|
|
|
|
// default value of a MySQL / MariaDB server is to allow 151
|
|
|
|
// connections at most. Therefore we tune the connection pool to
|
|
|
|
// sane values in order not to flood the database with connections
|
|
|
|
// in case a lot of events occur at the same time.
|
|
|
|
|
|
|
|
db.SetConnMaxIdleTime(time.Hour)
|
|
|
|
db.SetConnMaxLifetime(time.Hour)
|
|
|
|
db.SetMaxIdleConns(mysqlMaxIdleConnections)
|
|
|
|
db.SetMaxOpenConns(mysqlMaxOpenConnections)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
func tuneSQLiteDatabase(db *sql.DB, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting database")
|
2022-09-10 11:39:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
db.SetConnMaxIdleTime(0)
|
|
|
|
db.SetConnMaxLifetime(0)
|
|
|
|
db.SetMaxIdleConns(1)
|
|
|
|
db.SetMaxOpenConns(1)
|
|
|
|
|
|
|
|
return nil
|
2022-09-10 11:39:07 +00:00
|
|
|
}
|