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

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