db-migrator.go

Форк
0
115 строк · 2.6 Кб
1
/**
2
 * This file is part of the raoptimus/db-migrator.go library
3
 *
4
 * @copyright Copyright (c) Evgeniy Urvantsev
5
 * @license https://github.com/raoptimus/db-migrator.go/blob/master/LICENSE.md
6
 * @link https://github.com/raoptimus/db-migrator.go
7
 */
8

9
package action
10

11
import (
12
	"fmt"
13

14
	"github.com/raoptimus/db-migrator.go/internal/args"
15
	"github.com/urfave/cli/v2"
16
)
17

18
const (
19
	defaultUpgradeLimit = 0
20
)
21

22
type Upgrade struct {
23
	console         Console
24
	service         MigrationService
25
	fileNameBuilder FileNameBuilder
26
	interactive     bool
27
}
28

29
func NewUpgrade(
30
	console Console,
31
	service MigrationService,
32
	fileNameBuilder FileNameBuilder,
33
	interactive bool,
34
) *Upgrade {
35
	return &Upgrade{
36
		console:         console,
37
		service:         service,
38
		fileNameBuilder: fileNameBuilder,
39
		interactive:     interactive,
40
	}
41
}
42

43
func (u *Upgrade) Run(ctx *cli.Context) error {
44
	limit, err := args.ParseStepStringOrDefault(ctx.Args().Get(0), defaultUpgradeLimit)
45
	if err != nil {
46
		return err
47
	}
48

49
	migrations, err := u.service.NewMigrations(ctx.Context)
50
	if err != nil {
51
		return err
52
	}
53

54
	totalNewMigrations := migrations.Len()
55
	if totalNewMigrations == 0 {
56
		u.console.SuccessLn(noNewMigrationsFound)
57
		return nil
58
	}
59

60
	if limit > 0 && migrations.Len() > limit {
61
		migrations = migrations[:limit]
62
	}
63

64
	if migrations.Len() == totalNewMigrations {
65
		u.console.Warnf(
66
			"Total %d new %s to be applied: \n",
67
			migrations.Len(),
68
			u.console.NumberPlural(migrations.Len(), "migration", "migrations"),
69
		)
70
	} else {
71
		u.console.Warnf(
72
			"Total %d out of %d new %s to be applied: \n",
73
			migrations.Len(),
74
			totalNewMigrations,
75
			u.console.NumberPlural(totalNewMigrations, "migration", "migrations"),
76
		)
77
	}
78

79
	printMigrations(migrations, false)
80

81
	question := fmt.Sprintf("Apply the above %s?",
82
		u.console.NumberPlural(migrations.Len(), "migration", "migrations"),
83
	)
84
	if u.interactive && !u.console.Confirm(question) {
85
		return nil
86
	}
87

88
	var applied int
89
	for i := range migrations {
90
		migration := &migrations[i]
91
		fileName, safely := u.fileNameBuilder.Up(migration.Version, false)
92

93
		if err := u.service.ApplyFile(ctx.Context, migration, fileName, safely); err != nil {
94
			u.console.Errorf("%d from %d %s applied.\n",
95
				applied,
96
				migrations.Len(),
97
				u.console.NumberPlural(applied, migrationWas, migrationsWere),
98
			)
99
			u.console.Error("The rest of the migrations are canceled.\n")
100

101
			return err
102
		}
103

104
		applied++
105
	}
106

107
	u.console.Successf(
108
		"%d %s applied\n",
109
		migrations.Len(),
110
		u.console.NumberPlural(migrations.Len(), migrationWas, migrationsWere),
111
	)
112
	u.console.SuccessLn(migratedUpSuccessfully)
113

114
	return nil
115
}
116

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.