Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(imports): Weird fix around import errors. #80

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions pkg/dataaccess/db_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/Jacobbrewer1/puppet-summary/pkg/entities"
"github.com/Jacobbrewer1/puppet-summary/pkg/logging"
sqlite "github.com/mattn/go-sqlite3"
_ "github.com/mattn/go-sqlite3"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -468,9 +468,7 @@ func (s *sqliteImpl) SaveRun(ctx context.Context, run *entities.PuppetReport) er
run.Skipped,
)
// If the error is that the hash already exists, then we can ignore it.
sqlErr := new(sqlite.Error)
ok := errors.As(err, sqlErr)
if ok && sqlErr.Code == 19 { // 19 is SQLITE_CONSTRAINT violation.
if err != nil && err.Error() == "UNIQUE constraint failed: reports.hash" { // I don't like this, but we get weird import errors if we use errors.Is to do with the sqlite3 driver.
slog.Warn("Hash already exists, ignoring", slog.String(logging.KeyHash, run.ID))
return ErrDuplicate
} else if err != nil {
Expand Down
10 changes: 3 additions & 7 deletions pkg/dataaccess/db_sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package dataaccess
import (
"context"
"database/sql"
"errors"
"regexp"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/Jacobbrewer1/puppet-summary/pkg/entities"
"github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -656,11 +656,7 @@ func (s *sqliteSuite) TestSaveRunDuplicate() {
s.mockDB.ExpectExec(expSql).
WithArgs("hash", "fqdn", "PRODUCTION", "CHANGED", "reports/PRODUCTION/fqdn/2024-02-21T10:20:53Z.yaml",
now.Format(time.DateTime), "10s", 1, 2, 3, 0).
WillReturnError(sqlite3.Error{
Code: 19,
ExtendedCode: 2067,
SystemErrno: 0,
})
WillReturnError(errors.New("UNIQUE constraint failed: reports.hash"))

s.mockDB.ExpectClose()

Expand All @@ -676,5 +672,5 @@ func (s *sqliteSuite) TestSaveRunDuplicate() {
Changed: 2,
Total: 3,
})
s.Require().EqualError(err, ErrDuplicate.Error())
s.Require().Equal(ErrDuplicate, err)
}
Loading