mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00

* refactor: this should be Debug() just like the rest. * feat: catch error when updating client table. Before if we provided the wrong ID it will just say it's successful when it shouldn't. * chore: handle the errors. * fix: defer tx.Rollback(). When I try handling the error we always hit the error no matter what even though there wasn't any error, This is due that defer block being executed unconditionally so even after we commit it successfully it will just give error. So add checking then commit it if all good. * feat: added testing env. This way we can use in memory sqlite. * chore: Delete log should be debug as well. * feat: enable foreign keys for testing for sqlite. I recommend enabling all together. Not sure why it's commented but for now will keep it the same and only enable for testing. * chore: catch error, if deleting a record fails. * chore: catch error, if deleting a record fails. * chore: catch error, when failed to enable toggle. * chore: catch error, if updating failed. * chore(filter): catch error, if deleting failed. * chore(filter): catch error, if row is not modified for ToggleEnabled. * chore(feed): Should be debug level to match with others. * chore(feed): catch error when nothing is updated. * chore: update docker-compose.yml add test_db for postgres. * chore(ci): update include postgres db service before running tests. * feat(database): Added database testing. * feat(database): Added api integration testing. * feat(database): Added action integration testing. * feat(database): Added download_client integration testing. * feat(database): Added filter integration testing. * test(database): initial tests model (WIP) * chore(feed): handle error when nothing is deleted. * tests(feed): added delete testing. * chore(feed): handle error when nothing is updated. * chore(feed): handle error when nothing is updated. * chore(feed): handle error when nothing is updated. * feat(database): Added feed integration testing. * fix(feed_cache): This should be time.Time not time.Duration. * chore(feed_cache): handle error when deleting fails. * feat(database): Added feed_cache integration testing. * chore: add EOL * feat: added indexer_test.go * feat: added mock irc data * fix: the column is not pass anymore it's password. * chore: added assertion. * fix: This is password column not pass test is failing because of it. * feat: added tests cases for irc. * feat: added test cases for release. * feat: added test cases for notifications. * feat: added Delete to the User DB that way it can be used for testing. * feat: added user database tests. * refactor: Make setupLogger and setupDatabase private also renamed them. Changed the visibility of `setupLogger` to private based on feedback. Also renamed the function to `setupLoggerForTest` and `setupDatabaseForTest` to make its purpose more descriptive. * fix(database): tests postgres ssl mode disable * refactor(database): setup and teardown --------- Co-authored-by: ze0s <43699394+zze0s@users.noreply.github.com>
142 lines
3.2 KiB
Go
142 lines
3.2 KiB
Go
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
package database
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
|
|
"github.com/autobrr/autobrr/internal/domain"
|
|
"github.com/autobrr/autobrr/internal/logger"
|
|
"github.com/autobrr/autobrr/pkg/errors"
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
type DB struct {
|
|
log zerolog.Logger
|
|
handler *sql.DB
|
|
lock sync.RWMutex
|
|
ctx context.Context
|
|
cancel func()
|
|
|
|
Driver string
|
|
DSN string
|
|
|
|
squirrel sq.StatementBuilderType
|
|
}
|
|
|
|
func NewDB(cfg *domain.Config, log logger.Logger) (*DB, error) {
|
|
db := &DB{
|
|
// set default placeholder for squirrel to support both sqlite and postgres
|
|
squirrel: sq.StatementBuilder.PlaceholderFormat(sq.Dollar),
|
|
log: log.With().Str("module", "database").Str("type", cfg.DatabaseType).Logger(),
|
|
}
|
|
db.ctx, db.cancel = context.WithCancel(context.Background())
|
|
|
|
switch cfg.DatabaseType {
|
|
case "sqlite":
|
|
db.Driver = "sqlite"
|
|
if os.Getenv("IS_TEST_ENV") == "true" {
|
|
db.DSN = ":memory:"
|
|
} else {
|
|
db.DSN = dataSourceName(cfg.ConfigPath, "autobrr.db")
|
|
}
|
|
case "postgres":
|
|
if cfg.PostgresHost == "" || cfg.PostgresPort == 0 || cfg.PostgresDatabase == "" {
|
|
return nil, errors.New("postgres: bad variables")
|
|
}
|
|
db.DSN = fmt.Sprintf("postgres://%v:%v@%v:%d/%v?sslmode=%v", cfg.PostgresUser, cfg.PostgresPass, cfg.PostgresHost, cfg.PostgresPort, cfg.PostgresDatabase, cfg.PostgresSSLMode)
|
|
if cfg.PostgresExtraParams != "" {
|
|
db.DSN = fmt.Sprintf("%s&%s", db.DSN, cfg.PostgresExtraParams)
|
|
}
|
|
db.Driver = "postgres"
|
|
default:
|
|
return nil, errors.New("unsupported database: %v", cfg.DatabaseType)
|
|
}
|
|
|
|
return db, nil
|
|
}
|
|
|
|
func (db *DB) Open() error {
|
|
if db.DSN == "" {
|
|
return errors.New("DSN required")
|
|
}
|
|
|
|
var err error
|
|
|
|
switch db.Driver {
|
|
case "sqlite":
|
|
if err = db.openSQLite(); err != nil {
|
|
db.log.Fatal().Err(err).Msg("could not open sqlite db connection")
|
|
return err
|
|
}
|
|
case "postgres":
|
|
if err = db.openPostgres(); err != nil {
|
|
db.log.Fatal().Err(err).Msg("could not open postgres db connection")
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (db *DB) Close() error {
|
|
switch db.Driver {
|
|
case "sqlite":
|
|
if err := db.closingSQLite(); err != nil {
|
|
db.log.Fatal().Err(err).Msg("could not run sqlite shutdown tasks")
|
|
}
|
|
case "postgres":
|
|
}
|
|
|
|
// cancel background context
|
|
db.cancel()
|
|
|
|
// close database
|
|
if db.handler != nil {
|
|
return db.handler.Close()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (db *DB) Ping() error {
|
|
return db.handler.Ping()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
type ILikeDynamic interface {
|
|
ToSql() (sql string, args []interface{}, err error)
|
|
}
|
|
|
|
// ILike is a wrapper for sq.Like and sq.ILike
|
|
// SQLite does not support ILike but postgres does so this checks what database is being used
|
|
func (db *DB) ILike(col string, val string) ILikeDynamic {
|
|
//if databaseDriver == "sqlite" {
|
|
if db.Driver == "sqlite" {
|
|
return sq.Like{col: val}
|
|
}
|
|
|
|
return sq.ILike{col: val}
|
|
}
|