autobrr/internal/database/database.go
kenstir 4009554d10
feat(filters): skip duplicates (#1711)
* feat(filters): skip duplicates

* fix: add interface instead of any

* fix(filters): tonullint

* feat(filters): skip dupes check month day

* chore: cleanup

* feat(db): set autoincrement id

* feat(filters): add repack and proper to dupe profile

* feat(filters): add default dupe profiles

* feat(duplicates): check audio and website

* feat(duplicates): update tests

* feat(duplicates): add toggles on addform

* feat(duplicates): fix sqlite upgrade path and initialize duplicate profiles

* feat(duplicates): simplify sqlite upgrade

avoiding temp table and unwieldy select.  Besides, FK constraints
are turned off anyway in #229.

* feat(duplicates): change CheckIsDuplicateRelease treatment of PROPER and REPACK

"Proper" and "Repack" are not parallel to the other conditions like "Title",
so they do not belong as dedup conditions.  "PROPER" means there was an issue in
the previous release, and so a PROPER is never a duplicate, even if it replaces
another PROPER.  Similarly, "REPACK" means there was an issue in the previous
release by that group, and so it is a duplicate only if we previously took a
release from a DIFFERENT group.

I have not removed Proper and Repack from the UI or the schema yet.

* feat(duplicates): update postgres schema to match sqlite

* feat(duplicates): fix web build errors

* feat(duplicates): fix postgres errors

* feat(filters): do leftjoin for duplicate profile

* fix(filters): partial update dupe profile

* go fmt `internal/domain/filter.go`

* feat(duplicates): restore straightforward logic for proper/repack

* feat(duplicates): remove mostly duplicate TV duplicate profiles

Having one profile seems the cleanest.  If somebody wants multiple
resolutions then they can add Resolution to the duplicate profile.
Tested this profile with both weekly episodic releases and daily
show releases.

* feat(release): add db indexes and sub_title

* feat(release): add IsDuplicate tests

* feat(release): update action handler

* feat(release): add more tests for skip duplicates

* feat(duplicates): check audio

* feat(duplicates): add more tests

* feat(duplicates): match edition cut and more

* fix(duplicates): tests

* fix(duplicates): missing imports

* fix(duplicates): tests

* feat(duplicates): handle sub_title edition and language in ui

* fix(duplicates): tests

* feat(duplicates): check name against normalized hash

* fix(duplicates): tests

* chore: update .gitignore to ignore .pnpm-store

* fix: tests

* fix(filters): tests

* fix: bad conflict merge

* fix: update release type in test

* fix: use vendored hot-toast

* fix: release_test.go

* fix: rss_test.go

* feat(duplicates): improve title hashing for unique check

* feat(duplicates): further improve title hashing for unique check with lang

* feat(duplicates): fix tests

* feat(duplicates): add macros IsDuplicate and DuplicateProfile ID and name

* feat(duplicates): add normalized hash match option

* fix: headlessui-state prop warning

* fix(duplicates): add missing year in daily ep normalize

* fix(duplicates): check rejections len

---------

Co-authored-by: ze0s <ze0s@riseup.net>
2024-12-25 22:33:46 +01:00

141 lines
3.1 KiB
Go

// Copyright (c) 2021 - 2024, 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
cfg *domain.Config
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(),
cfg: cfg,
}
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
}
// 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) sq.Sqlizer {
//if databaseDriver == "sqlite" {
if db.Driver == "sqlite" {
return sq.Like{col: val}
}
return sq.ILike{col: val}
}