From 64900c4d56d3872bb5b90d46ac31d76c661ef14c Mon Sep 17 00:00:00 2001 From: ze0s <43699394+zze0s@users.noreply.github.com> Date: Mon, 1 May 2023 01:18:46 +0200 Subject: [PATCH] fix(downloadclients): remove from filter actions on delete (#891) fix(downloadclients): properly delete from filter actions --- internal/database/action.go | 12 +++++-- internal/database/download_client.go | 51 ++++++++++++++++++++++++++-- internal/domain/action.go | 2 +- 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/internal/database/action.go b/internal/database/action.go index 7220b3a..fc17163 100644 --- a/internal/database/action.go +++ b/internal/database/action.go @@ -41,12 +41,15 @@ func (r *ActionRepo) FindByFilterID(ctx context.Context, filterID int) ([]*domai } for _, action := range actions { - if action.ClientID != 0 { + if action.ClientID > 0 { client, err := r.attachDownloadClient(ctx, tx, action.ClientID) if err != nil { return nil, err } - action.Client = *client + + if client != nil { + action.Client = client + } } } @@ -180,6 +183,10 @@ func (r *ActionRepo) attachDownloadClient(ctx context.Context, tx *Tx, clientID 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 { + if err == sql.ErrNoRows { + r.log.Warn().Msgf("no download client with id %d", clientID) + return nil, nil + } return nil, errors.Wrap(err, "error scanning row") } @@ -608,7 +615,6 @@ func (r *ActionRepo) StoreFilterActions(ctx context.Context, actions []*domain.A if err := tx.Commit(); err != nil { return nil, errors.Wrap(err, "error updating filter actions") - } return actions, nil diff --git a/internal/database/download_client.go b/internal/database/download_client.go index fbf24ab..8bf15b4 100644 --- a/internal/database/download_client.go +++ b/internal/database/download_client.go @@ -254,6 +254,31 @@ func (r *DownloadClientRepo) Update(ctx context.Context, client domain.DownloadC } func (r *DownloadClientRepo) Delete(ctx context.Context, clientID int) error { + tx, err := r.db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelWriteCommitted}) + if err != nil { + return err + } + + defer tx.Rollback() + + if err := r.delete(ctx, tx, clientID); err != nil { + return errors.Wrap(err, "error deleting download client: %d", clientID) + } + + if err := r.deleteClientFromAction(ctx, tx, clientID); err != nil { + return errors.Wrap(err, "error deleting download client: %d", clientID) + } + + if err := tx.Commit(); err != nil { + return errors.Wrap(err, "error deleting download client: %d", clientID) + } + + r.log.Info().Msgf("delete download client: %d", clientID) + + return nil +} + +func (r *DownloadClientRepo) delete(ctx context.Context, tx *Tx, clientID int) error { queryBuilder := r.db.squirrel. Delete("client"). Where(sq.Eq{"id": clientID}) @@ -263,7 +288,7 @@ func (r *DownloadClientRepo) Delete(ctx context.Context, clientID int) error { return errors.Wrap(err, "error building query") } - res, err := r.db.handler.ExecContext(ctx, query, args...) + res, err := tx.ExecContext(ctx, query, args...) if err != nil { return errors.Wrap(err, "error executing query") } @@ -276,7 +301,29 @@ func (r *DownloadClientRepo) Delete(ctx context.Context, clientID int) error { return errors.New("no rows affected") } - r.log.Info().Msgf("delete download client: %d", clientID) + r.log.Debug().Msgf("delete download client: %d", clientID) + + return nil +} + +func (r *DownloadClientRepo) deleteClientFromAction(ctx context.Context, tx *Tx, clientID int) error { + var err error + + queryBuilder := r.db.squirrel. + Update("action"). + Set("enabled", false). + Set("client_id", 0). + Where(sq.Eq{"client_id": clientID}). + Suffix("RETURNING filter_id").RunWith(tx) + + // return values + var filterID int + + if err = queryBuilder.QueryRowContext(ctx).Scan(&filterID); err != nil { + return errors.Wrap(err, "error executing query") + } + + r.log.Debug().Msgf("deleting download client %d from action for filter %d", clientID, filterID) return nil } diff --git a/internal/domain/action.go b/internal/domain/action.go index 5570597..9a1c8ef 100644 --- a/internal/domain/action.go +++ b/internal/domain/action.go @@ -49,7 +49,7 @@ type Action struct { WebhookHeaders []string `json:"webhook_headers,omitempty"` FilterID int `json:"filter_id,omitempty"` ClientID int32 `json:"client_id,omitempty"` - Client DownloadClient `json:"client,omitempty"` + Client *DownloadClient `json:"client,omitempty"` } // ParseMacros parse all macros on action