mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
Fix: Performance issues and sqlite locking (#74)
* fix: performance issues and sqlite locking * fix: dashboard release stats was reversed * refactor: open and migrate db * chore: cleanup
This commit is contained in:
parent
d8c37dde2f
commit
f466657ed4
25 changed files with 362 additions and 658 deletions
|
@ -2,25 +2,26 @@ package database
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
)
|
||||
|
||||
type UserRepo struct {
|
||||
db *sql.DB
|
||||
db *SqliteDB
|
||||
}
|
||||
|
||||
func NewUserRepo(db *sql.DB) domain.UserRepo {
|
||||
func NewUserRepo(db *SqliteDB) domain.UserRepo {
|
||||
return &UserRepo{db: db}
|
||||
}
|
||||
|
||||
func (r *UserRepo) FindByUsername(ctx context.Context, username string) (*domain.User, error) {
|
||||
//r.db.lock.RLock()
|
||||
//defer r.db.lock.RUnlock()
|
||||
|
||||
query := `SELECT id, username, password FROM users WHERE username = ?`
|
||||
|
||||
row := r.db.QueryRowContext(ctx, query, username)
|
||||
row := r.db.handler.QueryRowContext(ctx, query, username)
|
||||
if err := row.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -36,11 +37,13 @@ func (r *UserRepo) FindByUsername(ctx context.Context, username string) (*domain
|
|||
}
|
||||
|
||||
func (r *UserRepo) Store(ctx context.Context, user domain.User) error {
|
||||
//r.db.lock.RLock()
|
||||
//defer r.db.lock.RUnlock()
|
||||
|
||||
var err error
|
||||
if user.ID != 0 {
|
||||
update := `UPDATE users SET password = ? WHERE username = ?`
|
||||
_, err = r.db.ExecContext(ctx, update, user.Password, user.Username)
|
||||
_, err = r.db.handler.ExecContext(ctx, update, user.Password, user.Username)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("error executing query")
|
||||
return err
|
||||
|
@ -48,7 +51,7 @@ func (r *UserRepo) Store(ctx context.Context, user domain.User) error {
|
|||
|
||||
} else {
|
||||
query := `INSERT INTO users (username, password) VALUES (?, ?)`
|
||||
_, err = r.db.ExecContext(ctx, query, user.Username, user.Password)
|
||||
_, err = r.db.handler.ExecContext(ctx, query, user.Username, user.Password)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("error executing query")
|
||||
return err
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue