Repository: gogs/gogs
File: cmd/gogs/restore.go (Go)

Code before:
	// Otherwise, it's optional to set config file flag.
	configFile := filepath.Join(archivePath, "custom", "conf", "app.ini")
	var customConf string
	if cmd.IsSet("config") {
		customConf = cmd.String("config")
	} else if !osutil.IsFile(configFile) {
		log.Fatal("'--config' is not specified and custom config file is not found in backup")
	} else {
		customConf = configFile
	}

	err = conf.Init(customConf)
	if err != nil {
		return errors.Wrap(err, "init configuration")
	}
	conf.InitLogging(true)

	conn, err := database.SetEngine()
	if err != nil {
		return errors.Wrap(err, "set engine")
	}

	// Database
	dbDir := path.Join(archivePath, "db")
	if err = database.ImportDatabase(context.Background(), conn, dbDir, cmd.Bool("verbose")); err != nil {
		log.Fatal("Failed to import database: %v", err)
	}

	if !cmd.Bool("database-only") {
		// Custom files
		if osutil.IsDir(conf.CustomDir()) {
			if err = os.Rename(conf.CustomDir(), conf.CustomDir()+".bak"); err != nil {
				log.Fatal("Failed to backup current 'custom': %v", err)
			}
		}
		if err = os.Rename(filepath.Join(archivePath, "custom"), conf.CustomDir()); err != nil {
			log.Fatal("Failed to import 'custom': %v", err)
		}

		// Data files
		_ = os.MkdirAll(conf.Server.AppDataPath, os.ModePerm)
		for _, dir := range []string{"attachments", "avatars", "repo-avatars"} {
			// Skip if backup archive does not have corresponding data
			srcPath := filepath.Join(archivePath, "data", dir)
			if !osutil.IsDir(srcPath) {
				continue
			}

			dirPath := filepath.Join(conf.Server.AppDataPath, dir)
			if osutil.IsDir(dirPath) {
				if err = os.Rename(dirPath, dirPath+".bak"); err != nil {

Review comment (suggestion):
`runRestore` calls `database.ImportDatabase` with `context.Background()` even though the CLI action provides a context. Restores can be lengthy; using the action context allows cancellation/timeout propagation and cleaner shutdown behavior.

Code after:
	// Otherwise, it's optional to set config file flag.
	configFile := filepath.Join(archivePath, "custom", "conf", "app.ini")
	var customConf string
	if lineageConf := configFromLineage(cmd); lineageConf != "" {
		customConf = lineageConf
	} else if !osutil.IsFile(configFile) {
		log.Fatal("'--config' is not specified and custom config file is not found in backup")
	} else {
		customConf = configFile
	}

	err = conf.Init(customConf)
	if err != nil {
		return errors.Wrap(err, "init configuration")
	}
	conf.InitLogging(true)

	conn, err := database.SetEngine()
	if err != nil {
		return errors.Wrap(err, "set engine")
	}

	// Database
	dbDir := path.Join(archivePath, "db")
	if err = database.ImportDatabase(ctx, conn, dbDir, cmd.Bool("verbose")); err != nil {
		log.Fatal("Failed to import database: %v", err)
	}

	if !cmd.Bool("database-only") {
		// Custom files
		if osutil.IsDir(conf.CustomDir()) {
			if err = os.Rename(conf.CustomDir(), conf.CustomDir()+".bak"); err != nil {
				log.Fatal("Failed to backup current 'custom': %v", err)
			}
		}
		if err = os.Rename(filepath.Join(archivePath, "custom"), conf.CustomDir()); err != nil {
			log.Fatal("Failed to import 'custom': %v", err)
		}

		// Data files
		_ = os.MkdirAll(conf.Server.AppDataPath, os.ModePerm)
		for _, dir := range []string{"attachments", "avatars", "repo-avatars"} {
			// Skip if backup archive does not have corresponding data
			srcPath := filepath.Join(archivePath, "data", dir)
			if !osutil.IsDir(srcPath) {
				continue
			}

			dirPath := filepath.Join(conf.Server.AppDataPath, dir)
			if osutil.IsDir(dirPath) {
				if err = os.Rename(dirPath, dirPath+".bak"); err != nil {