Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ['1.23', '1.24', '1.25']
go-version: ['1.25', '1.26']

steps:
- name: Checkout code
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ go.work.sum
.vscode/
.serena/
.claude/
references/
15 changes: 7 additions & 8 deletions examples/basic/go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/akfaiz/migris/examples/basic

go 1.23.0
go 1.25.0

require (
github.com/akfaiz/migris v0.0.0
github.com/jackc/pgx/v5 v5.7.5
github.com/jackc/pgx/v5 v5.8.0
github.com/joho/godotenv v1.5.1
)

Expand All @@ -16,14 +16,13 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/pressly/goose/v3 v3.26.0 // indirect
github.com/pressly/goose/v3 v3.27.0 // indirect
github.com/sethvargo/go-retry v0.3.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.40.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sys v0.34.0 // indirect
golang.org/x/term v0.33.0 // indirect
golang.org/x/text v0.28.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.45.0 // indirect
golang.org/x/term v0.43.0 // indirect
golang.org/x/text v0.37.0 // indirect
)

replace github.com/akfaiz/migris => ../..
176 changes: 90 additions & 86 deletions examples/basic/go.sum

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions extra/migriscli/example/go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/akfaiz/migris/extra/migriscli/example

go 1.23.0
go 1.25.0

require (
github.com/akfaiz/migris v0.4.1
github.com/akfaiz/migris/extra/migriscli v0.0.0
github.com/jackc/pgx/v5 v5.7.5
github.com/jackc/pgx/v5 v5.8.0
github.com/joho/godotenv v1.5.1
)

Expand All @@ -17,15 +17,14 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/pressly/goose/v3 v3.26.0 // indirect
github.com/pressly/goose/v3 v3.27.0 // indirect
github.com/sethvargo/go-retry v0.3.0 // indirect
github.com/urfave/cli/v3 v3.6.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.40.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sys v0.34.0 // indirect
golang.org/x/term v0.33.0 // indirect
golang.org/x/text v0.28.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.45.0 // indirect
golang.org/x/term v0.43.0 // indirect
golang.org/x/text v0.37.0 // indirect
)

replace github.com/akfaiz/migris => ../../..
Expand Down
176 changes: 90 additions & 86 deletions extra/migriscli/example/go.sum

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions extra/migriscli/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/akfaiz/migris/extra/migriscli

go 1.23.0
go 1.25.0

require (
github.com/akfaiz/migris v0.4.1
Expand All @@ -12,12 +12,12 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/pressly/goose/v3 v3.26.0 // indirect
github.com/pressly/goose/v3 v3.27.0 // indirect
github.com/sethvargo/go-retry v0.3.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sys v0.34.0 // indirect
golang.org/x/term v0.33.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.45.0 // indirect
golang.org/x/term v0.43.0 // indirect
)

replace github.com/akfaiz/migris => ../..
176 changes: 90 additions & 86 deletions extra/migriscli/go.sum

Large diffs are not rendered by default.

150 changes: 150 additions & 0 deletions extra/migriscli/migriscli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package migriscli //nolint:testpackage // Tests cover unexported command builders and option helpers.

import (
"bytes"
"context"
"os"
"slices"
"strings"
"testing"

"github.com/urfave/cli/v3"
)

func TestNewCLI(t *testing.T) {
cmd := NewCLI(Config{MigrationsDir: "migrations"})

if cmd.Name != "migrate" {
t.Fatalf("expected command name migrate, got %q", cmd.Name)
}
if cmd.Usage != "Database migration CLI tool" {
t.Fatalf("unexpected command usage: %q", cmd.Usage)
}
if len(cmd.Commands) != 7 {
t.Fatalf("expected 7 commands, got %d", len(cmd.Commands))
}
wantCommands := []string{"create", "up", "up-to", "down", "down-to", "reset", "status"}
if !slices.Equal(commandNames(cmd.Commands), wantCommands) {
t.Fatalf("expected commands %v, got %v", wantCommands, commandNames(cmd.Commands))
}

createCmd := cmd.Command("create")
if createCmd == nil {
t.Fatal("expected create command")
}
if len(createCmd.Flags) != 1 {
t.Fatalf("expected 1 create flag, got %d", len(createCmd.Flags))
}
nameFlag, ok := createCmd.Flags[0].(*cli.StringFlag)
if !ok {
t.Fatalf("expected create flag to be *cli.StringFlag, got %T", createCmd.Flags[0])
}
if nameFlag.Name != "name" || !slices.Equal(nameFlag.Aliases, []string{"n"}) || !nameFlag.Required {
t.Fatalf("unexpected create name flag: %#v", nameFlag)
}

upToCmd := cmd.Command("up-to")
if upToCmd == nil {
t.Fatal("expected up-to command")
}
if len(upToCmd.Flags) != 2 {
t.Fatalf("expected 2 up-to flags, got %d", len(upToCmd.Flags))
}
versionFlag, ok := upToCmd.Flags[1].(*cli.Int64Flag)
if !ok {
t.Fatalf("expected version flag to be *cli.Int64Flag, got %T", upToCmd.Flags[1])
}
if versionFlag.Name != "version" || !slices.Equal(versionFlag.Aliases, []string{"v"}) || !versionFlag.Required {
t.Fatalf("unexpected up-to version flag: %#v", versionFlag)
}
}

func TestCreateCommandCreatesMigrationFile(t *testing.T) {
dir := t.TempDir()
cmd := NewCLI(Config{MigrationsDir: dir})
cmd.Writer = bytes.NewBuffer(nil)
cmd.ErrWriter = bytes.NewBuffer(nil)

err := cmd.Run(context.Background(), []string{"migrate", "create", "--name", "create_users_table"})
if err != nil {
t.Fatalf("expected create command to succeed: %v", err)
}

entries, err := os.ReadDir(dir)
if err != nil {
t.Fatalf("expected to read migration dir: %v", err)
}
if len(entries) != 1 {
t.Fatalf("expected 1 migration file, got %d", len(entries))
}
if !strings.Contains(entries[0].Name(), "_create_users_table.go") {
t.Fatalf("expected generated create users migration, got %q", entries[0].Name())
}
}

func TestMigrationCommandsReturnMigratorConfigErrors(t *testing.T) {
for _, name := range []string{"up", "up-to", "down", "down-to", "reset", "status"} {
t.Run(name, func(t *testing.T) {
args := []string{"migrate", name}
if name == "up-to" || name == "down-to" {
args = append(args, "--version", "20250101000000")
}

cmd := NewCLI(Config{Dialect: "unknown", MigrationsDir: t.TempDir()})
cmd.Writer = bytes.NewBuffer(nil)
cmd.ErrWriter = bytes.NewBuffer(nil)

err := cmd.Run(context.Background(), args)
if err == nil {
t.Fatal("expected migrator config error")
}
if !strings.Contains(err.Error(), "unknown database dialect") {
t.Fatalf("expected unknown dialect error, got %v", err)
}
})
}
}

func TestRunOpts(t *testing.T) {
var got []any
cmd := runOptsTestCommand(&got, Config{AllowMissing: true})

err := cmd.Run(context.Background(), []string{"up", "--dry-run"})
if err != nil {
t.Fatalf("expected dry-run command to succeed: %v", err)
}
if len(got) != 2 {
t.Fatalf("expected 2 run options, got %d", len(got))
}

got = nil
cmd = runOptsTestCommand(&got, Config{})
err = cmd.Run(context.Background(), []string{"up"})
if err != nil {
t.Fatalf("expected command to succeed: %v", err)
}
if len(got) != 0 {
t.Fatalf("expected no run options, got %d", len(got))
}
}

func runOptsTestCommand(got *[]any, cfg Config) *cli.Command {
return &cli.Command{
Name: "up",
Flags: []cli.Flag{dryRunFlag()},
Action: func(_ context.Context, c *cli.Command) error {
for _, opt := range runOpts(c, cfg) {
*got = append(*got, opt)
}
return nil
},
}
}

func commandNames(commands []*cli.Command) []string {
names := make([]string, len(commands))
for i, cmd := range commands {
names[i] = cmd.Name
}
return names
}
15 changes: 7 additions & 8 deletions extra/migriscobra/example/go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/akfaiz/migris/extra/migriscobra/example

go 1.23.0
go 1.25.0

require (
github.com/akfaiz/migris v0.4.1
github.com/akfaiz/migris/extra/migriscobra v0.0.0
github.com/jackc/pgx/v5 v5.7.5
github.com/jackc/pgx/v5 v5.8.0
github.com/joho/godotenv v1.5.1
)

Expand All @@ -18,16 +18,15 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/pressly/goose/v3 v3.26.0 // indirect
github.com/pressly/goose/v3 v3.27.0 // indirect
github.com/sethvargo/go-retry v0.3.0 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.40.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sys v0.34.0 // indirect
golang.org/x/term v0.33.0 // indirect
golang.org/x/text v0.28.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.45.0 // indirect
golang.org/x/term v0.43.0 // indirect
golang.org/x/text v0.37.0 // indirect
)

replace github.com/akfaiz/migris => ../../..
Expand Down
Loading
Loading