Merge pull request #8853 from wy65701436/fix-pb-dao

fix sql in remove blob from project
This commit is contained in:
Wang Yan 2019-08-28 11:09:19 +08:00 committed by GitHub
commit 6045a69457
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 2 deletions

View File

@ -64,9 +64,9 @@ func RemoveBlobsFromProject(projectID int64, blobs ...*models.Blob) error {
return nil
}
sql := fmt.Sprintf(`DELETE FROM project_blob WHERE blob_id IN (%s)`, ParamPlaceholderForIn(len(blobIDs)))
sql := fmt.Sprintf(`DELETE FROM project_blob WHERE project_id = ? AND blob_id IN (%s)`, ParamPlaceholderForIn(len(blobIDs)))
_, err := GetOrmer().Raw(sql, blobIDs).Exec()
_, err := GetOrmer().Raw(sql, projectID, blobIDs).Exec()
return err
}

View File

@ -24,6 +24,23 @@ import (
"github.com/stretchr/testify/require"
)
func TestAddBlobToProject(t *testing.T) {
bbID, err := AddBlob(&models.Blob{
Digest: "TestAddBlobToProject_blob1",
Size: 101,
})
require.Nil(t, err)
pid, err := AddProject(models.Project{
Name: "TestAddBlobToProject_project1",
OwnerID: 1,
})
require.Nil(t, err)
_, err = AddBlobToProject(bbID, pid)
require.Nil(t, err)
}
func TestHasBlobInProject(t *testing.T) {
_, blob, err := GetOrCreateBlob(&models.Blob{
Digest: digest.FromString(utils.GenerateRandomString()).String(),
@ -102,6 +119,51 @@ func TestCountSizeOfProject(t *testing.T) {
assert.Equal(t, pSize, int64(606))
}
func TestRemoveBlobsFromProject(t *testing.T) {
var blobs1 []*models.Blob
var blobsRm []*models.Blob
bb1 := &models.Blob{
Digest: "TestRemoveBlobsFromProject_blob1",
Size: 101,
}
bb2 := &models.Blob{
Digest: "TestRemoveBlobsFromProject_blob2",
Size: 101,
}
bb3 := &models.Blob{
Digest: "TestRemoveBlobsFromProject_blob3",
Size: 101,
}
_, err := AddBlob(bb1)
require.Nil(t, err)
_, err = AddBlob(bb2)
require.Nil(t, err)
_, err = AddBlob(bb3)
require.Nil(t, err)
blobs1 = append(blobs1, bb1)
blobs1 = append(blobs1, bb2)
blobs1 = append(blobs1, bb3)
blobsRm = append(blobsRm, bb1)
blobsRm = append(blobsRm, bb2)
pid, err := AddProject(models.Project{
Name: "TestRemoveBlobsFromProject_project1",
OwnerID: 1,
})
require.Nil(t, err)
AddBlobsToProject(pid, blobs1...)
err = RemoveBlobsFromProject(pid, blobsRm...)
require.Nil(t, err)
has, err := HasBlobInProject(pid, bb1.Digest)
require.Nil(t, err)
assert.False(t, has)
has, err = HasBlobInProject(pid, bb3.Digest)
require.Nil(t, err)
assert.True(t, has)
}
func TestCountSizeOfProjectDupdigest(t *testing.T) {
_, err := AddBlob(&models.Blob{
Digest: "CountSizeOfProject_blob11",