summaryrefslogtreecommitdiffstats
path: root/cmd/doctor.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2020-09-06 22:52:01 +0100
committerGitHub <noreply@github.com>2020-09-06 22:52:01 +0100
commit1b9d5074a7ebb1b470f468cc9195d54915291ee3 (patch)
tree1045623ccc744aedb934017f6bf8ae345131db17 /cmd/doctor.go
parentad2bf376dfd934394cad46c3ff3e022ca232958f (diff)
downloadgitea-1b9d5074a7ebb1b470f468cc9195d54915291ee3.tar.gz
gitea-1b9d5074a7ebb1b470f468cc9195d54915291ee3.zip
Add command to recreate tables (#12407)
Provides new command: `gitea doctor recreate-table` which will recreate db tables and copy the old data in to the new table. This function can be used to remove the old warning of struct defaults being out of date. Fix #8868 Fix #3265 Fix #8894 Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'cmd/doctor.go')
-rw-r--r--cmd/doctor.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/cmd/doctor.go b/cmd/doctor.go
index 2a93db27da..2ca2bb5e70 100644
--- a/cmd/doctor.go
+++ b/cmd/doctor.go
@@ -26,6 +26,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"xorm.io/builder"
+ "xorm.io/xorm"
"github.com/urfave/cli"
)
@@ -62,6 +63,27 @@ var CmdDoctor = cli.Command{
Usage: `Name of the log file (default: "doctor.log"). Set to "-" to output to stdout, set to "" to disable`,
},
},
+ Subcommands: []cli.Command{
+ cmdRecreateTable,
+ },
+}
+
+var cmdRecreateTable = cli.Command{
+ Name: "recreate-table",
+ Usage: "Recreate tables from XORM definitions and copy the data.",
+ ArgsUsage: "[TABLE]... : (TABLEs to recreate - leave blank for all)",
+ Flags: []cli.Flag{
+ cli.BoolFlag{
+ Name: "debug",
+ Usage: "Print SQL commands sent",
+ },
+ },
+ Description: `The database definitions Gitea uses change across versions, sometimes changing default values and leaving old unused columns.
+
+This command will cause Xorm to recreate tables, copying over the data and deleting the old table.
+
+You should back-up your database before doing this and ensure that your database is up-to-date first.`,
+ Action: runRecreateTable,
}
type check struct {
@@ -136,6 +158,47 @@ var checklist = []check{
// more checks please append here
}
+func runRecreateTable(ctx *cli.Context) error {
+ // Redirect the default golog to here
+ golog.SetFlags(0)
+ golog.SetPrefix("")
+ golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT)))
+
+ setting.NewContext()
+ setting.InitDBConfig()
+
+ setting.EnableXORMLog = ctx.Bool("debug")
+ setting.Database.LogSQL = ctx.Bool("debug")
+ setting.Cfg.Section("log").Key("XORM").SetValue(",")
+
+ setting.NewXORMLogService(!ctx.Bool("debug"))
+ if err := models.SetEngine(); err != nil {
+ fmt.Println(err)
+ fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.")
+ return nil
+ }
+
+ args := ctx.Args()
+ names := make([]string, 0, ctx.NArg())
+ for i := 0; i < ctx.NArg(); i++ {
+ names = append(names, args.Get(i))
+ }
+
+ beans, err := models.NamesToBean(names...)
+ if err != nil {
+ return err
+ }
+ recreateTables := migrations.RecreateTables(beans...)
+
+ return models.NewEngine(context.Background(), func(x *xorm.Engine) error {
+ if err := migrations.EnsureUpToDate(x); err != nil {
+ return err
+ }
+ return recreateTables(x)
+ })
+
+}
+
func runDoctor(ctx *cli.Context) error {
// Silence the default loggers