@@ -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
1617var 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
6670func 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+
116134type 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