Skip to content

Commit ac80551

Browse files
Add more repo sources support (#264)
* Add more repo sources support * Optimize code * Fix test
1 parent 07a54be commit ac80551

File tree

16 files changed

+429
-0
lines changed

16 files changed

+429
-0
lines changed

_mocks/opencsg.com/csghub-server/builder/store/database/mock_RepoStore.go

Lines changed: 156 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
SET statement_timeout = 0;
2+
3+
--bun:split
4+
5+
ALTER TABLE repositories DROP COLUMN IF EXISTS csg_path;
6+
7+
--bun:split
8+
9+
ALTER TABLE repositories DROP COLUMN IF EXISTS hf_path;
10+
11+
--bun:split
12+
13+
ALTER TABLE repositories DROP COLUMN IF EXISTS ms_path;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
SET statement_timeout = 0;
2+
3+
--bun:split
4+
5+
ALTER TABLE repositories ADD COLUMN IF NOT EXISTS csg_path VARCHAR;
6+
7+
--bun:split
8+
9+
ALTER TABLE repositories ADD COLUMN IF NOT EXISTS hf_path VARCHAR;
10+
11+
--bun:split
12+
13+
ALTER TABLE repositories ADD COLUMN IF NOT EXISTS ms_path VARCHAR;

builder/store/database/repository.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/uptrace/bun"
1313
"opencsg.com/csghub-server/common/types"
14+
"opencsg.com/csghub-server/common/types/enum"
1415
)
1516

1617
var RepositorySourceAndPrefixMapping = map[types.RepositorySource]string{
@@ -61,6 +62,9 @@ type RepoStore interface {
6162
FindWithBatch(ctx context.Context, batchSize, batch int, repoTypes ...types.RepositoryType) ([]Repository, error)
6263
ByUser(ctx context.Context, userID int64) ([]Repository, error)
6364
FindByRepoSourceWithBatch(ctx context.Context, repoSource types.RepositorySource, batchSize, batch int) ([]Repository, error)
65+
UpdateSourcePath(ctx context.Context, repoID int64, sourcePath, sourceType string) error
66+
FindMirrorReposWithBatch(ctx context.Context, batchSize, batch int) ([]Repository, error)
67+
BulkUpdateSourcePath(ctx context.Context, repos []*Repository) error
6468
}
6569

6670
func NewRepoStore() RepoStore {
@@ -103,6 +107,9 @@ type Repository struct {
103107
Source types.RepositorySource `bun:",nullzero,default:'local'" json:"source"`
104108
SyncStatus types.RepositorySyncStatus `bun:",nullzero" json:"sync_status"`
105109
SensitiveCheckStatus types.SensitiveCheckStatus `bun:",default:0" json:"sensitive_check_status"`
110+
MSPath string `bun:",nullzero" json:"ms_path"`
111+
CSGPath string `bun:",nullzero" json:"csg_path"`
112+
HFPath string `bun:",nullzero" json:"hf_path"`
106113
// updated_at timestamp will be updated only if files changed
107114
times
108115
}
@@ -113,6 +120,17 @@ func (r Repository) NamespaceAndName() (namespace string, name string) {
113120
return fields[0], fields[1]
114121
}
115122

123+
func (r *Repository) UpdateSourceBySourceTypeAndSourcePath(sourceType, sourcePath string) {
124+
switch sourceType {
125+
case enum.HFSource:
126+
r.HFPath = sourcePath
127+
case enum.MSSource:
128+
r.MSPath = sourcePath
129+
case enum.CSGSource:
130+
r.CSGPath = sourcePath
131+
}
132+
}
133+
116134
type RepositoryTag struct {
117135
ID int64 `bun:",pk,autoincrement" json:"id"`
118136
RepositoryID int64 `bun:",notnull" json:"repository_id"`
@@ -723,3 +741,49 @@ func (s *repoStoreImpl) ByUser(ctx context.Context, userID int64) ([]Repository,
723741
err := s.db.Operator.Core.NewSelect().Model(&repos).Where("user_id = ?", userID).Scan(ctx)
724742
return repos, err
725743
}
744+
745+
func (s *repoStoreImpl) FindMirrorReposWithBatch(ctx context.Context, batchSize, batch int) ([]Repository, error) {
746+
var res []Repository
747+
err := s.db.Operator.Core.NewSelect().
748+
Model(&res).
749+
Relation("Mirror").
750+
Where("mirror.id is not null").
751+
Order("id desc").
752+
Limit(batchSize).
753+
Offset(batchSize * (batch - 1)).
754+
Scan(ctx)
755+
return res, err
756+
}
757+
758+
func (s *repoStoreImpl) UpdateSourcePath(ctx context.Context, repoID int64, sourcePath, sourceType string) error {
759+
var field string
760+
switch sourceType {
761+
case enum.CSGSource:
762+
field = "csg_path"
763+
case enum.HFSource:
764+
field = "hf_path"
765+
case enum.MSSource:
766+
field = "ms_path"
767+
default:
768+
return fmt.Errorf("unknown source type: %s", sourceType)
769+
}
770+
771+
_, err := s.db.Operator.Core.NewUpdate().
772+
Model(&Repository{}).
773+
Set(field+" = ?", sourcePath).
774+
Where("id = ?", repoID).
775+
Exec(ctx)
776+
if err != nil {
777+
return err
778+
}
779+
return nil
780+
}
781+
782+
func (s *repoStoreImpl) BulkUpdateSourcePath(ctx context.Context, repos []*Repository) error {
783+
_, err := s.db.Operator.Core.NewUpdate().
784+
Model(&repos).
785+
Column("csg_path", "hf_path", "ms_path").
786+
Bulk().
787+
Exec(ctx)
788+
return err
789+
}

0 commit comments

Comments
 (0)