From b5e7a0b033f8d6ee0bc34adc3fef0f9d254659ec Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 6 Jun 2019 12:52:12 +0800 Subject: [PATCH 1/8] add command to convert mysql database from utf8 to utf8mb4 --- cmd/convert.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + models/convert.go | 27 ++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 cmd/convert.go create mode 100644 models/convert.go diff --git a/cmd/convert.go b/cmd/convert.go new file mode 100644 index 0000000000000..b2072c8de8d74 --- /dev/null +++ b/cmd/convert.go @@ -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", + Description: "This is a command for converting the database.", + 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") + return nil + } + + if err := models.ConvertUtf8ToUtf8mb4(); err != nil { + log.Fatal("Failed to comvert database from utf8 to utf8mb4: %v", err) + return err + } + + fmt.Println("Convert successfully, please confirm your database connstr with utf8mb4") + + return nil +} diff --git a/main.go b/main.go index d8c37fee2f097..79c9b01114b05 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/models/convert.go b/models/convert.go new file mode 100644 index 0000000000000..81165a144c6bf --- /dev/null +++ b/models/convert.go @@ -0,0 +1,27 @@ +// Copyright 2017 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 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)) + 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 +} From 034dbcc88e475974ec3ddbb4d39b032870937fa8 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 8 Jun 2019 15:33:57 +0800 Subject: [PATCH 2/8] Update cmd/convert.go Co-Authored-By: John Olheiser <42128690+jolheiser@users.noreply.github.com> --- cmd/convert.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/convert.go b/cmd/convert.go index b2072c8de8d74..6e243548f3ac1 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -17,7 +17,7 @@ import ( // CmdConvert represents the available convert sub-command. var CmdConvert = cli.Command{ Name: "convert", - Usage: "Covert the database", + Usage: "Convert the database", Description: "This is a command for converting the database.", Action: runConvert, } From 8da5d79d319686452ab4b96c9dfd2ce06dc0b4a1 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 8 Jun 2019 15:34:23 +0800 Subject: [PATCH 3/8] Update cmd/convert.go Co-Authored-By: John Olheiser <42128690+jolheiser@users.noreply.github.com> --- cmd/convert.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/convert.go b/cmd/convert.go index 6e243548f3ac1..35a2686e70f97 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -18,7 +18,7 @@ import ( var CmdConvert = cli.Command{ Name: "convert", Usage: "Convert the database", - Description: "This is a command for converting the database.", + Description: "A command to convert an existing MySQL database from utf8 to utf8mb4", Action: runConvert, } From 9246c8233e3be02792e1ac51d37305da4d11b17f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 8 Jun 2019 15:34:47 +0800 Subject: [PATCH 4/8] Update cmd/convert.go Co-Authored-By: John Olheiser <42128690+jolheiser@users.noreply.github.com> --- cmd/convert.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/convert.go b/cmd/convert.go index 35a2686e70f97..85ff459d74853 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -34,7 +34,7 @@ func runConvert(ctx *cli.Context) error { models.LoadConfigs() if models.DbCfg.Type != "mysql" { - fmt.Println("This command only be used with mysql database") + fmt.Println("This command can only be used with a MySQL database") return nil } From d683e72df57fa5715d5c7c44e58f1b63a989a05c Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 8 Jun 2019 15:35:08 +0800 Subject: [PATCH 5/8] Update models/convert.go Co-Authored-By: John Olheiser <42128690+jolheiser@users.noreply.github.com> --- models/convert.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/convert.go b/models/convert.go index 81165a144c6bf..9e2ef7f97c244 100644 --- a/models/convert.go +++ b/models/convert.go @@ -8,7 +8,7 @@ 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)) + _, err := x.Exec(fmt.Sprintf("ALTER DATABASE `%s` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci", DbCfg.Name)) if err != nil { return err } From f1b9b1c6fe8a56eebc033a10320a77de264135b1 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 8 Jun 2019 15:35:22 +0800 Subject: [PATCH 6/8] Update models/convert.go Co-Authored-By: John Olheiser <42128690+jolheiser@users.noreply.github.com> --- models/convert.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/convert.go b/models/convert.go index 9e2ef7f97c244..c34be973c6b53 100644 --- a/models/convert.go +++ b/models/convert.go @@ -1,4 +1,4 @@ -// Copyright 2017 The Gitea Authors. All rights reserved. +// 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. From c684acdde32451bbc7dc6f4d7106967e3089bdb2 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 8 Jun 2019 15:35:36 +0800 Subject: [PATCH 7/8] Update cmd/convert.go Co-Authored-By: John Olheiser <42128690+jolheiser@users.noreply.github.com> --- cmd/convert.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/convert.go b/cmd/convert.go index 85ff459d74853..ff53dc1fe48ba 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -39,7 +39,7 @@ func runConvert(ctx *cli.Context) error { } if err := models.ConvertUtf8ToUtf8mb4(); err != nil { - log.Fatal("Failed to comvert database from utf8 to utf8mb4: %v", err) + log.Fatal("Failed to convert database from utf8 to utf8mb4: %v", err) return err } From 62d324df218c91b3df66386a70ba662c6de3240e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 8 Jun 2019 15:35:46 +0800 Subject: [PATCH 8/8] Update cmd/convert.go Co-Authored-By: John Olheiser <42128690+jolheiser@users.noreply.github.com> --- cmd/convert.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/convert.go b/cmd/convert.go index ff53dc1fe48ba..cb0510c722fc8 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -43,7 +43,7 @@ func runConvert(ctx *cli.Context) error { return err } - fmt.Println("Convert successfully, please confirm your database connstr with utf8mb4") + fmt.Println("Converted successfully, please confirm your database's character set is now utf8mb4") return nil }