Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to convert mysql database from utf8 to utf8mb4 #7144

Merged
merged 10 commits into from
Jun 8, 2019
49 changes: 49 additions & 0 deletions cmd/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package cmd

import (
"fmt"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"

"github.com/urfave/cli"
)

// CmdConvert represents the available convert sub-command.
var CmdConvert = cli.Command{
Name: "convert",
Usage: "Covert the database",
lunny marked this conversation as resolved.
Show resolved Hide resolved
Description: "This is a command for converting the database.",
lunny marked this conversation as resolved.
Show resolved Hide resolved
Action: runConvert,
}

func runConvert(ctx *cli.Context) error {
if err := initDB(); err != nil {
return err
}

log.Trace("AppPath: %s", setting.AppPath)
log.Trace("AppWorkPath: %s", setting.AppWorkPath)
log.Trace("Custom path: %s", setting.CustomPath)
log.Trace("Log path: %s", setting.LogRootPath)
models.LoadConfigs()

if models.DbCfg.Type != "mysql" {
fmt.Println("This command only be used with mysql database")
lunny marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

if err := models.ConvertUtf8ToUtf8mb4(); err != nil {
log.Fatal("Failed to comvert database from utf8 to utf8mb4: %v", err)
lunny marked this conversation as resolved.
Show resolved Hide resolved
return err
}

fmt.Println("Convert successfully, please confirm your database connstr with utf8mb4")
lunny marked this conversation as resolved.
Show resolved Hide resolved

return nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ arguments - which can alternatively be run by running the subcommand web.`
cmd.CmdGenerate,
cmd.CmdMigrate,
cmd.CmdKeys,
cmd.CmdConvert,
}
// Now adjust these commands to add our global configuration options

Expand Down
27 changes: 27 additions & 0 deletions models/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
lunny marked this conversation as resolved.
Show resolved Hide resolved
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models

import "fmt"

// ConvertUtf8ToUtf8mb4 converts database and tables from utf8 to utf8mb4 if it's mysql
func ConvertUtf8ToUtf8mb4() error {
_, err := x.Exec(fmt.Sprintf("ALTER DATABASE %s CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci", DbCfg.Name))
lunny marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

tables, err := x.DBMetas()
if err != nil {
return err
}
for _, table := range tables {
if _, err := x.Exec(fmt.Sprintf("ALTER TABLE `%s` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;", table.Name)); err != nil {
return err
}
}

return nil
}