|
| 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 | +} |
0 commit comments