Skip to content

Commit

Permalink
Add EnsureBlobKey method to metadata repository (#44)
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Shishkin <me@teran.dev>
  • Loading branch information
teran authored Jul 13, 2024
1 parent eb81f14 commit e32777f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions repositories/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ type Repository interface {

CreateBLOB(ctx context.Context, checksum string, size uint64, mimeType string) error
GetBlobKeyByObject(ctx context.Context, container, version, key string) (string, error)
EnsureBlobKey(ctx context.Context, key string, size uint64) error
}
5 changes: 5 additions & 0 deletions repositories/metadata/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ func (m *Mock) GetBlobKeyByObject(_ context.Context, container, version, key str
args := m.Called(container, version, key)
return args.String(0), args.Error(1)
}

func (m *Mock) EnsureBlobKey(_ context.Context, key string, size uint64) error {
args := m.Called(key, size)
return args.Error(0)
}
19 changes: 19 additions & 0 deletions repositories/metadata/postgresql/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,22 @@ func (r *repository) GetBlobKeyByObject(ctx context.Context, container, version,

return checksum, nil
}

func (r *repository) EnsureBlobKey(ctx context.Context, key string, size uint64) error {
row := psql.
Select("id").
From("blobs").
Where(sq.Eq{
"checksum": key,
"size": size,
}).
RunWith(r.db).
QueryRowContext(ctx)

var blobID uint
if err := row.Scan(&blobID); err != nil {
return mapSQLErrors(err)
}

return nil
}
12 changes: 12 additions & 0 deletions repositories/metadata/postgresql/blobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,15 @@ func (s *postgreSQLRepositoryTestSuite) TestGetBlobKeyByObjectErrors() {
s.Require().Error(err)
s.Require().Equal(metadata.ErrNotFound, err)
}

func (s *postgreSQLRepositoryTestSuite) TestEnsureBlobKey() {
err := s.repo.EnsureBlobKey(s.ctx, "deadbeef", 1234)
s.Require().Error(err)
s.Require().Equal(metadata.ErrNotFound, err)

err = s.repo.CreateBLOB(s.ctx, "deadbeef", 1234, "application/octet-stream")
s.Require().NoError(err)

err = s.repo.EnsureBlobKey(s.ctx, "deadbeef", 1234)
s.Require().NoError(err)
}

0 comments on commit e32777f

Please sign in to comment.