mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(logging); improve messages and errors (#336)
* feat(logger): add module context * feat(logger): change errors package * feat(logger): update tests
This commit is contained in:
parent
95471a4cf7
commit
0e88117702
69 changed files with 1172 additions and 957 deletions
|
@ -7,19 +7,21 @@ import (
|
|||
|
||||
"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 ActionRepo struct {
|
||||
log logger.Logger
|
||||
log zerolog.Logger
|
||||
db *DB
|
||||
clientRepo domain.DownloadClientRepo
|
||||
}
|
||||
|
||||
func NewActionRepo(log logger.Logger, db *DB, clientRepo domain.DownloadClientRepo) domain.ActionRepo {
|
||||
return &ActionRepo{
|
||||
log: log,
|
||||
log: log.With().Str("repo", "action").Logger(),
|
||||
db: db,
|
||||
clientRepo: clientRepo,
|
||||
}
|
||||
|
@ -87,14 +89,12 @@ func (r *ActionRepo) findByFilterID(ctx context.Context, tx *Tx, filterID int) (
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.findByFilterID: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
rows, err := tx.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.findByFilterID: query error")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
@ -112,8 +112,7 @@ func (r *ActionRepo) findByFilterID(ctx context.Context, tx *Tx, filterID int) (
|
|||
var paused, ignoreRules sql.NullBool
|
||||
|
||||
if err := rows.Scan(&a.ID, &a.Name, &a.Type, &a.Enabled, &execCmd, &execArgs, &watchFolder, &category, &tags, &label, &savePath, &paused, &ignoreRules, &limitDl, &limitUl, &limitRatio, &limitSeedTime, &a.ReAnnounceSkip, &a.ReAnnounceDelete, &a.ReAnnounceInterval, &a.ReAnnounceMaxAttempts, &webhookHost, &webhookType, &webhookMethod, &webhookData, &clientID); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.findByFilterID: error scanning row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
a.ExecCmd = execCmd.String
|
||||
|
@ -141,8 +140,7 @@ func (r *ActionRepo) findByFilterID(ctx context.Context, tx *Tx, filterID int) (
|
|||
actions = append(actions, &a)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.findByFilterID: row error")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "row error")
|
||||
}
|
||||
|
||||
return actions, nil
|
||||
|
@ -168,28 +166,24 @@ func (r *ActionRepo) attachDownloadClient(ctx context.Context, tx *Tx, clientID
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.attachDownloadClient: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
row := tx.QueryRowContext(ctx, query, args...)
|
||||
if err := row.Err(); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.attachDownloadClient: error query row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
var client domain.DownloadClient
|
||||
var settingsJsonStr string
|
||||
|
||||
if err := row.Scan(&client.ID, &client.Name, &client.Type, &client.Enabled, &client.Host, &client.Port, &client.TLS, &client.TLSSkipVerify, &client.Username, &client.Password, &settingsJsonStr); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.attachDownloadClient: error scanning row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
if settingsJsonStr != "" {
|
||||
if err := json.Unmarshal([]byte(settingsJsonStr), &client.Settings); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msgf("action.attachDownloadClient: could not marshal download client settings %v", settingsJsonStr)
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "could not unmarshal download client settings: %v", settingsJsonStr)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -230,14 +224,12 @@ func (r *ActionRepo) List(ctx context.Context) ([]domain.Action, error) {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.list: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
rows, err := r.db.handler.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.list: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
@ -253,8 +245,7 @@ func (r *ActionRepo) List(ctx context.Context) ([]domain.Action, error) {
|
|||
var paused, ignoreRules sql.NullBool
|
||||
|
||||
if err := rows.Scan(&a.ID, &a.Name, &a.Type, &a.Enabled, &execCmd, &execArgs, &watchFolder, &category, &tags, &label, &savePath, &paused, &ignoreRules, &limitDl, &limitUl, &limitRatio, &limitSeedTime, &a.ReAnnounceSkip, &a.ReAnnounceDelete, &a.ReAnnounceInterval, &a.ReAnnounceMaxAttempts, &webhookHost, &webhookType, &webhookMethod, &webhookData, &clientID); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.list: error scanning row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
a.Category = category.String
|
||||
|
@ -279,8 +270,7 @@ func (r *ActionRepo) List(ctx context.Context) ([]domain.Action, error) {
|
|||
actions = append(actions, a)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.list: row error")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "rows error")
|
||||
}
|
||||
|
||||
return actions, nil
|
||||
|
@ -293,14 +283,12 @@ func (r *ActionRepo) Delete(actionID int) error {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.delete: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.Exec(query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.delete: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
r.log.Debug().Msgf("action.delete: %v", actionID)
|
||||
|
@ -315,14 +303,12 @@ func (r *ActionRepo) DeleteByFilterID(ctx context.Context, filterID int) error {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.deleteByFilterID: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.deleteByFilterID: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
r.log.Debug().Msgf("action.deleteByFilterID: %v", filterID)
|
||||
|
@ -415,8 +401,7 @@ func (r *ActionRepo) Store(ctx context.Context, action domain.Action) (*domain.A
|
|||
|
||||
err := queryBuilder.QueryRowContext(ctx).Scan(&retID)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.store: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
r.log.Debug().Msgf("action.store: added new %v", retID)
|
||||
|
@ -480,14 +465,12 @@ func (r *ActionRepo) Update(ctx context.Context, action domain.Action) (*domain.
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.update: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.update: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
r.log.Debug().Msgf("action.update: %v", action.ID)
|
||||
|
@ -498,7 +481,7 @@ func (r *ActionRepo) Update(ctx context.Context, action domain.Action) (*domain.
|
|||
func (r *ActionRepo) StoreFilterActions(ctx context.Context, actions []*domain.Action, filterID int64) ([]*domain.Action, error) {
|
||||
tx, err := r.db.handler.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error begin transaction")
|
||||
}
|
||||
|
||||
defer tx.Rollback()
|
||||
|
@ -509,13 +492,11 @@ func (r *ActionRepo) StoreFilterActions(ctx context.Context, actions []*domain.A
|
|||
|
||||
deleteQuery, deleteArgs, err := deleteQueryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.StoreFilterActions: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, deleteQuery, deleteArgs...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.StoreFilterActions: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
for _, action := range actions {
|
||||
|
@ -602,8 +583,7 @@ func (r *ActionRepo) StoreFilterActions(ctx context.Context, actions []*domain.A
|
|||
|
||||
err = queryBuilder.QueryRowContext(ctx).Scan(&retID)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.StoreFilterActions: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
action.ID = retID
|
||||
|
@ -613,8 +593,7 @@ func (r *ActionRepo) StoreFilterActions(ctx context.Context, actions []*domain.A
|
|||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.StoreFilterActions: error updating actions")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error updating filter actions")
|
||||
|
||||
}
|
||||
|
||||
|
@ -631,14 +610,12 @@ func (r *ActionRepo) ToggleEnabled(actionID int) error {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.toggleEnabled: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.Exec(query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.toggleEnabled: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
r.log.Debug().Msgf("action.toggleEnabled: %v", actionID)
|
||||
|
|
|
@ -8,12 +8,14 @@ import (
|
|||
|
||||
"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 logger.Logger
|
||||
log zerolog.Logger
|
||||
handler *sql.DB
|
||||
lock sync.RWMutex
|
||||
ctx context.Context
|
||||
|
@ -29,7 +31,7 @@ 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,
|
||||
log: log.With().Str("module", "database").Logger(),
|
||||
}
|
||||
db.ctx, db.cancel = context.WithCancel(context.Background())
|
||||
|
||||
|
@ -39,12 +41,12 @@ func NewDB(cfg *domain.Config, log logger.Logger) (*DB, error) {
|
|||
db.DSN = dataSourceName(cfg.ConfigPath, "autobrr.db")
|
||||
case "postgres":
|
||||
if cfg.PostgresHost == "" || cfg.PostgresPort == 0 || cfg.PostgresDatabase == "" {
|
||||
return nil, fmt.Errorf("postgres: bad variables")
|
||||
return nil, errors.New("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 nil, errors.New("unsupported databse: %v", cfg.DatabaseType)
|
||||
}
|
||||
|
||||
return db, nil
|
||||
|
@ -52,7 +54,7 @@ func NewDB(cfg *domain.Config, log logger.Logger) (*DB, error) {
|
|||
|
||||
func (db *DB) Open() error {
|
||||
if db.DSN == "" {
|
||||
return fmt.Errorf("DSN required")
|
||||
return errors.New("DSN required")
|
||||
}
|
||||
|
||||
var err error
|
||||
|
|
|
@ -7,10 +7,13 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/internal/logger"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type DownloadClientRepo struct {
|
||||
log logger.Logger
|
||||
log zerolog.Logger
|
||||
db *DB
|
||||
cache *clientCache
|
||||
}
|
||||
|
@ -50,7 +53,7 @@ func (c *clientCache) Pop(id int) {
|
|||
|
||||
func NewDownloadClientRepo(log logger.Logger, db *DB) domain.DownloadClientRepo {
|
||||
return &DownloadClientRepo{
|
||||
log: log,
|
||||
log: log.With().Str("repo", "action").Logger(),
|
||||
db: db,
|
||||
cache: NewClientCache(),
|
||||
}
|
||||
|
@ -77,14 +80,12 @@ func (r *DownloadClientRepo) List(ctx context.Context) ([]domain.DownloadClient,
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("download_client.list: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
rows, err := r.db.handler.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("download_client.list: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
@ -94,22 +95,19 @@ func (r *DownloadClientRepo) List(ctx context.Context) ([]domain.DownloadClient,
|
|||
var settingsJsonStr string
|
||||
|
||||
if err := rows.Scan(&f.ID, &f.Name, &f.Type, &f.Enabled, &f.Host, &f.Port, &f.TLS, &f.TLSSkipVerify, &f.Username, &f.Password, &settingsJsonStr); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("download_client.list: error scanning row")
|
||||
return clients, err
|
||||
return clients, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
if settingsJsonStr != "" {
|
||||
if err := json.Unmarshal([]byte(settingsJsonStr), &f.Settings); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msgf("could not marshal download client settings %v", settingsJsonStr)
|
||||
return clients, err
|
||||
return clients, errors.Wrap(err, "could not unmarshal download client settings: %v", settingsJsonStr)
|
||||
}
|
||||
}
|
||||
|
||||
clients = append(clients, f)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("download_client.list: row error")
|
||||
return clients, err
|
||||
return clients, errors.Wrap(err, "rows error")
|
||||
}
|
||||
|
||||
return clients, nil
|
||||
|
@ -141,28 +139,24 @@ func (r *DownloadClientRepo) FindByID(ctx context.Context, id int32) (*domain.Do
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("download_client.findByID: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
row := r.db.handler.QueryRowContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("download_client.findByID: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
var client domain.DownloadClient
|
||||
var settingsJsonStr string
|
||||
|
||||
if err := row.Scan(&client.ID, &client.Name, &client.Type, &client.Enabled, &client.Host, &client.Port, &client.TLS, &client.TLSSkipVerify, &client.Username, &client.Password, &settingsJsonStr); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("download_client.findByID: error scanning row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
if settingsJsonStr != "" {
|
||||
if err := json.Unmarshal([]byte(settingsJsonStr), &client.Settings); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msgf("could not marshal download client settings %v", settingsJsonStr)
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "could not unmarshal download client settings: %v", settingsJsonStr)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -180,8 +174,7 @@ func (r *DownloadClientRepo) Store(ctx context.Context, client domain.DownloadCl
|
|||
|
||||
settingsJson, err := json.Marshal(&settings)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msgf("could not marshal download client settings %v", settings)
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error marshal download client settings %+v", settings)
|
||||
}
|
||||
|
||||
queryBuilder := r.db.squirrel.
|
||||
|
@ -195,8 +188,7 @@ func (r *DownloadClientRepo) Store(ctx context.Context, client domain.DownloadCl
|
|||
|
||||
err = queryBuilder.QueryRowContext(ctx).Scan(&retID)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("download_client.store: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
client.ID = retID
|
||||
|
@ -220,8 +212,7 @@ func (r *DownloadClientRepo) Update(ctx context.Context, client domain.DownloadC
|
|||
|
||||
settingsJson, err := json.Marshal(&settings)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msgf("could not marshal download client settings %v", settings)
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error marshal download client settings %+v", settings)
|
||||
}
|
||||
|
||||
queryBuilder := r.db.squirrel.
|
||||
|
@ -240,14 +231,12 @@ func (r *DownloadClientRepo) Update(ctx context.Context, client domain.DownloadC
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("download_client.update: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("download_client.update: error querying data")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
r.log.Debug().Msgf("download_client.update: %d", client.ID)
|
||||
|
@ -265,14 +254,12 @@ func (r *DownloadClientRepo) Delete(ctx context.Context, clientID int) error {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("download_client.delete: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
res, err := r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("download_client.delete: error query data")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
// remove from cache
|
||||
|
@ -280,7 +267,7 @@ func (r *DownloadClientRepo) Delete(ctx context.Context, clientID int) error {
|
|||
|
||||
rows, _ := res.RowsAffected()
|
||||
if rows == 0 {
|
||||
return err
|
||||
return errors.New("no rows affected")
|
||||
}
|
||||
|
||||
r.log.Info().Msgf("delete download client: %d", clientID)
|
||||
|
|
|
@ -6,19 +6,21 @@ import (
|
|||
|
||||
"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"
|
||||
)
|
||||
|
||||
func NewFeedRepo(log logger.Logger, db *DB) domain.FeedRepo {
|
||||
return &FeedRepo{
|
||||
log: log,
|
||||
log: log.With().Str("repo", "feed").Logger(),
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
type FeedRepo struct {
|
||||
log logger.Logger
|
||||
log zerolog.Logger
|
||||
db *DB
|
||||
}
|
||||
|
||||
|
@ -41,14 +43,12 @@ func (r *FeedRepo) FindByID(ctx context.Context, id int) (*domain.Feed, error) {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.FindById: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
row := r.db.handler.QueryRowContext(ctx, query, args...)
|
||||
if err := row.Err(); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.FindById: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
var f domain.Feed
|
||||
|
@ -56,8 +56,7 @@ func (r *FeedRepo) FindByID(ctx context.Context, id int) (*domain.Feed, error) {
|
|||
var apiKey sql.NullString
|
||||
|
||||
if err := row.Scan(&f.ID, &f.Indexer, &f.Name, &f.Type, &f.Enabled, &f.URL, &f.Interval, &apiKey, &f.CreatedAt, &f.UpdatedAt); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.FindById: error scanning row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
|
||||
}
|
||||
|
||||
|
@ -85,14 +84,12 @@ func (r *FeedRepo) FindByIndexerIdentifier(ctx context.Context, indexer string)
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.FindByIndexerIdentifier: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
row := r.db.handler.QueryRowContext(ctx, query, args...)
|
||||
if err := row.Err(); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.FindByIndexerIdentifier: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
var f domain.Feed
|
||||
|
@ -100,8 +97,7 @@ func (r *FeedRepo) FindByIndexerIdentifier(ctx context.Context, indexer string)
|
|||
var apiKey sql.NullString
|
||||
|
||||
if err := row.Scan(&f.ID, &f.Indexer, &f.Name, &f.Type, &f.Enabled, &f.URL, &f.Interval, &apiKey, &f.CreatedAt, &f.UpdatedAt); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.FindByIndexerIdentifier: error scanning row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
|
||||
}
|
||||
|
||||
|
@ -129,14 +125,12 @@ func (r *FeedRepo) Find(ctx context.Context) ([]domain.Feed, error) {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.Find: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
rows, err := r.db.handler.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.Find: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
@ -148,8 +142,7 @@ func (r *FeedRepo) Find(ctx context.Context) ([]domain.Feed, error) {
|
|||
var apiKey sql.NullString
|
||||
|
||||
if err := rows.Scan(&f.ID, &f.Indexer, &f.Name, &f.Type, &f.Enabled, &f.URL, &f.Interval, &apiKey, &f.CreatedAt, &f.UpdatedAt); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.Find: error scanning row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
|
||||
}
|
||||
|
||||
|
@ -189,8 +182,7 @@ func (r *FeedRepo) Store(ctx context.Context, feed *domain.Feed) error {
|
|||
var retID int
|
||||
|
||||
if err := queryBuilder.QueryRowContext(ctx).Scan(&retID); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.Store: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
feed.ID = retID
|
||||
|
@ -212,14 +204,12 @@ func (r *FeedRepo) Update(ctx context.Context, feed *domain.Feed) error {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.Update: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.Update: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -236,13 +226,11 @@ func (r *FeedRepo) ToggleEnabled(ctx context.Context, id int, enabled bool) erro
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.ToggleEnabled: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.ToggleEnabled: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -255,14 +243,12 @@ func (r *FeedRepo) Delete(ctx context.Context, id int) error {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.delete: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feed.delete: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
r.log.Info().Msgf("feed.delete: successfully deleted: %v", id)
|
||||
|
|
|
@ -6,16 +6,19 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/internal/logger"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type FeedCacheRepo struct {
|
||||
log logger.Logger
|
||||
log zerolog.Logger
|
||||
db *DB
|
||||
}
|
||||
|
||||
func NewFeedCacheRepo(log logger.Logger, db *DB) domain.FeedCacheRepo {
|
||||
return &FeedCacheRepo{
|
||||
log: log,
|
||||
log: log.With().Str("repo", "feed_cache").Logger(),
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
@ -33,22 +36,19 @@ func (r *FeedCacheRepo) Get(bucket string, key string) ([]byte, error) {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feedCache.Get: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
row := r.db.handler.QueryRow(query, args...)
|
||||
if err := row.Err(); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feedCache.Get: query error")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
var value []byte
|
||||
var ttl time.Duration
|
||||
|
||||
if err := row.Scan(&value, &ttl); err != nil && err != sql.ErrNoRows {
|
||||
r.log.Error().Stack().Err(err).Msg("feedCache.Get: error scanning row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
return value, nil
|
||||
|
@ -65,14 +65,13 @@ func (r *FeedCacheRepo) Exists(bucket string, key string) (bool, error) {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feedCache.Exists: error building query")
|
||||
return false, err
|
||||
return false, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
var exists bool
|
||||
err = r.db.handler.QueryRow(query, args...).Scan(&exists)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
r.log.Error().Stack().Err(err).Msg("feedCache.Exists: query error")
|
||||
return false, errors.Wrap(err, "error query")
|
||||
}
|
||||
|
||||
return exists, nil
|
||||
|
@ -86,13 +85,11 @@ func (r *FeedCacheRepo) Put(bucket string, key string, val []byte, ttl time.Time
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feedCache.Put: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
if _, err = r.db.handler.Exec(query, args...); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("feedCache.Put: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -7,19 +7,21 @@ import (
|
|||
|
||||
"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/lib/pq"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type FilterRepo struct {
|
||||
log logger.Logger
|
||||
log zerolog.Logger
|
||||
db *DB
|
||||
}
|
||||
|
||||
func NewFilterRepo(log logger.Logger, db *DB) domain.FilterRepo {
|
||||
return &FilterRepo{
|
||||
log: log,
|
||||
log: log.With().Str("repo", "filter").Logger(),
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
@ -40,14 +42,12 @@ func (r *FilterRepo) ListFilters(ctx context.Context) ([]domain.Filter, error) {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.list: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
rows, err := r.db.handler.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.list: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
@ -59,8 +59,7 @@ func (r *FilterRepo) ListFilters(ctx context.Context) ([]domain.Filter, error) {
|
|||
var matchReleases, exceptReleases sql.NullString
|
||||
|
||||
if err := rows.Scan(&f.ID, &f.Enabled, &f.Name, &matchReleases, &exceptReleases, &f.CreatedAt, &f.UpdatedAt); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.list: error scanning row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
f.MatchReleases = matchReleases.String
|
||||
|
@ -69,8 +68,7 @@ func (r *FilterRepo) ListFilters(ctx context.Context) ([]domain.Filter, error) {
|
|||
filters = append(filters, f)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.list: row error")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "row error")
|
||||
}
|
||||
|
||||
return filters, nil
|
||||
|
@ -133,14 +131,12 @@ func (r *FilterRepo) FindByID(ctx context.Context, filterID int) (*domain.Filter
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.findByID: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
row := r.db.handler.QueryRowContext(ctx, query, args...)
|
||||
if err := row.Err(); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.findByID: error query row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
var f domain.Filter
|
||||
|
@ -149,8 +145,7 @@ func (r *FilterRepo) FindByID(ctx context.Context, filterID int) (*domain.Filter
|
|||
var delay, maxDownloads, logScore sql.NullInt32
|
||||
|
||||
if err := row.Scan(&f.ID, &f.Enabled, &f.Name, &minSize, &maxSize, &delay, &f.Priority, &maxDownloads, &maxDownloadsUnit, &matchReleases, &exceptReleases, &useRegex, &matchReleaseGroups, &exceptReleaseGroups, &scene, &freeleech, &freeleechPercent, &shows, &seasons, &episodes, pq.Array(&f.Resolutions), pq.Array(&f.Codecs), pq.Array(&f.Sources), pq.Array(&f.Containers), pq.Array(&f.MatchHDR), pq.Array(&f.ExceptHDR), pq.Array(&f.MatchOther), pq.Array(&f.ExceptOther), &years, &artists, &albums, pq.Array(&f.MatchReleaseTypes), pq.Array(&f.Formats), pq.Array(&f.Quality), pq.Array(&f.Media), &logScore, &hasLog, &hasCue, &perfectFlac, &matchCategories, &exceptCategories, &matchUploaders, &exceptUploaders, &tags, &exceptTags, pq.Array(&f.Origins), &f.CreatedAt, &f.UpdatedAt); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msgf("filter.findByID: %v : error scanning row", filterID)
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
f.MinSize = minSize.String
|
||||
|
@ -191,7 +186,7 @@ func (r *FilterRepo) FindByIndexerIdentifier(indexer string) ([]domain.Filter, e
|
|||
ctx := context.TODO()
|
||||
tx, err := r.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error begin transaction")
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
|
@ -273,14 +268,12 @@ func (r *FilterRepo) findByIndexerIdentifier(ctx context.Context, tx *Tx, indexe
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.findByIndexerIdentifier: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
rows, err := tx.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.findByIndexerIdentifier: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
@ -294,8 +287,7 @@ func (r *FilterRepo) findByIndexerIdentifier(ctx context.Context, tx *Tx, indexe
|
|||
var delay, maxDownloads, logScore sql.NullInt32
|
||||
|
||||
if err := rows.Scan(&f.ID, &f.Enabled, &f.Name, &minSize, &maxSize, &delay, &f.Priority, &maxDownloads, &maxDownloadsUnit, &matchReleases, &exceptReleases, &useRegex, &matchReleaseGroups, &exceptReleaseGroups, &scene, &freeleech, &freeleechPercent, &shows, &seasons, &episodes, pq.Array(&f.Resolutions), pq.Array(&f.Codecs), pq.Array(&f.Sources), pq.Array(&f.Containers), pq.Array(&f.MatchHDR), pq.Array(&f.ExceptHDR), pq.Array(&f.MatchOther), pq.Array(&f.ExceptOther), &years, &artists, &albums, pq.Array(&f.MatchReleaseTypes), pq.Array(&f.Formats), pq.Array(&f.Quality), pq.Array(&f.Media), &logScore, &hasLog, &hasCue, &perfectFlac, &matchCategories, &exceptCategories, &matchUploaders, &exceptUploaders, &tags, &exceptTags, pq.Array(&f.Origins), &f.CreatedAt, &f.UpdatedAt); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.findByIndexerIdentifier: error scanning row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
f.MinSize = minSize.String
|
||||
|
@ -438,8 +430,7 @@ func (r *FilterRepo) Store(ctx context.Context, filter domain.Filter) (*domain.F
|
|||
|
||||
err := queryBuilder.QueryRowContext(ctx).Scan(&retID)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.store: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
filter.ID = retID
|
||||
|
@ -502,14 +493,12 @@ func (r *FilterRepo) Update(ctx context.Context, filter domain.Filter) (*domain.
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.update: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.update: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
return &filter, nil
|
||||
|
@ -526,13 +515,11 @@ func (r *FilterRepo) ToggleEnabled(ctx context.Context, filterID int, enabled bo
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.toggleEnabled: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.toggleEnabled: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -552,13 +539,11 @@ func (r *FilterRepo) StoreIndexerConnections(ctx context.Context, filterID int,
|
|||
|
||||
deleteQuery, deleteArgs, err := deleteQueryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.StoreIndexerConnections: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, deleteQuery, deleteArgs...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msgf("filter.StoreIndexerConnections: error deleting indexers for filter: %v", filterID)
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
for _, indexer := range indexers {
|
||||
|
@ -568,13 +553,11 @@ func (r *FilterRepo) StoreIndexerConnections(ctx context.Context, filterID int,
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.StoreIndexerConnections: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.StoreIndexerConnections: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
r.log.Debug().Msgf("filter.StoreIndexerConnections: store '%v' on filter: %v", indexer.Name, filterID)
|
||||
|
@ -582,8 +565,7 @@ func (r *FilterRepo) StoreIndexerConnections(ctx context.Context, filterID int,
|
|||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msgf("filter.StoreIndexerConnections: error storing indexers for filter: %v", filterID)
|
||||
return err
|
||||
return errors.Wrap(err, "error store indexers for filter: %v", filterID)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -596,14 +578,12 @@ func (r *FilterRepo) StoreIndexerConnection(ctx context.Context, filterID int, i
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.storeIndexerConnection: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.storeIndexerConnection: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -616,14 +596,12 @@ func (r *FilterRepo) DeleteIndexerConnections(ctx context.Context, filterID int)
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.deleteIndexerConnections: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.deleteIndexerConnections: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -636,14 +614,12 @@ func (r *FilterRepo) Delete(ctx context.Context, filterID int) error {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.delete: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.delete: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
r.log.Info().Msgf("filter.delete: successfully deleted: %v", filterID)
|
||||
|
@ -671,15 +647,13 @@ WHERE "release".filter_id = ?;`
|
|||
|
||||
row := tx.QueryRowContext(ctx, query, filterID)
|
||||
if err := row.Err(); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.downloadsByFilterSqlite: error querying stats")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
var f domain.FilterDownloads
|
||||
|
||||
if err := row.Scan(&f.HourCount, &f.DayCount, &f.WeekCount, &f.MonthCount, &f.TotalCount); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.downloadsByFilterSqlite: error scanning stats data to struct")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning stats data sqlite")
|
||||
}
|
||||
|
||||
return &f, nil
|
||||
|
@ -697,15 +671,13 @@ WHERE "release".filter_id = $1;`
|
|||
|
||||
row := tx.QueryRowContext(ctx, query, filterID)
|
||||
if err := row.Err(); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.downloadsByFilterPostgres: error querying stats")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
var f domain.FilterDownloads
|
||||
|
||||
if err := row.Scan(&f.HourCount, &f.DayCount, &f.WeekCount, &f.MonthCount, &f.TotalCount); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filter.downloadsByFilterPostgres: error scanning stats data to struct")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning stats data postgres")
|
||||
}
|
||||
|
||||
return &f, nil
|
||||
|
|
|
@ -8,16 +8,19 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/internal/logger"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type IndexerRepo struct {
|
||||
log logger.Logger
|
||||
log zerolog.Logger
|
||||
db *DB
|
||||
}
|
||||
|
||||
func NewIndexerRepo(log logger.Logger, db *DB) domain.IndexerRepo {
|
||||
return &IndexerRepo{
|
||||
log: log,
|
||||
log: log.With().Str("repo", "indexer").Logger(),
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
@ -25,8 +28,7 @@ func NewIndexerRepo(log logger.Logger, db *DB) domain.IndexerRepo {
|
|||
func (r *IndexerRepo) Store(ctx context.Context, indexer domain.Indexer) (*domain.Indexer, error) {
|
||||
settings, err := json.Marshal(indexer.Settings)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("error marshaling json data")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error marshaling json data")
|
||||
}
|
||||
|
||||
queryBuilder := r.db.squirrel.
|
||||
|
@ -39,8 +41,7 @@ func (r *IndexerRepo) Store(ctx context.Context, indexer domain.Indexer) (*domai
|
|||
|
||||
err = queryBuilder.QueryRowContext(ctx).Scan(&retID)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("indexer.store: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
indexer.ID = retID
|
||||
|
@ -51,8 +52,7 @@ func (r *IndexerRepo) Store(ctx context.Context, indexer domain.Indexer) (*domai
|
|||
func (r *IndexerRepo) Update(ctx context.Context, indexer domain.Indexer) (*domain.Indexer, error) {
|
||||
settings, err := json.Marshal(indexer.Settings)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("error marshaling json data")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error marshaling json data")
|
||||
}
|
||||
|
||||
queryBuilder := r.db.squirrel.
|
||||
|
@ -65,14 +65,12 @@ func (r *IndexerRepo) Update(ctx context.Context, indexer domain.Indexer) (*doma
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("indexer.update: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("indexer.update: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
return &indexer, nil
|
||||
|
@ -81,8 +79,7 @@ func (r *IndexerRepo) Update(ctx context.Context, indexer domain.Indexer) (*doma
|
|||
func (r *IndexerRepo) List(ctx context.Context) ([]domain.Indexer, error) {
|
||||
rows, err := r.db.handler.QueryContext(ctx, "SELECT id, enabled, name, identifier, implementation, settings FROM indexer ORDER BY name ASC")
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("indexer.list: error query indexer")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
@ -96,16 +93,14 @@ func (r *IndexerRepo) List(ctx context.Context) ([]domain.Indexer, error) {
|
|||
var settingsMap map[string]string
|
||||
|
||||
if err := rows.Scan(&f.ID, &f.Enabled, &f.Name, &f.Identifier, &implementation, &settings); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("indexer.list: error scanning data to struct")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
f.Implementation = implementation.String
|
||||
|
||||
err = json.Unmarshal([]byte(settings), &settingsMap)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("indexer.list: error unmarshal settings")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error unmarshal settings")
|
||||
}
|
||||
|
||||
f.Settings = settingsMap
|
||||
|
@ -113,7 +108,7 @@ func (r *IndexerRepo) List(ctx context.Context) ([]domain.Indexer, error) {
|
|||
indexers = append(indexers, f)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error rows")
|
||||
}
|
||||
|
||||
return indexers, nil
|
||||
|
@ -128,14 +123,12 @@ func (r *IndexerRepo) FindByFilterID(ctx context.Context, id int) ([]domain.Inde
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.check_existing_network: error fetching data")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
rows, err := r.db.handler.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("indexer.find_by_filter_id: error query indexer")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
@ -148,14 +141,12 @@ func (r *IndexerRepo) FindByFilterID(ctx context.Context, id int) ([]domain.Inde
|
|||
var settingsMap map[string]string
|
||||
|
||||
if err := rows.Scan(&f.ID, &f.Enabled, &f.Name, &f.Identifier, &settings); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("indexer.find_by_filter_id: error scanning data to struct")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
err = json.Unmarshal([]byte(settings), &settingsMap)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("indexer.find_by_filter_id: error unmarshal settings")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error unmarshal settings")
|
||||
}
|
||||
|
||||
f.Settings = settingsMap
|
||||
|
@ -163,7 +154,7 @@ func (r *IndexerRepo) FindByFilterID(ctx context.Context, id int) ([]domain.Inde
|
|||
indexers = append(indexers, f)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error rows")
|
||||
}
|
||||
|
||||
return indexers, nil
|
||||
|
@ -177,14 +168,12 @@ func (r *IndexerRepo) Delete(ctx context.Context, id int) error {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("indexer.delete: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msgf("indexer.delete: error executing query: '%v'", query)
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
r.log.Debug().Msgf("indexer.delete: id %v", id)
|
||||
|
|
|
@ -7,18 +7,19 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/internal/logger"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type IrcRepo struct {
|
||||
log logger.Logger
|
||||
log zerolog.Logger
|
||||
db *DB
|
||||
}
|
||||
|
||||
func NewIrcRepo(log logger.Logger, db *DB) domain.IrcRepo {
|
||||
return &IrcRepo{
|
||||
log: log,
|
||||
log: log.With().Str("repo", "irc").Logger(),
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
@ -31,8 +32,7 @@ func (r *IrcRepo) GetNetworkByID(ctx context.Context, id int64) (*domain.IrcNetw
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.getNetworkByID: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
r.log.Trace().Str("database", "irc.check_existing_network").Msgf("query: '%v', args: '%v'", query, args)
|
||||
|
||||
|
@ -44,8 +44,7 @@ func (r *IrcRepo) GetNetworkByID(ctx context.Context, id int64) (*domain.IrcNetw
|
|||
|
||||
row := r.db.handler.QueryRowContext(ctx, query, args...)
|
||||
if err := row.Scan(&n.ID, &n.Enabled, &n.Name, &n.Server, &n.Port, &tls, &pass, &inviteCmd, &nsAccount, &nsPassword); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.getNetworkByID: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
n.TLS = tls.Bool
|
||||
|
@ -60,7 +59,7 @@ func (r *IrcRepo) GetNetworkByID(ctx context.Context, id int64) (*domain.IrcNetw
|
|||
func (r *IrcRepo) DeleteNetwork(ctx context.Context, id int64) error {
|
||||
tx, err := r.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.Wrap(err, "error begin transaction")
|
||||
}
|
||||
|
||||
defer tx.Rollback()
|
||||
|
@ -71,14 +70,12 @@ func (r *IrcRepo) DeleteNetwork(ctx context.Context, id int64) error {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.deleteNetwork: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = tx.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.deleteNetwork: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
netQueryBuilder := r.db.squirrel.
|
||||
|
@ -87,20 +84,17 @@ func (r *IrcRepo) DeleteNetwork(ctx context.Context, id int64) error {
|
|||
|
||||
netQuery, netArgs, err := netQueryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.deleteNetwork: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = tx.ExecContext(ctx, netQuery, netArgs...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.deleteNetwork: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msgf("irc.deleteNetwork: error deleting network %v", id)
|
||||
return err
|
||||
return errors.Wrap(err, "error commit deleting network")
|
||||
|
||||
}
|
||||
|
||||
|
@ -115,14 +109,12 @@ func (r *IrcRepo) FindActiveNetworks(ctx context.Context) ([]domain.IrcNetwork,
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.findActiveNetworks: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
rows, err := r.db.handler.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.findActiveNetworks: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
@ -136,8 +128,7 @@ func (r *IrcRepo) FindActiveNetworks(ctx context.Context) ([]domain.IrcNetwork,
|
|||
var tls sql.NullBool
|
||||
|
||||
if err := rows.Scan(&net.ID, &net.Enabled, &net.Name, &net.Server, &net.Port, &tls, &pass, &inviteCmd, &nsAccount, &nsPassword); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.findActiveNetworks: error scanning row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
net.TLS = tls.Bool
|
||||
|
@ -150,8 +141,7 @@ func (r *IrcRepo) FindActiveNetworks(ctx context.Context) ([]domain.IrcNetwork,
|
|||
networks = append(networks, net)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.findActiveNetworks: row error")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error row")
|
||||
}
|
||||
|
||||
return networks, nil
|
||||
|
@ -165,14 +155,12 @@ func (r *IrcRepo) ListNetworks(ctx context.Context) ([]domain.IrcNetwork, error)
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.listNetworks: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
rows, err := r.db.handler.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.listNetworks: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
@ -186,8 +174,7 @@ func (r *IrcRepo) ListNetworks(ctx context.Context) ([]domain.IrcNetwork, error)
|
|||
var tls sql.NullBool
|
||||
|
||||
if err := rows.Scan(&net.ID, &net.Enabled, &net.Name, &net.Server, &net.Port, &tls, &pass, &inviteCmd, &nsAccount, &nsPassword); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.listNetworks: error scanning row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
net.TLS = tls.Bool
|
||||
|
@ -200,8 +187,7 @@ func (r *IrcRepo) ListNetworks(ctx context.Context) ([]domain.IrcNetwork, error)
|
|||
networks = append(networks, net)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.listNetworks: row error")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error row")
|
||||
}
|
||||
|
||||
return networks, nil
|
||||
|
@ -215,14 +201,12 @@ func (r *IrcRepo) ListChannels(networkID int64) ([]domain.IrcChannel, error) {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.listChannels: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
rows, err := r.db.handler.Query(query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.listChannels: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
|
@ -232,8 +216,7 @@ func (r *IrcRepo) ListChannels(networkID int64) ([]domain.IrcChannel, error) {
|
|||
var pass sql.NullString
|
||||
|
||||
if err := rows.Scan(&ch.ID, &ch.Name, &ch.Enabled, &pass); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.listChannels: error scanning row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
ch.Password = pass.String
|
||||
|
@ -241,8 +224,7 @@ func (r *IrcRepo) ListChannels(networkID int64) ([]domain.IrcChannel, error) {
|
|||
channels = append(channels, ch)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.listChannels: error row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error row")
|
||||
}
|
||||
|
||||
return channels, nil
|
||||
|
@ -257,8 +239,7 @@ func (r *IrcRepo) CheckExistingNetwork(ctx context.Context, network *domain.IrcN
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.checkExistingNetwork: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
r.log.Trace().Str("database", "irc.checkExistingNetwork").Msgf("query: '%v', args: '%v'", query, args)
|
||||
|
||||
|
@ -274,8 +255,7 @@ func (r *IrcRepo) CheckExistingNetwork(ctx context.Context, network *domain.IrcN
|
|||
// no result is not an error in our case
|
||||
return nil, nil
|
||||
} else if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.checkExistingNetwork: error scanning data to struct")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
net.TLS = tls.Bool
|
||||
|
@ -326,7 +306,6 @@ func (r *IrcRepo) StoreNetwork(network *domain.IrcNetwork) error {
|
|||
|
||||
err = queryBuilder.QueryRow().Scan(&retID)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.storeNetwork: error executing query")
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
|
@ -361,15 +340,13 @@ func (r *IrcRepo) UpdateNetwork(ctx context.Context, network *domain.IrcNetwork)
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.updateNetwork: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
// update record
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.updateNetwork: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
return err
|
||||
|
@ -391,14 +368,12 @@ func (r *IrcRepo) StoreNetworkChannels(ctx context.Context, networkID int64, cha
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.storeNetworkChannels: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = tx.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.storeNetworkChannels: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
for _, channel := range channels {
|
||||
|
@ -429,8 +404,7 @@ func (r *IrcRepo) StoreNetworkChannels(ctx context.Context, networkID int64, cha
|
|||
|
||||
err = channelQueryBuilder.QueryRowContext(ctx).Scan(&retID)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.storeNetworkChannels: error executing query")
|
||||
return errors.Wrap(err, "error executing query")
|
||||
return errors.Wrap(err, "error executing query storeNetworkChannels")
|
||||
}
|
||||
|
||||
channel.ID = retID
|
||||
|
@ -452,8 +426,7 @@ func (r *IrcRepo) StoreNetworkChannels(ctx context.Context, networkID int64, cha
|
|||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msgf("irc.storeNetworkChannels: error deleting network: %v", networkID)
|
||||
return err
|
||||
return errors.Wrap(err, "error commit transaction store network")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -475,14 +448,12 @@ func (r *IrcRepo) StoreChannel(networkID int64, channel *domain.IrcChannel) erro
|
|||
|
||||
query, args, err := channelQueryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.storeChannel: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.Exec(query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.storeChannel: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
} else {
|
||||
queryBuilder := r.db.squirrel.
|
||||
|
@ -509,7 +480,6 @@ func (r *IrcRepo) StoreChannel(networkID int64, channel *domain.IrcChannel) erro
|
|||
|
||||
err = queryBuilder.QueryRow().Scan(&retID)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.storeChannels: error executing query")
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
|
@ -548,14 +518,12 @@ func (r *IrcRepo) UpdateChannel(channel *domain.IrcChannel) error {
|
|||
|
||||
query, args, err := channelQueryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.updateChannel: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.Exec(query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.updateChannel: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
return err
|
||||
|
@ -571,14 +539,12 @@ func (r *IrcRepo) UpdateInviteCommand(networkID int64, invite string) error {
|
|||
|
||||
query, args, err := channelQueryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.UpdateInviteCommand: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.Exec(query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("irc.UpdateInviteCommand: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
return err
|
||||
|
|
|
@ -6,19 +6,21 @@ import (
|
|||
|
||||
"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/lib/pq"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type NotificationRepo struct {
|
||||
log logger.Logger
|
||||
log zerolog.Logger
|
||||
db *DB
|
||||
}
|
||||
|
||||
func NewNotificationRepo(log logger.Logger, db *DB) domain.NotificationRepo {
|
||||
return &NotificationRepo{
|
||||
log: log,
|
||||
log: log.With().Str("repo", "notification").Logger(),
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
@ -32,14 +34,12 @@ func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQ
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("notification.find: error building query")
|
||||
return nil, 0, err
|
||||
return nil, 0, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
rows, err := r.db.handler.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("notification.find: error executing query")
|
||||
return nil, 0, err
|
||||
return nil, 0, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
@ -54,8 +54,7 @@ func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQ
|
|||
//if err := rows.Scan(&n.ID, &n.Name, &n.Type, &n.Enabled, pq.Array(&n.Events), &token, &apiKey, &webhook, &title, &icon, &host, &username, &password, &channel, &targets, &devices, &n.CreatedAt, &n.UpdatedAt); err != nil {
|
||||
//var token, apiKey, webhook, title, icon, host, username, password, channel, targets, devices sql.NullString
|
||||
if err := rows.Scan(&n.ID, &n.Name, &n.Type, &n.Enabled, pq.Array(&n.Events), &webhook, &token, &channel, &n.CreatedAt, &n.UpdatedAt, &totalCount); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("notification.find: error scanning row")
|
||||
return nil, 0, err
|
||||
return nil, 0, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
//n.APIKey = apiKey.String
|
||||
|
@ -74,7 +73,7 @@ func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQ
|
|||
notifications = append(notifications, n)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, errors.Wrap(err, "error rows find")
|
||||
}
|
||||
|
||||
return notifications, totalCount, nil
|
||||
|
@ -82,17 +81,9 @@ func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQ
|
|||
|
||||
func (r *NotificationRepo) List(ctx context.Context) ([]domain.Notification, error) {
|
||||
|
||||
//queryBuilder := r.db.squirrel.
|
||||
// Select("r.id", "r.filter_status", "r.rejections", "r.indexer", "r.filter", "r.protocol", "r.title", "r.torrent_name", "r.size", "r.timestamp", "COUNT(*) OVER() AS total_count").
|
||||
// From("release r").
|
||||
// OrderBy("r.timestamp DESC")
|
||||
//
|
||||
//query, args, err := queryBuilder.ToSql()
|
||||
|
||||
rows, err := r.db.handler.QueryContext(ctx, "SELECT id, name, type, enabled, events, token, api_key, webhook, title, icon, host, username, password, channel, targets, devices, created_at, updated_at FROM notification ORDER BY name ASC")
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("filters_list: error query data")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
@ -104,8 +95,7 @@ func (r *NotificationRepo) List(ctx context.Context) ([]domain.Notification, err
|
|||
|
||||
var token, apiKey, webhook, title, icon, host, username, password, channel, targets, devices sql.NullString
|
||||
if err := rows.Scan(&n.ID, &n.Name, &n.Type, &n.Enabled, pq.Array(&n.Events), &token, &apiKey, &webhook, &title, &icon, &host, &username, &password, &channel, &targets, &devices, &n.CreatedAt, &n.UpdatedAt); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("notification_list: error scanning data to struct")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
//n.Events = ([]domain.NotificationEvent)(eventsSlice)
|
||||
|
@ -124,7 +114,7 @@ func (r *NotificationRepo) List(ctx context.Context) ([]domain.Notification, err
|
|||
notifications = append(notifications, n)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error rows list")
|
||||
}
|
||||
|
||||
return notifications, nil
|
||||
|
@ -148,22 +138,20 @@ func (r *NotificationRepo) FindByID(ctx context.Context, id int) (*domain.Notifi
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("notification.findByID: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
//row := r.db.handler.QueryRowContext(ctx, "SELECT id, name, type, enabled, events, token, api_key, webhook, title, icon, host, username, password, channel, targets, devices, created_at, updated_at FROM notification WHERE id = ?", id)
|
||||
row := r.db.handler.QueryRowContext(ctx, query, args...)
|
||||
if err := row.Err(); err != nil {
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
var n domain.Notification
|
||||
|
||||
var token, apiKey, webhook, title, icon, host, username, password, channel, targets, devices sql.NullString
|
||||
if err := row.Scan(&n.ID, &n.Name, &n.Type, &n.Enabled, pq.Array(&n.Events), &token, &apiKey, &webhook, &title, &icon, &host, &username, &password, &channel, &targets, &devices, &n.CreatedAt, &n.UpdatedAt); err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("notification.findByID: error scanning row")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
n.Token = token.String
|
||||
|
@ -213,8 +201,7 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi
|
|||
|
||||
err := queryBuilder.QueryRowContext(ctx).Scan(&retID)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("notification.store: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
r.log.Debug().Msgf("notification.store: added new %v", retID)
|
||||
|
@ -242,14 +229,12 @@ func (r *NotificationRepo) Update(ctx context.Context, notification domain.Notif
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("action.update: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("notification.update: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
r.log.Debug().Msgf("notification.update: %v", notification.Name)
|
||||
|
@ -264,14 +249,12 @@ func (r *NotificationRepo) Delete(ctx context.Context, notificationID int) error
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("notification.delete: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("notification.delete: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
r.log.Info().Msgf("notification.delete: successfully deleted: %v", notificationID)
|
||||
|
|
|
@ -2,8 +2,8 @@ package database
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
@ -14,19 +14,19 @@ func (db *DB) openPostgres() error {
|
|||
// open database connection
|
||||
if db.handler, err = sql.Open("postgres", db.DSN); err != nil {
|
||||
db.log.Fatal().Err(err).Msg("could not open postgres connection")
|
||||
return err
|
||||
return errors.Wrap(err, "could not open postgres connection")
|
||||
}
|
||||
|
||||
err = db.handler.Ping()
|
||||
if err != nil {
|
||||
db.log.Fatal().Err(err).Msg("could not ping postgres database")
|
||||
return err
|
||||
return errors.Wrap(err, "could not ping postgres database")
|
||||
}
|
||||
|
||||
// migrate db
|
||||
if err = db.migratePostgres(); err != nil {
|
||||
db.log.Fatal().Err(err).Msg("could not migrate postgres database")
|
||||
return err
|
||||
return errors.Wrap(err, "could not migrate postgres database")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -35,7 +35,7 @@ func (db *DB) openPostgres() error {
|
|||
func (db *DB) migratePostgres() error {
|
||||
tx, err := db.handler.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.Wrap(err, "error starting transaction")
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
|
@ -45,37 +45,37 @@ func (db *DB) migratePostgres() error {
|
|||
);`
|
||||
|
||||
if _, err := tx.Exec(initialSchema); err != nil {
|
||||
return fmt.Errorf("failed to create schema_migrations table: %s", err)
|
||||
return errors.New("failed to create schema_migrations table")
|
||||
}
|
||||
|
||||
var version int
|
||||
err = tx.QueryRow(`SELECT version FROM schema_migrations`).Scan(&version)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
return errors.Wrap(err, "no rows")
|
||||
}
|
||||
|
||||
if version == len(postgresMigrations) {
|
||||
return nil
|
||||
}
|
||||
if version > len(postgresMigrations) {
|
||||
return fmt.Errorf("old")
|
||||
return errors.New("old")
|
||||
}
|
||||
|
||||
if version == 0 {
|
||||
if _, err := tx.Exec(postgresSchema); err != nil {
|
||||
return fmt.Errorf("failed to initialize schema: %v", err)
|
||||
return errors.Wrap(err, "failed to initialize schema")
|
||||
}
|
||||
} else {
|
||||
for i := version; i < len(postgresMigrations); i++ {
|
||||
if _, err := tx.Exec(postgresMigrations[i]); err != nil {
|
||||
return fmt.Errorf("failed to execute migration #%v: %v", i, err)
|
||||
return errors.Wrap(err, "failed to execute migration #%v", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, err = tx.Exec(`INSERT INTO schema_migrations (id, version) VALUES (1, $1) ON CONFLICT (id) DO UPDATE SET version = $1`, len(postgresMigrations))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to bump schema version: %v", err)
|
||||
return errors.Wrap(err, "failed to bump schema version")
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
|
|
|
@ -8,19 +8,21 @@ import (
|
|||
|
||||
"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/lib/pq"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type ReleaseRepo struct {
|
||||
log logger.Logger
|
||||
log zerolog.Logger
|
||||
db *DB
|
||||
}
|
||||
|
||||
func NewReleaseRepo(log logger.Logger, db *DB) domain.ReleaseRepo {
|
||||
return &ReleaseRepo{
|
||||
log: log,
|
||||
log: log.With().Str("repo", "release").Logger(),
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
@ -40,8 +42,7 @@ func (repo *ReleaseRepo) Store(ctx context.Context, r *domain.Release) (*domain.
|
|||
|
||||
err := queryBuilder.QueryRowContext(ctx).Scan(&retID)
|
||||
if err != nil {
|
||||
repo.log.Error().Stack().Err(err).Msg("release.store: error executing query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
r.ID = retID
|
||||
|
@ -63,14 +64,12 @@ func (repo *ReleaseRepo) StoreReleaseActionStatus(ctx context.Context, a *domain
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
repo.log.Error().Stack().Err(err).Msg("release.store: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = repo.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
repo.log.Error().Stack().Err(err).Msg("error updating status of release")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
} else {
|
||||
|
@ -85,8 +84,7 @@ func (repo *ReleaseRepo) StoreReleaseActionStatus(ctx context.Context, a *domain
|
|||
|
||||
err := queryBuilder.QueryRowContext(ctx).Scan(&retID)
|
||||
if err != nil {
|
||||
repo.log.Error().Stack().Err(err).Msg("release.storeReleaseActionStatus: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
a.ID = retID
|
||||
|
@ -100,7 +98,7 @@ func (repo *ReleaseRepo) StoreReleaseActionStatus(ctx context.Context, a *domain
|
|||
func (repo *ReleaseRepo) Find(ctx context.Context, params domain.ReleaseQueryParams) ([]*domain.Release, int64, int64, error) {
|
||||
tx, err := repo.db.BeginTx(ctx, &sql.TxOptions{})
|
||||
if err != nil {
|
||||
return nil, 0, 0, err
|
||||
return nil, 0, 0, errors.Wrap(err, "error begin transaction")
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
|
@ -118,8 +116,7 @@ func (repo *ReleaseRepo) Find(ctx context.Context, params domain.ReleaseQueryPar
|
|||
}
|
||||
|
||||
if err = tx.Commit(); err != nil {
|
||||
repo.log.Error().Stack().Err(err).Msg("error finding releases")
|
||||
return nil, 0, 0, err
|
||||
return nil, 0, 0, errors.Wrap(err, "error commit transaction find releases")
|
||||
}
|
||||
|
||||
return releases, nextCursor, total, nil
|
||||
|
@ -165,23 +162,20 @@ func (repo *ReleaseRepo) findReleases(ctx context.Context, tx *Tx, params domain
|
|||
query, args, err := queryBuilder.ToSql()
|
||||
repo.log.Trace().Str("database", "release.find").Msgf("query: '%v', args: '%v'", query, args)
|
||||
if err != nil {
|
||||
repo.log.Error().Stack().Err(err).Msg("error building query")
|
||||
return nil, 0, 0, err
|
||||
return nil, 0, 0, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
res := make([]*domain.Release, 0)
|
||||
|
||||
rows, err := tx.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
repo.log.Error().Stack().Err(err).Msg("error fetching releases")
|
||||
return res, 0, 0, nil
|
||||
return nil, 0, 0, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
repo.log.Error().Stack().Err(err)
|
||||
return res, 0, 0, err
|
||||
return res, 0, 0, errors.Wrap(err, "error rows findreleases")
|
||||
}
|
||||
|
||||
var countItems int64 = 0
|
||||
|
@ -192,8 +186,7 @@ func (repo *ReleaseRepo) findReleases(ctx context.Context, tx *Tx, params domain
|
|||
var indexer, filter sql.NullString
|
||||
|
||||
if err := rows.Scan(&rls.ID, &rls.FilterStatus, pq.Array(&rls.Rejections), &indexer, &filter, &rls.Protocol, &rls.Title, &rls.TorrentName, &rls.Size, &rls.Timestamp, &countItems); err != nil {
|
||||
repo.log.Error().Stack().Err(err).Msg("release.find: error scanning data to struct")
|
||||
return res, 0, 0, err
|
||||
return res, 0, 0, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
rls.Indexer = indexer.String
|
||||
|
@ -373,23 +366,20 @@ func (repo *ReleaseRepo) attachActionStatus(ctx context.Context, tx *Tx, release
|
|||
|
||||
rows, err := tx.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
repo.log.Error().Stack().Err(err).Msg("error fetching releases")
|
||||
return res, nil
|
||||
return res, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
repo.log.Error().Stack().Err(err)
|
||||
return res, err
|
||||
return res, errors.Wrap(err, "error rows")
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var rls domain.ReleaseActionStatus
|
||||
|
||||
if err := rows.Scan(&rls.ID, &rls.Status, &rls.Action, &rls.Type, pq.Array(&rls.Rejections), &rls.Timestamp); err != nil {
|
||||
repo.log.Error().Stack().Err(err).Msg("release.find: error scanning data to struct")
|
||||
return res, err
|
||||
return res, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
res = append(res, rls)
|
||||
|
@ -411,15 +401,13 @@ FROM "release";`
|
|||
|
||||
row := repo.db.handler.QueryRowContext(ctx, query)
|
||||
if err := row.Err(); err != nil {
|
||||
repo.log.Error().Stack().Err(err).Msg("release.stats: error querying stats")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
var rls domain.ReleaseStats
|
||||
|
||||
if err := row.Scan(&rls.TotalCount, &rls.FilteredCount, &rls.FilterRejectedCount, &rls.PushApprovedCount, &rls.PushRejectedCount); err != nil {
|
||||
repo.log.Error().Stack().Err(err).Msg("release.stats: error scanning stats data to struct")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
return &rls, nil
|
||||
|
@ -435,20 +423,17 @@ func (repo *ReleaseRepo) Delete(ctx context.Context) error {
|
|||
|
||||
_, err = tx.ExecContext(ctx, `DELETE FROM "release"`)
|
||||
if err != nil {
|
||||
repo.log.Error().Stack().Err(err).Msg("error deleting all releases")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
_, err = tx.ExecContext(ctx, `DELETE FROM release_action_status`)
|
||||
if err != nil {
|
||||
repo.log.Error().Stack().Err(err).Msg("error deleting all release_action_status")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
repo.log.Error().Stack().Err(err).Msg("error deleting all releases")
|
||||
return err
|
||||
return errors.Wrap(err, "error commit transaction delete")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -4,13 +4,15 @@ import (
|
|||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
"github.com/lib/pq"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func (db *DB) openSQLite() error {
|
||||
if db.DSN == "" {
|
||||
return fmt.Errorf("DSN required")
|
||||
return errors.New("DSN required")
|
||||
}
|
||||
|
||||
var err error
|
||||
|
@ -23,20 +25,20 @@ func (db *DB) openSQLite() error {
|
|||
|
||||
// Set busy timeout
|
||||
//if _, err = db.handler.Exec(`PRAGMA busy_timeout = 5000;`); err != nil {
|
||||
// return fmt.Errorf("busy timeout pragma: %w", err)
|
||||
// return errors.New("busy timeout pragma: %w", err)
|
||||
//}
|
||||
|
||||
// Enable WAL. SQLite performs better with the WAL because it allows
|
||||
// multiple readers to operate while data is being written.
|
||||
if _, err = db.handler.Exec(`PRAGMA journal_mode = wal;`); err != nil {
|
||||
return fmt.Errorf("enable wal: %w", err)
|
||||
return errors.Wrap(err, "enable wal")
|
||||
}
|
||||
|
||||
// Enable foreign key checks. For historical reasons, SQLite does not check
|
||||
// foreign key constraints by default. There's some overhead on inserts to
|
||||
// verify foreign key integrity, but it's definitely worth it.
|
||||
//if _, err = db.handler.Exec(`PRAGMA foreign_keys = ON;`); err != nil {
|
||||
// return fmt.Errorf("foreign keys pragma: %w", err)
|
||||
// return errors.New("foreign keys pragma: %w", err)
|
||||
//}
|
||||
|
||||
// migrate db
|
||||
|
@ -54,13 +56,13 @@ func (db *DB) migrateSQLite() error {
|
|||
|
||||
var version int
|
||||
if err := db.handler.QueryRow("PRAGMA user_version").Scan(&version); err != nil {
|
||||
return fmt.Errorf("failed to query schema version: %v", err)
|
||||
return errors.Wrap(err, "failed to query schema version")
|
||||
}
|
||||
|
||||
if version == len(sqliteMigrations) {
|
||||
return nil
|
||||
} else if version > len(sqliteMigrations) {
|
||||
return fmt.Errorf("autobrr (version %d) older than schema (version: %d)", len(sqliteMigrations), version)
|
||||
return errors.New("autobrr (version %d) older than schema (version: %d)", len(sqliteMigrations), version)
|
||||
}
|
||||
|
||||
tx, err := db.handler.Begin()
|
||||
|
@ -71,12 +73,12 @@ func (db *DB) migrateSQLite() error {
|
|||
|
||||
if version == 0 {
|
||||
if _, err := tx.Exec(sqliteSchema); err != nil {
|
||||
return fmt.Errorf("failed to initialize schema: %v", err)
|
||||
return errors.Wrap(err, "failed to initialize schema")
|
||||
}
|
||||
} else {
|
||||
for i := version; i < len(sqliteMigrations); i++ {
|
||||
if _, err := tx.Exec(sqliteMigrations[i]); err != nil {
|
||||
return fmt.Errorf("failed to execute migration #%v: %v", i, err)
|
||||
return errors.Wrap(err, "failed to execute migration #%v", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -87,13 +89,13 @@ func (db *DB) migrateSQLite() error {
|
|||
// TODO 2022-01-30 remove this in future version
|
||||
if version == 5 && len(sqliteMigrations) == 6 {
|
||||
if err := customMigrateCopySourcesToMedia(tx); err != nil {
|
||||
return fmt.Errorf("could not run custom data migration: %v", err)
|
||||
return errors.Wrap(err, "could not run custom data migration")
|
||||
}
|
||||
}
|
||||
|
||||
_, err = tx.Exec(fmt.Sprintf("PRAGMA user_version = %d", len(sqliteMigrations)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to bump schema version: %v", err)
|
||||
return errors.Wrap(err, "failed to bump schema version")
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
|
@ -115,7 +117,7 @@ func customMigrateCopySourcesToMedia(tx *sql.Tx) error {
|
|||
OR sources LIKE '%"SACD"%'
|
||||
;`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not run custom data migration: %v", err)
|
||||
return errors.Wrap(err, "could not run custom data migration")
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
|
|
@ -2,18 +2,22 @@ package database
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/internal/logger"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type UserRepo struct {
|
||||
log logger.Logger
|
||||
log zerolog.Logger
|
||||
db *DB
|
||||
}
|
||||
|
||||
func NewUserRepo(log logger.Logger, db *DB) domain.UserRepo {
|
||||
return &UserRepo{
|
||||
log: log,
|
||||
log: log.With().Str("repo", "user").Logger(),
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
@ -23,19 +27,17 @@ func (r *UserRepo) GetUserCount(ctx context.Context) (int, error) {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("user.store: error building query")
|
||||
return 0, err
|
||||
return 0, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
row := r.db.handler.QueryRowContext(ctx, query, args...)
|
||||
if err := row.Err(); err != nil {
|
||||
return 0, err
|
||||
return 0, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
result := 0
|
||||
if err := row.Scan(&result); err != nil {
|
||||
r.log.Error().Err(err).Msg("could not query number of users")
|
||||
return 0, err
|
||||
return 0, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
@ -50,20 +52,18 @@ func (r *UserRepo) FindByUsername(ctx context.Context, username string) (*domain
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("user.store: error building query")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
row := r.db.handler.QueryRowContext(ctx, query, args...)
|
||||
if err := row.Err(); err != nil {
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
var user domain.User
|
||||
|
||||
if err := row.Scan(&user.ID, &user.Username, &user.Password); err != nil {
|
||||
r.log.Error().Err(err).Msg("could not scan user to struct")
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
|
@ -80,14 +80,12 @@ func (r *UserRepo) Store(ctx context.Context, user domain.User) error {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("user.store: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("user.store: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
return err
|
||||
|
@ -105,14 +103,12 @@ func (r *UserRepo) Update(ctx context.Context, user domain.User) error {
|
|||
|
||||
query, args, err := queryBuilder.ToSql()
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("user.store: error building query")
|
||||
return err
|
||||
return errors.Wrap(err, "error building query")
|
||||
}
|
||||
|
||||
_, err = r.db.handler.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
r.log.Error().Stack().Err(err).Msg("user.store: error executing query")
|
||||
return err
|
||||
return errors.Wrap(err, "error executing query")
|
||||
}
|
||||
|
||||
return err
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue