Skip to content

HMS-5790: cannot change size of upload #1102

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

Merged
merged 1 commit into from
May 13, 2025
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
2 changes: 1 addition & 1 deletion db/migrations.latest
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20250128083413
20250429141338
5 changes: 5 additions & 0 deletions db/migrations/20250429141338_add_size_to_uploads.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BEGIN;

ALTER TABLE uploads DROP COLUMN IF EXISTS size;

COMMIT;
5 changes: 5 additions & 0 deletions db/migrations/20250429141338_add_size_to_uploads.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BEGIN;

ALTER TABLE uploads ADD COLUMN IF NOT EXISTS size int DEFAULT 0 NOT NULL;

COMMIT;
4 changes: 2 additions & 2 deletions pkg/dao/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ type TemplateDao interface {
}

type UploadDao interface {
StoreFileUpload(ctx context.Context, orgID string, uploadUUID string, sha256 string, chunkSize int64) error
StoreFileUpload(ctx context.Context, orgID string, uploadUUID string, sha256 string, chunkSize int64, uploadSize int64) error
StoreChunkUpload(ctx context.Context, orgID string, uploadUUID string, sha256 string) error
GetExistingUploadIDAndCompletedChunks(ctx context.Context, orgID string, sha256 string, chunkSize int64) (string, []string, error)
GetExistingUploadIDAndCompletedChunks(ctx context.Context, orgID string, sha256 string, chunkSize int64, uploadSize int64) (string, []string, error)
DeleteUpload(ctx context.Context, uploadUUID string) error
ListUploadsForCleanup(ctx context.Context) ([]models.Upload, error)
}
11 changes: 8 additions & 3 deletions pkg/dao/uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ func GetUploadDao(db *gorm.DB, pulpClient pulp_client.PulpClient) UploadDao {
}
}

func (t uploadDaoImpl) StoreFileUpload(ctx context.Context, orgID string, uploadUUID string, sha256 string, chunkSize int64) error {
func (t uploadDaoImpl) StoreFileUpload(ctx context.Context, orgID string, uploadUUID string, sha256 string, chunkSize int64, uploadSize int64) error {
var upload models.Upload

upload.OrgID = orgID
upload.UploadUUID = uploadUUID
upload.Sha256 = sha256
upload.ChunkSize = chunkSize
upload.Size = uploadSize
upload.ChunkList = []string{}

db := t.db.Model(models.Upload{}).WithContext(ctx).Create(&upload)
Expand All @@ -37,12 +38,16 @@ func (t uploadDaoImpl) StoreFileUpload(ctx context.Context, orgID string, upload
return nil
}

func (t uploadDaoImpl) GetExistingUploadIDAndCompletedChunks(ctx context.Context, orgID string, sha256 string, chunkSize int64) (string, []string, error) {
func (t uploadDaoImpl) GetExistingUploadIDAndCompletedChunks(ctx context.Context, orgID string, sha256 string, chunkSize int64, uploadSize int64) (string, []string, error) {
db := t.db.Model(models.Upload{}).WithContext(ctx)

var result models.Upload

db.Where("org_id = ?", orgID).Where("chunk_size = ?", chunkSize).Where("sha256 = ?", sha256).First(&result)
db.Where("org_id = ?", orgID).
Where("chunk_size = ?", chunkSize).
Where("sha256 = ?", sha256).
Where("size = ?", uploadSize).
First(&result)

if db.Error != nil {
return "", []string{}, db.Error
Expand Down
23 changes: 15 additions & 8 deletions pkg/dao/uploads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,31 @@ func (s *UploadsSuite) SetupTest() {
func (s *UploadsSuite) TestStoreFileUpload() {
uploadDao := s.uploadsDao()
ctx := context.Background()
uploadUUID := "bananaUUID"
chunkSize := int64(16000)
uploadSize := int64(16000)
uploadSha := "bananaHash256"
chunkSha := "bananaChunkHash256"

err := uploadDao.StoreFileUpload(ctx, "bananaOrg", "bananaUUID", "bananaHash256", 16000)
err := uploadDao.StoreFileUpload(ctx, orgIDTest, uploadUUID, uploadSha, chunkSize, uploadSize)

assert.Equal(s.T(), err, nil)

uploadUUID, chunkList, err := uploadDao.GetExistingUploadIDAndCompletedChunks(ctx, "bananaOrg", "bananaHash256", 16000)
existingUploadUUID, chunkList, err := uploadDao.GetExistingUploadIDAndCompletedChunks(ctx, orgIDTest, uploadSha, chunkSize, uploadSize)

assert.Equal(s.T(), nil, err)
assert.Equal(s.T(), "bananaUUID", uploadUUID)
assert.Equal(s.T(), uploadUUID, existingUploadUUID)
assert.Equal(s.T(), []string{}, chunkList)

err = uploadDao.StoreChunkUpload(ctx, "bananaOrg", "bananaUUID", "bananaChunkHash256")
err = uploadDao.StoreChunkUpload(ctx, orgIDTest, uploadUUID, chunkSha)

assert.Equal(s.T(), nil, err)

uploadUUID, chunkList, err = uploadDao.GetExistingUploadIDAndCompletedChunks(ctx, "bananaOrg", "bananaHash256", 16000)
existingUploadUUID, chunkList, err = uploadDao.GetExistingUploadIDAndCompletedChunks(ctx, orgIDTest, uploadSha, chunkSize, uploadSize)

assert.Equal(s.T(), nil, err)
assert.Equal(s.T(), "bananaUUID", uploadUUID)
assert.Equal(s.T(), []string{"bananaChunkHash256"}, chunkList)
assert.Equal(s.T(), uploadUUID, existingUploadUUID)
assert.Equal(s.T(), []string{chunkSha}, chunkList)
}

func (s *UploadsSuite) TestDeleteUpload() {
Expand All @@ -66,7 +71,7 @@ func (s *UploadsSuite) TestDeleteUpload() {
uploadUUID := uuid.New()
var found models.Upload

err := uploadDao.StoreFileUpload(ctx, orgIDTest, uploadUUID.String(), "test-sha", 500)
err := uploadDao.StoreFileUpload(ctx, orgIDTest, uploadUUID.String(), "test-sha", 500, 500)
require.NoError(s.T(), err)

err = uploadDao.DeleteUpload(ctx, uploadUUID.String())
Expand All @@ -89,6 +94,7 @@ func (s *UploadsSuite) TestListUploadsForCleanup() {
UploadUUID: uuid.NewString(),
OrgID: orgIDTest,
ChunkSize: int64(1),
Size: int64(1),
Sha256: uuid.NewString(),
ChunkList: []string{uuid.NewString()},
}
Expand All @@ -101,6 +107,7 @@ func (s *UploadsSuite) TestListUploadsForCleanup() {
UploadUUID: uuid.NewString(),
OrgID: orgIDTest,
ChunkSize: int64(1),
Size: int64(1),
Sha256: uuid.NewString(),
ChunkList: []string{uuid.NewString()},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/pulp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (ph *PulpHandler) createUploadInternal(c echo.Context, request api.CreateUp
}

// Associate the file uploaduuid for later use
err = ph.DaoRegistry.Uploads.StoreFileUpload(c.Request().Context(), orgId, uploadUuid, request.Sha256, request.ChunkSize)
err = ph.DaoRegistry.Uploads.StoreFileUpload(c.Request().Context(), orgId, uploadUuid, request.Sha256, request.ChunkSize, request.Size)

if err != nil {
return nil, err
Expand Down
6 changes: 5 additions & 1 deletion pkg/handler/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,10 @@ func (rh *RepositoryHandler) createUpload(c echo.Context) error {
return ce.NewErrorResponse(http.StatusBadRequest, "Error creating upload", "Chunk size must be greater than 0")
}

if req.Size <= 0 {
return ce.NewErrorResponse(http.StatusBadRequest, "Error creating upload", "Size must be greater than 0")
}

domainName, err := ph.DaoRegistry.Domain.FetchOrCreateDomain(c.Request().Context(), orgId)

if err != nil {
Expand All @@ -621,7 +625,7 @@ func (rh *RepositoryHandler) createUpload(c echo.Context) error {
return err
}

existingUUID, completedChunks, err := ph.DaoRegistry.Uploads.GetExistingUploadIDAndCompletedChunks(c.Request().Context(), orgId, req.Sha256, req.ChunkSize)
existingUUID, completedChunks, err := ph.DaoRegistry.Uploads.GetExistingUploadIDAndCompletedChunks(c.Request().Context(), orgId, req.Sha256, req.ChunkSize, req.Size)
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/models/uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Upload struct {
CreatedAt time.Time
OrgID string
ChunkSize int64
Size int64
Sha256 string
ChunkList pq.StringArray `gorm:"type:text[]"`
}
Expand Down Expand Up @@ -41,6 +42,11 @@ func (t *Upload) validate() error {
return err
}

if t.Size == 0 {
err = Error{Message: "Size cannot be 0.", Validation: true}
return err
}

if t.Sha256 == "" {
err = Error{Message: "Sha256 cannot be blank.", Validation: true}
return err
Expand Down
Loading