forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete unreferenced packages when deleting a package version (go-gite…
…a#20977) (go-gitea#21060) Backport go-gitea#20977 Delete a package if its last version got deleted. Otherwise removing the owner works only after the clean up job ran. Fix go-gitea#20969 Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
- Loading branch information
Showing
2 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package packages_test | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
packages_model "code.gitea.io/gitea/models/packages" | ||
"code.gitea.io/gitea/models/unittest" | ||
user_model "code.gitea.io/gitea/models/user" | ||
|
||
_ "code.gitea.io/gitea/models" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
unittest.MainTest(m, &unittest.TestOptions{ | ||
GiteaRootPath: filepath.Join("..", ".."), | ||
}) | ||
} | ||
|
||
func TestHasOwnerPackages(t *testing.T) { | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
|
||
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) | ||
|
||
p, err := packages_model.TryInsertPackage(db.DefaultContext, &packages_model.Package{ | ||
OwnerID: owner.ID, | ||
LowerName: "package", | ||
}) | ||
assert.NotNil(t, p) | ||
assert.NoError(t, err) | ||
|
||
// A package without package versions gets automatically cleaned up and should return false | ||
has, err := packages_model.HasOwnerPackages(db.DefaultContext, owner.ID) | ||
assert.False(t, has) | ||
assert.NoError(t, err) | ||
|
||
pv, err := packages_model.GetOrInsertVersion(db.DefaultContext, &packages_model.PackageVersion{ | ||
PackageID: p.ID, | ||
LowerVersion: "internal", | ||
IsInternal: true, | ||
}) | ||
assert.NotNil(t, pv) | ||
assert.NoError(t, err) | ||
|
||
// A package with an internal package version gets automaticaly cleaned up and should return false | ||
has, err = packages_model.HasOwnerPackages(db.DefaultContext, owner.ID) | ||
assert.False(t, has) | ||
assert.NoError(t, err) | ||
|
||
pv, err = packages_model.GetOrInsertVersion(db.DefaultContext, &packages_model.PackageVersion{ | ||
PackageID: p.ID, | ||
LowerVersion: "normal", | ||
IsInternal: false, | ||
}) | ||
assert.NotNil(t, pv) | ||
assert.NoError(t, err) | ||
|
||
// A package with a normal package version should return true | ||
has, err = packages_model.HasOwnerPackages(db.DefaultContext, owner.ID) | ||
assert.True(t, has) | ||
assert.NoError(t, err) | ||
} |