mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +00:00
feat: wip postgres support
This commit is contained in:
parent
ffa2447c59
commit
cc0c071cce
16 changed files with 362 additions and 211 deletions
94
internal/database/database.go
Normal file
94
internal/database/database.go
Normal file
|
@ -0,0 +1,94 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
handler *sql.DB
|
||||
lock sync.RWMutex
|
||||
ctx context.Context
|
||||
cancel func()
|
||||
|
||||
Driver string
|
||||
DSN string
|
||||
}
|
||||
|
||||
func NewDB(cfg domain.Config) (*DB, error) {
|
||||
db := &DB{}
|
||||
db.ctx, db.cancel = context.WithCancel(context.Background())
|
||||
|
||||
switch cfg.DatabaseType {
|
||||
case "sqlite":
|
||||
db.Driver = "sqlite"
|
||||
db.DSN = dataSourceName(cfg.ConfigPath, "autobrr.db")
|
||||
case "postgres":
|
||||
if cfg.PostgresHost == "" || cfg.PostgresPort == 0 || cfg.PostgresDatabase == "" {
|
||||
return nil, fmt.Errorf("postgres: bad variables")
|
||||
}
|
||||
db.DSN = fmt.Sprintf("postgres://%v:%v@%v:%d/%v?sslmode=disable", cfg.PostgresUser, cfg.PostgresPass, cfg.PostgresHost, cfg.PostgresPort, cfg.PostgresDatabase)
|
||||
db.Driver = "postgres"
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported databse: %v", cfg.DatabaseType)
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func (db *DB) Open() error {
|
||||
if db.DSN == "" {
|
||||
return fmt.Errorf("DSN required")
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
switch db.Driver {
|
||||
case "sqlite":
|
||||
if err = db.openSQLite(); err != nil {
|
||||
log.Fatal().Err(err).Msg("could not open sqlite db connection")
|
||||
return err
|
||||
}
|
||||
case "postgres":
|
||||
if err = db.openPostgres(); err != nil {
|
||||
log.Fatal().Err(err).Msg("could not open postgres db connection")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) Close() error {
|
||||
// cancel background context
|
||||
db.cancel()
|
||||
|
||||
// close database
|
||||
if db.handler != nil {
|
||||
return db.handler.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
|
||||
tx, err := db.handler.BeginTx(ctx, opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Tx{
|
||||
Tx: tx,
|
||||
handler: db,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type Tx struct {
|
||||
*sql.Tx
|
||||
handler *DB
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue