Skip to content

Commit

Permalink
fix: Fixing scheduler pattern matching and clean of old versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ViBiOh committed Feb 7, 2021
1 parent 7ff5676 commit a4ea86f
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 2 deletions.
7 changes: 6 additions & 1 deletion pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,17 @@ func (a app) checkRepositoryVersion(repo model.Repository) []model.Release {
continue
}

compiledPattern, err := semver.ParsePattern(pattern)
if err != nil {
logger.Error("unable to parse pattern: %s", err)
}

repositoryVersion, err := semver.Parse(repositoryVersionName)
if err != nil {
continue
}

if !version.IsGreater(repositoryVersion) {
if !compiledPattern.Check(version) || !version.IsGreater(repositoryVersion) {
continue
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/service/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ func (a app) Update(ctx context.Context, item model.Repository) error {
func (a app) Clean(ctx context.Context) error {
return a.repositoryStore.DoAtomic(ctx, func(ctx context.Context) error {
if err := a.repositoryStore.DeleteUnused(ctx); err != nil {
return httpModel.WrapInternal(fmt.Errorf("unable to delete: %w", err))
return httpModel.WrapInternal(fmt.Errorf("unable to delete unused repository: %w", err))
}

if err := a.repositoryStore.DeleteUnusedVersions(ctx); err != nil {
return httpModel.WrapInternal(fmt.Errorf("unable to delete unused repository versions: %w", err))
}

return nil
Expand Down
8 changes: 8 additions & 0 deletions pkg/service/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,14 @@ func TestClean(t *testing.T) {
},
httpModel.ErrInternalError,
},
{
"error versions",
New(repositorytest.New().SetDeleteUnusedVersions(errors.New("failed")), nil, nil),
args{
ctx: context.Background(),
},
httpModel.ErrInternalError,
},
{
"success",
New(repositorytest.New(), nil, nil),
Expand Down
18 changes: 18 additions & 0 deletions pkg/store/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type App interface {
Create(ctx context.Context, o model.Repository) (uint64, error)
UpdateVersions(ctx context.Context, o model.Repository) error
DeleteUnused(ctx context.Context) error
DeleteUnusedVersions(ctx context.Context) error
}

type app struct {
Expand Down Expand Up @@ -233,3 +234,20 @@ WHERE
func (a app) DeleteUnused(ctx context.Context) error {
return db.Exec(ctx, deleteQuery)
}

const deleteVersionsQuery = `
DELETE FROM
ketchup.repository_version r
WHERE NOT EXISTS (
SELECT
FROM
ketchup.ketchup k
WHERE
r.repository_id = k.repository_id
AND r.pattern = k.pattern
)
`

func (a app) DeleteUnusedVersions(ctx context.Context) error {
return db.Exec(ctx, deleteVersionsQuery)
}
48 changes: 48 additions & 0 deletions pkg/store/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,51 @@ func TestDeleteUnused(t *testing.T) {
})
}
}

func TestDeleteUnusedVersions(t *testing.T) {
var cases = []struct {
intention string
wantErr error
}{
{
"simple",
nil,
},
}

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
mockDb, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("unable to create mock database: %s", err)
}
defer mockDb.Close()

ctx := context.Background()
mock.ExpectBegin()
tx, err := mockDb.Begin()
if err != nil {
t.Errorf("unable to create tx: %s", err)
}
ctx = db.StoreTx(ctx, tx)

mock.ExpectExec("DELETE FROM ketchup.repository_version r WHERE NOT EXISTS").WillReturnResult(sqlmock.NewResult(0, 1))

gotErr := New(mockDb).DeleteUnusedVersions(ctx)

failed := false

if !errors.Is(gotErr, tc.wantErr) {
failed = true
}

if failed {
t.Errorf("DeleteUnusedVersions() = `%s`, want `%s`", gotErr, tc.wantErr)
}

if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("sqlmock unfilled expectations: %s", err)
}
})
}
}
14 changes: 14 additions & 0 deletions pkg/store/repository/repositorytest/repositorytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type App struct {
updateVersionsErr error

deleteUnusedErr error

deleteUnusedVersionsErr error
}

// New creates mock instance
Expand Down Expand Up @@ -101,6 +103,13 @@ func (a *App) SetDeleteUnused(err error) *App {
return a
}

// SetDeleteUnusedVersions mocks
func (a *App) SetDeleteUnusedVersions(err error) *App {
a.deleteUnusedVersionsErr = err

return a
}

// DoAtomic mocks
func (a *App) DoAtomic(ctx context.Context, action func(context.Context) error) error {
if ctx == context.TODO() {
Expand Down Expand Up @@ -144,3 +153,8 @@ func (a *App) UpdateVersions(_ context.Context, o model.Repository) error {
func (a *App) DeleteUnused(_ context.Context) error {
return a.deleteUnusedErr
}

// DeleteUnusedVersions mocks
func (a *App) DeleteUnusedVersions(_ context.Context) error {
return a.deleteUnusedVersionsErr
}

0 comments on commit a4ea86f

Please sign in to comment.