You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

migrate_storage.go 1.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package cmd
  5. import (
  6. "context"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/models/migrations"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/storage"
  12. "github.com/urfave/cli"
  13. )
  14. // CmdMigrateStorage represents the available migrate storage sub-command.
  15. var CmdMigrateStorage = cli.Command{
  16. Name: "migrate-storage",
  17. Usage: "Migrate the storage",
  18. Description: "This is a command for migrating storage.",
  19. Action: runMigrateStorage,
  20. }
  21. func migrateAttachments(dstStorage storage.ObjectStorage) error {
  22. return models.IterateAttachment(func(attach *models.Attachment) error {
  23. _, err := storage.Copy(dstStorage, attach.UUID, storage.Attachments, attach.RelativePath())
  24. return err
  25. })
  26. }
  27. func runMigrateStorage(ctx *cli.Context) error {
  28. if err := initDB(); err != nil {
  29. return err
  30. }
  31. log.Trace("AppPath: %s", setting.AppPath)
  32. log.Trace("AppWorkPath: %s", setting.AppWorkPath)
  33. log.Trace("Custom path: %s", setting.CustomPath)
  34. log.Trace("Log path: %s", setting.LogRootPath)
  35. setting.InitDBConfig()
  36. if err := models.NewEngine(context.Background(), migrations.Migrate); err != nil {
  37. log.Fatal("Failed to initialize ORM engine: %v", err)
  38. return err
  39. }
  40. tp := ctx.String("type")
  41. // TODO: init setting
  42. if err := storage.Init(); err != nil {
  43. return err
  44. }
  45. switch tp {
  46. case "attachments":
  47. dstStorage, err := storage.NewLocalStorage(ctx.String("dst"))
  48. if err != nil {
  49. return err
  50. }
  51. return migrateAttachments(dstStorage)
  52. }
  53. return nil
  54. }