-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migrate repo archiver and packages storage support on command line (
#20757) (#20806) * Add migrate repo archiver and packages storage support on command line (#20757) * Add migrate repo archiver and packages storage support on command line * Fix typo * Use stdCtx * Use packageblob and fix command description * Add migrate packages unit tests * Fix comment year * Fix the migrate storage command line description * Update cmd/migrate_storage.go Co-authored-by: zeripath <art27@cantab.net> * Update cmd/migrate_storage.go Co-authored-by: zeripath <art27@cantab.net> * Update cmd/migrate_storage.go Co-authored-by: zeripath <art27@cantab.net> * Fix test Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net> * bug fix Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
- Loading branch information
1 parent
b43d7e1
commit 7a9b01a
Showing
11 changed files
with
187 additions
and
134 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// 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 cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models/unittest" | ||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
func init() { | ||
setting.SetCustomPathAndConf("", "", "") | ||
setting.LoadForTest() | ||
} | ||
|
||
func TestMain(m *testing.M) { | ||
unittest.MainTest(m, &unittest.TestOptions{ | ||
GiteaRootPath: "..", | ||
}) | ||
} |
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,74 @@ | ||
// 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 cmd | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models/packages" | ||
"code.gitea.io/gitea/models/unittest" | ||
user_model "code.gitea.io/gitea/models/user" | ||
packages_module "code.gitea.io/gitea/modules/packages" | ||
"code.gitea.io/gitea/modules/storage" | ||
packages_service "code.gitea.io/gitea/services/packages" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMigratePackages(t *testing.T) { | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
|
||
creator := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) | ||
|
||
content := "package main\n\nfunc main() {\nfmt.Println(\"hi\")\n}\n" | ||
buf, err := packages_module.CreateHashedBufferFromReader(strings.NewReader(content), 1024) | ||
assert.NoError(t, err) | ||
defer buf.Close() | ||
|
||
v, f, err := packages_service.CreatePackageAndAddFile(&packages_service.PackageCreationInfo{ | ||
PackageInfo: packages_service.PackageInfo{ | ||
Owner: creator, | ||
PackageType: packages.TypeGeneric, | ||
Name: "test", | ||
Version: "1.0.0", | ||
}, | ||
Creator: creator, | ||
SemverCompatible: true, | ||
VersionProperties: map[string]string{}, | ||
}, &packages_service.PackageFileCreationInfo{ | ||
PackageFileInfo: packages_service.PackageFileInfo{ | ||
Filename: "a.go", | ||
}, | ||
Data: buf, | ||
IsLead: true, | ||
}) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, v) | ||
assert.NotNil(t, f) | ||
|
||
ctx := context.Background() | ||
|
||
p, err := os.MkdirTemp(os.TempDir(), "migrated_packages") | ||
assert.NoError(t, err) | ||
|
||
dstStorage, err := storage.NewLocalStorage( | ||
ctx, | ||
storage.LocalStorageConfig{ | ||
Path: p, | ||
}) | ||
assert.NoError(t, err) | ||
|
||
err = migratePackages(ctx, dstStorage) | ||
assert.NoError(t, err) | ||
|
||
entries, err := os.ReadDir(p) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, 2, len(entries)) | ||
assert.EqualValues(t, "01", entries[0].Name()) | ||
assert.EqualValues(t, "tmp", entries[1].Name()) | ||
} |
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,34 @@ | ||
// 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 db | ||
|
||
import ( | ||
"context" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
// IterateObjects iterate all the Bean object | ||
func IterateObjects[Object any](ctx context.Context, f func(repo *Object) error) error { | ||
var start int | ||
batchSize := setting.Database.IterateBufferSize | ||
sess := GetEngine(ctx) | ||
for { | ||
repos := make([]*Object, 0, batchSize) | ||
if err := sess.Limit(batchSize, start).Find(&repos); err != nil { | ||
return err | ||
} | ||
if len(repos) == 0 { | ||
return nil | ||
} | ||
start += len(repos) | ||
|
||
for _, repo := range repos { | ||
if err := f(repo); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.