@@ -12,9 +12,11 @@ import (
1212 "code.gitea.io/gitea/models/db"
1313 git_model "code.gitea.io/gitea/models/git"
1414 "code.gitea.io/gitea/models/migrations"
15+ packages_model "code.gitea.io/gitea/models/packages"
1516 repo_model "code.gitea.io/gitea/models/repo"
1617 user_model "code.gitea.io/gitea/models/user"
1718 "code.gitea.io/gitea/modules/log"
19+ packages_module "code.gitea.io/gitea/modules/packages"
1820 "code.gitea.io/gitea/modules/setting"
1921 "code.gitea.io/gitea/modules/storage"
2022
@@ -25,13 +27,13 @@ import (
2527var CmdMigrateStorage = cli.Command {
2628 Name : "migrate-storage" ,
2729 Usage : "Migrate the storage" ,
28- Description : "This is a command for migrating storage. " ,
30+ Description : "Copies stored files from storage configured in app.ini to parameter-configured storage " ,
2931 Action : runMigrateStorage ,
3032 Flags : []cli.Flag {
3133 cli.StringFlag {
3234 Name : "type, t" ,
3335 Value : "" ,
34- Usage : "Kinds of files to migrate, currently only 'attachments' is supported " ,
36+ Usage : "Type of stored files to copy. Allowed types: 'attachments', 'lfs', 'avatars', 'repo-avatars', 'repo-archivers', 'packages' " ,
3537 },
3638 cli.StringFlag {
3739 Name : "storage, s" ,
@@ -80,34 +82,53 @@ var CmdMigrateStorage = cli.Command{
8082 },
8183}
8284
83- func migrateAttachments (dstStorage storage.ObjectStorage ) error {
84- return repo_model . IterateAttachment ( func (attach * repo_model.Attachment ) error {
85+ func migrateAttachments (ctx context. Context , dstStorage storage.ObjectStorage ) error {
86+ return db . IterateObjects ( ctx , func (attach * repo_model.Attachment ) error {
8587 _ , err := storage .Copy (dstStorage , attach .RelativePath (), storage .Attachments , attach .RelativePath ())
8688 return err
8789 })
8890}
8991
90- func migrateLFS (dstStorage storage.ObjectStorage ) error {
91- return git_model . IterateLFS ( func (mo * git_model.LFSMetaObject ) error {
92+ func migrateLFS (ctx context. Context , dstStorage storage.ObjectStorage ) error {
93+ return db . IterateObjects ( ctx , func (mo * git_model.LFSMetaObject ) error {
9294 _ , err := storage .Copy (dstStorage , mo .RelativePath (), storage .LFS , mo .RelativePath ())
9395 return err
9496 })
9597}
9698
97- func migrateAvatars (dstStorage storage.ObjectStorage ) error {
98- return user_model . IterateUser ( func (user * user_model.User ) error {
99+ func migrateAvatars (ctx context. Context , dstStorage storage.ObjectStorage ) error {
100+ return db . IterateObjects ( ctx , func (user * user_model.User ) error {
99101 _ , err := storage .Copy (dstStorage , user .CustomAvatarRelativePath (), storage .Avatars , user .CustomAvatarRelativePath ())
100102 return err
101103 })
102104}
103105
104- func migrateRepoAvatars (dstStorage storage.ObjectStorage ) error {
105- return repo_model . IterateRepository ( func (repo * repo_model.Repository ) error {
106+ func migrateRepoAvatars (ctx context. Context , dstStorage storage.ObjectStorage ) error {
107+ return db . IterateObjects ( ctx , func (repo * repo_model.Repository ) error {
106108 _ , err := storage .Copy (dstStorage , repo .CustomAvatarRelativePath (), storage .RepoAvatars , repo .CustomAvatarRelativePath ())
107109 return err
108110 })
109111}
110112
113+ func migrateRepoArchivers (ctx context.Context , dstStorage storage.ObjectStorage ) error {
114+ return db .IterateObjects (ctx , func (archiver * repo_model.RepoArchiver ) error {
115+ p , err := archiver .RelativePath ()
116+ if err != nil {
117+ return err
118+ }
119+ _ , err = storage .Copy (dstStorage , p , storage .RepoArchives , p )
120+ return err
121+ })
122+ }
123+
124+ func migratePackages (ctx context.Context , dstStorage storage.ObjectStorage ) error {
125+ return db .IterateObjects (ctx , func (pb * packages_model.PackageBlob ) error {
126+ p := packages_module .KeyToRelativePath (packages_module .BlobHash256Key (pb .HashSHA256 ))
127+ _ , err := storage .Copy (dstStorage , p , storage .Packages , p )
128+ return err
129+ })
130+ }
131+
111132func runMigrateStorage (ctx * cli.Context ) error {
112133 stdCtx , cancel := installSignals ()
113134 defer cancel ()
@@ -127,8 +148,6 @@ func runMigrateStorage(ctx *cli.Context) error {
127148 return err
128149 }
129150
130- goCtx := context .Background ()
131-
132151 if err := storage .Init (); err != nil {
133152 return err
134153 }
@@ -145,13 +164,13 @@ func runMigrateStorage(ctx *cli.Context) error {
145164 return nil
146165 }
147166 dstStorage , err = storage .NewLocalStorage (
148- goCtx ,
167+ stdCtx ,
149168 storage.LocalStorageConfig {
150169 Path : p ,
151170 })
152171 case string (storage .MinioStorageType ):
153172 dstStorage , err = storage .NewMinioStorage (
154- goCtx ,
173+ stdCtx ,
155174 storage.MinioStorageConfig {
156175 Endpoint : ctx .String ("minio-endpoint" ),
157176 AccessKeyID : ctx .String ("minio-access-key-id" ),
@@ -162,35 +181,29 @@ func runMigrateStorage(ctx *cli.Context) error {
162181 UseSSL : ctx .Bool ("minio-use-ssl" ),
163182 })
164183 default :
165- return fmt .Errorf ("Unsupported storage type: %s" , ctx .String ("storage" ))
184+ return fmt .Errorf ("unsupported storage type: %s" , ctx .String ("storage" ))
166185 }
167186 if err != nil {
168187 return err
169188 }
170189
190+ migratedMethods := map [string ]func (context.Context , storage.ObjectStorage ) error {
191+ "attachments" : migrateAttachments ,
192+ "lfs" : migrateLFS ,
193+ "avatars" : migrateAvatars ,
194+ "repo-avatars" : migrateRepoAvatars ,
195+ "repo-archivers" : migrateRepoArchivers ,
196+ "packages" : migratePackages ,
197+ }
198+
171199 tp := strings .ToLower (ctx .String ("type" ))
172- switch tp {
173- case "attachments" :
174- if err := migrateAttachments (dstStorage ); err != nil {
175- return err
176- }
177- case "lfs" :
178- if err := migrateLFS (dstStorage ); err != nil {
179- return err
180- }
181- case "avatars" :
182- if err := migrateAvatars (dstStorage ); err != nil {
183- return err
184- }
185- case "repo-avatars" :
186- if err := migrateRepoAvatars (dstStorage ); err != nil {
200+ if m , ok := migratedMethods [tp ]; ok {
201+ if err := m (stdCtx , dstStorage ); err != nil {
187202 return err
188203 }
189- default :
190- return fmt . Errorf ( "Unsupported storage: %s" , ctx . String ( "type" ))
204+ log . Info ( "%s files have successfully been copied to the new storage." , tp )
205+ return nil
191206 }
192207
193- log .Warn ("All files have been copied to the new placement but old files are still on the original placement." )
194-
195- return nil
208+ return fmt .Errorf ("unsupported storage: %s" , ctx .String ("type" ))
196209}
0 commit comments