code.gitea.io/gitea@v1.21.7/cmd/doctor_convert.go (about)

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