2019-06-08 15:53:45 +02:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-06-08 15:53:45 +02:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-09-19 13:49:59 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2019-06-08 15:53:45 +02:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2023-07-21 11:28:19 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
2019-06-08 15:53:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// CmdConvert represents the available convert sub-command.
|
2023-07-21 11:28:19 +02:00
|
|
|
var CmdConvert = &cli.Command{
|
2019-06-08 15:53:45 +02:00
|
|
|
Name: "convert",
|
|
|
|
Usage: "Convert the database",
|
2023-04-17 15:22:10 +02:00
|
|
|
Description: "A command to convert an existing MySQL database from utf8 to utf8mb4 or MSSQL database from varchar to nvarchar",
|
2019-06-08 15:53:45 +02:00
|
|
|
Action: runConvert,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runConvert(ctx *cli.Context) error {
|
2021-11-07 04:11:27 +01:00
|
|
|
stdCtx, cancel := installSignals()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := initDB(stdCtx); err != nil {
|
2019-06-08 15:53:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-27 02:56:58 +02:00
|
|
|
log.Info("AppPath: %s", setting.AppPath)
|
|
|
|
log.Info("AppWorkPath: %s", setting.AppWorkPath)
|
|
|
|
log.Info("Custom path: %s", setting.CustomPath)
|
2023-02-19 17:12:01 +01:00
|
|
|
log.Info("Log path: %s", setting.Log.RootPath)
|
2021-09-14 03:24:57 +02:00
|
|
|
log.Info("Configuration file: %s", setting.CustomConf)
|
2019-06-08 15:53:45 +02:00
|
|
|
|
2023-04-17 15:22:10 +02:00
|
|
|
switch {
|
|
|
|
case setting.Database.Type.IsMySQL():
|
|
|
|
if err := db.ConvertUtf8ToUtf8mb4(); err != nil {
|
|
|
|
log.Fatal("Failed to convert database from utf8 to utf8mb4: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println("Converted successfully, please confirm your database's character set is now utf8mb4")
|
|
|
|
case setting.Database.Type.IsMSSQL():
|
|
|
|
if err := db.ConvertVarcharToNVarchar(); err != nil {
|
|
|
|
log.Fatal("Failed to convert database from varchar to nvarchar: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println("Converted successfully, please confirm your database's all columns character is NVARCHAR now")
|
|
|
|
default:
|
|
|
|
fmt.Println("This command can only be used with a MySQL or MSSQL database")
|
2019-06-08 15:53:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|