Skip to content

Commit 7edb930

Browse files
rstefan1techknowlogick
authored andcommitted
Add command for migrating database (#4954)
1 parent 6eff62a commit 7edb930

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

Diff for: cmd/migrate.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2018 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+
"code.gitea.io/gitea/models"
9+
"code.gitea.io/gitea/models/migrations"
10+
"code.gitea.io/gitea/modules/log"
11+
"code.gitea.io/gitea/modules/setting"
12+
13+
"github.com/urfave/cli"
14+
)
15+
16+
// CmdMigrate represents the available migrate sub-command.
17+
var CmdMigrate = cli.Command{
18+
Name: "migrate",
19+
Usage: "Migrate the database",
20+
Description: "This is a command for migrating the database, so that you can run gitea admin create-user before starting the server.",
21+
Action: runMigrate,
22+
Flags: []cli.Flag{
23+
cli.StringFlag{
24+
Name: "config, c",
25+
Value: "custom/conf/app.ini",
26+
Usage: "Custom configuration file path",
27+
},
28+
},
29+
}
30+
31+
func runMigrate(ctx *cli.Context) error {
32+
if ctx.IsSet("config") {
33+
setting.CustomConf = ctx.String("config")
34+
}
35+
36+
if err := initDB(); err != nil {
37+
return err
38+
}
39+
40+
log.Trace("AppPath: %s", setting.AppPath)
41+
log.Trace("AppWorkPath: %s", setting.AppWorkPath)
42+
log.Trace("Custom path: %s", setting.CustomPath)
43+
log.Trace("Log path: %s", setting.LogRootPath)
44+
models.LoadConfigs()
45+
46+
if err := models.NewEngine(migrations.Migrate); err != nil {
47+
log.Fatal(4, "Failed to initialize ORM engine: %v", err)
48+
return err
49+
}
50+
51+
return nil
52+
}

Diff for: main.go

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ arguments - which can alternatively be run by running the subcommand web.`
4747
cmd.CmdCert,
4848
cmd.CmdAdmin,
4949
cmd.CmdGenerate,
50+
cmd.CmdMigrate,
5051
}
5152
app.Flags = append(app.Flags, []cli.Flag{}...)
5253
app.Action = cmd.CmdWeb.Action

0 commit comments

Comments
 (0)