gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "context"
  6. "fmt"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/urfave/cli/v3"
  11. )
  12. // cmdDoctorConvert represents the available convert sub-command.
  13. var cmdDoctorConvert = &cli.Command{
  14. Name: "convert",
  15. Usage: "Convert the database",
  16. Description: "A command to convert an existing MySQL database from utf8 to utf8mb4 or MSSQL database from varchar to nvarchar",
  17. Action: runDoctorConvert,
  18. }
  19. func runDoctorConvert(ctx context.Context, cmd *cli.Command) error {
  20. if err := initDB(ctx); err != nil {
  21. return err
  22. }
  23. log.Info("AppPath: %s", setting.AppPath)
  24. log.Info("AppWorkPath: %s", setting.AppWorkPath)
  25. log.Info("Custom path: %s", setting.CustomPath)
  26. log.Info("Log path: %s", setting.Log.RootPath)
  27. log.Info("Configuration file: %s", setting.CustomConf)
  28. switch {
  29. case setting.Database.Type.IsMySQL():
  30. if err := db.ConvertDatabaseTable(); err != nil {
  31. log.Fatal("Failed to convert database & table: %v", err)
  32. return err
  33. }
  34. fmt.Println("Converted successfully, please confirm your database's character set is now utf8mb4")
  35. case setting.Database.Type.IsMSSQL():
  36. if err := db.ConvertVarcharToNVarchar(); err != nil {
  37. log.Fatal("Failed to convert database from varchar to nvarchar: %v", err)
  38. return err
  39. }
  40. fmt.Println("Converted successfully, please confirm your database's all columns character is NVARCHAR now")
  41. default:
  42. fmt.Println("This command can only be used with a MySQL or MSSQL database")
  43. }
  44. return nil
  45. }