@@ -12,9 +12,11 @@ import (
12
12
"code.gitea.io/gitea/models/db"
13
13
git_model "code.gitea.io/gitea/models/git"
14
14
"code.gitea.io/gitea/models/migrations"
15
+ packages_model "code.gitea.io/gitea/models/packages"
15
16
repo_model "code.gitea.io/gitea/models/repo"
16
17
user_model "code.gitea.io/gitea/models/user"
17
18
"code.gitea.io/gitea/modules/log"
19
+ packages_module "code.gitea.io/gitea/modules/packages"
18
20
"code.gitea.io/gitea/modules/setting"
19
21
"code.gitea.io/gitea/modules/storage"
20
22
@@ -25,13 +27,13 @@ import (
25
27
var CmdMigrateStorage = cli.Command {
26
28
Name : "migrate-storage" ,
27
29
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 " ,
29
31
Action : runMigrateStorage ,
30
32
Flags : []cli.Flag {
31
33
cli.StringFlag {
32
34
Name : "type, t" ,
33
35
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' " ,
35
37
},
36
38
cli.StringFlag {
37
39
Name : "storage, s" ,
@@ -80,34 +82,53 @@ var CmdMigrateStorage = cli.Command{
80
82
},
81
83
}
82
84
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 {
85
87
_ , err := storage .Copy (dstStorage , attach .RelativePath (), storage .Attachments , attach .RelativePath ())
86
88
return err
87
89
})
88
90
}
89
91
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 {
92
94
_ , err := storage .Copy (dstStorage , mo .RelativePath (), storage .LFS , mo .RelativePath ())
93
95
return err
94
96
})
95
97
}
96
98
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 {
99
101
_ , err := storage .Copy (dstStorage , user .CustomAvatarRelativePath (), storage .Avatars , user .CustomAvatarRelativePath ())
100
102
return err
101
103
})
102
104
}
103
105
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 {
106
108
_ , err := storage .Copy (dstStorage , repo .CustomAvatarRelativePath (), storage .RepoAvatars , repo .CustomAvatarRelativePath ())
107
109
return err
108
110
})
109
111
}
110
112
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
+
111
132
func runMigrateStorage (ctx * cli.Context ) error {
112
133
stdCtx , cancel := installSignals ()
113
134
defer cancel ()
@@ -127,8 +148,6 @@ func runMigrateStorage(ctx *cli.Context) error {
127
148
return err
128
149
}
129
150
130
- goCtx := context .Background ()
131
-
132
151
if err := storage .Init (); err != nil {
133
152
return err
134
153
}
@@ -145,13 +164,13 @@ func runMigrateStorage(ctx *cli.Context) error {
145
164
return nil
146
165
}
147
166
dstStorage , err = storage .NewLocalStorage (
148
- goCtx ,
167
+ stdCtx ,
149
168
storage.LocalStorageConfig {
150
169
Path : p ,
151
170
})
152
171
case string (storage .MinioStorageType ):
153
172
dstStorage , err = storage .NewMinioStorage (
154
- goCtx ,
173
+ stdCtx ,
155
174
storage.MinioStorageConfig {
156
175
Endpoint : ctx .String ("minio-endpoint" ),
157
176
AccessKeyID : ctx .String ("minio-access-key-id" ),
@@ -162,35 +181,29 @@ func runMigrateStorage(ctx *cli.Context) error {
162
181
UseSSL : ctx .Bool ("minio-use-ssl" ),
163
182
})
164
183
default :
165
- return fmt .Errorf ("Unsupported storage type: %s" , ctx .String ("storage" ))
184
+ return fmt .Errorf ("unsupported storage type: %s" , ctx .String ("storage" ))
166
185
}
167
186
if err != nil {
168
187
return err
169
188
}
170
189
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
+
171
199
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 {
187
202
return err
188
203
}
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
191
206
}
192
207
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" ))
196
209
}
0 commit comments