Skip to content

Commit f866a38

Browse files
authored
Merge pull request #135 from gofiber/codex/2025-07-14-06-13-25
2 parents 0a88483 + dcde4be commit f866a38

File tree

3 files changed

+98
-16
lines changed

3 files changed

+98
-16
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,10 @@ fiber migrate --to 3.0.0
114114
### Options
115115

116116
```text
117-
-t, --to string Migrate to a specific version e.g:3.0.0 Format: X.Y.Z
118-
-h, --help help for migrate
117+
-t, --to string Migrate to a specific version e.g:3.0.0 Format: X.Y.Z
118+
-f, --force Force migration even if already on the version
119+
-s, --skip_go_mod Skip running go mod tidy, download and vendor
120+
-h, --help help for migrate
119121
```
120122

121123
## fiber upgrade

cmd/migrate.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import (
1212
"github.com/gofiber/cli/cmd/internal/migrations"
1313
)
1414

15-
func newMigrateCmd(currentVersionFile string) *cobra.Command {
15+
func newMigrateCmd() *cobra.Command {
1616
var targetVersionS string
17+
var force bool
18+
var skipGoMod bool
1719

1820
cmd := &cobra.Command{
1921
Use: "migrate",
@@ -29,32 +31,46 @@ func newMigrateCmd(currentVersionFile string) *cobra.Command {
2931
if err := cmd.MarkFlagRequired("to"); err != nil {
3032
panic(err)
3133
}
34+
cmd.Flags().BoolVarP(&force, "force", "f", false, "Force migration even if already on version")
35+
cmd.Flags().BoolVarP(&skipGoMod, "skip_go_mod", "s", false, "Skip running go mod tidy, download and vendor")
3236

3337
cmd.RunE = func(cmd *cobra.Command, _ []string) error {
34-
return migrateRunE(cmd, currentVersionFile, targetVersionS)
38+
return migrateRunE(cmd, MigrateOptions{
39+
CurrentVersionFile: currentVersionFile,
40+
TargetVersionS: targetVersionS,
41+
Force: force,
42+
SkipGoMod: skipGoMod,
43+
})
3544
}
3645

3746
return cmd
3847
}
3948

40-
var migrateCmd = newMigrateCmd("go.mod")
49+
var migrateCmd = newMigrateCmd()
4150

42-
func migrateRunE(cmd *cobra.Command, currentVersionFile, targetVersionS string) error {
43-
currentVersionS, err := currentVersionFromFile(currentVersionFile)
51+
type MigrateOptions struct {
52+
CurrentVersionFile string
53+
TargetVersionS string
54+
Force bool
55+
SkipGoMod bool
56+
}
57+
58+
func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error {
59+
currentVersionS, err := currentVersionFromFile(opts.CurrentVersionFile)
4460
if err != nil {
4561
return fmt.Errorf("current fiber project version not found: %w", err)
4662
}
4763
currentVersionS = strings.TrimPrefix(currentVersionS, "v")
4864
currentVersion := semver.MustParse(currentVersionS)
4965

50-
targetVersionS = strings.TrimPrefix(targetVersionS, "v")
51-
targetVersion, err := semver.NewVersion(targetVersionS)
66+
opts.TargetVersionS = strings.TrimPrefix(opts.TargetVersionS, "v")
67+
targetVersion, err := semver.NewVersion(opts.TargetVersionS)
5268
if err != nil {
53-
return fmt.Errorf("invalid version for \"%s\": %w", targetVersionS, err)
69+
return fmt.Errorf("invalid version for \"%s\": %w", opts.TargetVersionS, err)
5470
}
5571

56-
if !targetVersion.GreaterThan(currentVersion) {
57-
return fmt.Errorf("target version v%s is not greater than current version v%s", targetVersionS, currentVersionS)
72+
if !targetVersion.GreaterThan(currentVersion) && !(opts.Force && targetVersion.Equal(currentVersion)) {
73+
return fmt.Errorf("target version v%s is not greater than current version v%s", opts.TargetVersionS, currentVersionS)
5874
}
5975

6076
wd, err := os.Getwd()
@@ -67,11 +83,13 @@ func migrateRunE(cmd *cobra.Command, currentVersionFile, targetVersionS string)
6783
return fmt.Errorf("migration failed %w", err)
6884
}
6985

70-
if err := runGoMod(wd); err != nil {
71-
return fmt.Errorf("go mod: %w", err)
86+
if !opts.SkipGoMod {
87+
if err := runGoMod(wd); err != nil {
88+
return fmt.Errorf("go mod: %w", err)
89+
}
7290
}
7391

74-
msg := fmt.Sprintf("Migration from Fiber %s to %s", currentVersionS, targetVersionS)
92+
msg := fmt.Sprintf("Migration from Fiber %s to %s", currentVersionS, opts.TargetVersionS)
7593
cmd.Println(termenv.String(msg).
7694
Foreground(termenv.ANSIBrightBlue))
7795

cmd/migrate_test.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func main() {
6969
require.NoError(t, os.Chdir(dir))
7070
defer func() { require.NoError(t, os.Chdir(cwd)) }()
7171

72-
cmd := newMigrateCmd("go.mod")
72+
cmd := newMigrateCmd()
7373
setupCmd()
7474
defer teardownCmd()
7575
out, err := runCobraCmd(cmd, "-t=3.0.0")
@@ -138,3 +138,65 @@ require github.com/gofiber/fiber/v2 v2.0.0`
138138
needError = true
139139
assert.Error(t, runGoMod(dir))
140140
}
141+
142+
func Test_Migrate_ForceAndSkip(t *testing.T) {
143+
dir := t.TempDir()
144+
gomod := `module example
145+
146+
go 1.20
147+
148+
require github.com/gofiber/fiber/v3 v3.0.0
149+
`
150+
require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"), []byte(gomod), 0o600))
151+
require.NoError(t, os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main"), 0o600))
152+
153+
cwd, err := os.Getwd()
154+
require.NoError(t, err)
155+
require.NoError(t, os.Chdir(dir))
156+
defer func() { require.NoError(t, os.Chdir(cwd)) }()
157+
158+
t.Run("without force", func(t *testing.T) {
159+
cmd := newMigrateCmd()
160+
out, err := runCobraCmd(cmd, "-t=3.0.0")
161+
require.Error(t, err)
162+
assert.Contains(t, out, "not greater")
163+
})
164+
165+
t.Run("force", func(t *testing.T) {
166+
origExec := execCommand
167+
var cmds []*exec.Cmd
168+
execCommand = func(name string, args ...string) *exec.Cmd {
169+
cs := append([]string{"-test.run=TestHelperProcess", "--", name}, args...)
170+
cmd := exec.Command(os.Args[0], cs...) // #nosec G204 -- safe for test
171+
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
172+
cmds = append(cmds, cmd)
173+
return cmd
174+
}
175+
defer func() { execCommand = origExec }()
176+
177+
cmd := newMigrateCmd()
178+
out, err := runCobraCmd(cmd, "-t=3.0.0", "-f")
179+
require.NoError(t, err)
180+
assert.Contains(t, out, "Migration from Fiber 3.0.0 to 3.0.0")
181+
assert.Len(t, cmds, 3)
182+
})
183+
184+
t.Run("force skip go mod", func(t *testing.T) {
185+
origExec := execCommand
186+
var cmds []*exec.Cmd
187+
execCommand = func(name string, args ...string) *exec.Cmd {
188+
cs := append([]string{"-test.run=TestHelperProcess", "--", name}, args...)
189+
cmd := exec.Command(os.Args[0], cs...) // #nosec G204 -- safe for test
190+
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
191+
cmds = append(cmds, cmd)
192+
return cmd
193+
}
194+
defer func() { execCommand = origExec }()
195+
196+
cmd := newMigrateCmd()
197+
out, err := runCobraCmd(cmd, "-t=3.0.0", "-f", "-s")
198+
require.NoError(t, err)
199+
assert.Contains(t, out, "Migration from Fiber 3.0.0 to 3.0.0")
200+
assert.Empty(t, cmds)
201+
})
202+
}

0 commit comments

Comments
 (0)