Skip to content

Commit 23a2ee3

Browse files
lunnyjolheiser
andauthored
Add command to convert mysql database from utf8 to utf8mb4 (#7144)
* add command to convert mysql database from utf8 to utf8mb4 * Update cmd/convert.go Co-Authored-By: John Olheiser <[email protected]> * Update cmd/convert.go Co-Authored-By: John Olheiser <[email protected]> * Update cmd/convert.go Co-Authored-By: John Olheiser <[email protected]> * Update models/convert.go Co-Authored-By: John Olheiser <[email protected]> * Update models/convert.go Co-Authored-By: John Olheiser <[email protected]> * Update cmd/convert.go Co-Authored-By: John Olheiser <[email protected]> * Update cmd/convert.go Co-Authored-By: John Olheiser <[email protected]>
1 parent 6fb31a5 commit 23a2ee3

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

cmd/convert.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package cmd
6+
7+
import (
8+
"fmt"
9+
10+
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/modules/log"
12+
"code.gitea.io/gitea/modules/setting"
13+
14+
"github.com/urfave/cli"
15+
)
16+
17+
// CmdConvert represents the available convert sub-command.
18+
var CmdConvert = cli.Command{
19+
Name: "convert",
20+
Usage: "Convert the database",
21+
Description: "A command to convert an existing MySQL database from utf8 to utf8mb4",
22+
Action: runConvert,
23+
}
24+
25+
func runConvert(ctx *cli.Context) error {
26+
if err := initDB(); err != nil {
27+
return err
28+
}
29+
30+
log.Trace("AppPath: %s", setting.AppPath)
31+
log.Trace("AppWorkPath: %s", setting.AppWorkPath)
32+
log.Trace("Custom path: %s", setting.CustomPath)
33+
log.Trace("Log path: %s", setting.LogRootPath)
34+
models.LoadConfigs()
35+
36+
if models.DbCfg.Type != "mysql" {
37+
fmt.Println("This command can only be used with a MySQL database")
38+
return nil
39+
}
40+
41+
if err := models.ConvertUtf8ToUtf8mb4(); err != nil {
42+
log.Fatal("Failed to convert database from utf8 to utf8mb4: %v", err)
43+
return err
44+
}
45+
46+
fmt.Println("Converted successfully, please confirm your database's character set is now utf8mb4")
47+
48+
return nil
49+
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ arguments - which can alternatively be run by running the subcommand web.`
6767
cmd.CmdGenerate,
6868
cmd.CmdMigrate,
6969
cmd.CmdKeys,
70+
cmd.CmdConvert,
7071
}
7172
// Now adjust these commands to add our global configuration options
7273

models/convert.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package models
6+
7+
import "fmt"
8+
9+
// ConvertUtf8ToUtf8mb4 converts database and tables from utf8 to utf8mb4 if it's mysql
10+
func ConvertUtf8ToUtf8mb4() error {
11+
_, err := x.Exec(fmt.Sprintf("ALTER DATABASE `%s` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci", DbCfg.Name))
12+
if err != nil {
13+
return err
14+
}
15+
16+
tables, err := x.DBMetas()
17+
if err != nil {
18+
return err
19+
}
20+
for _, table := range tables {
21+
if _, err := x.Exec(fmt.Sprintf("ALTER TABLE `%s` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;", table.Name)); err != nil {
22+
return err
23+
}
24+
}
25+
26+
return nil
27+
}

0 commit comments

Comments
 (0)