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
32 changes: 19 additions & 13 deletions pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cli
import (
"context"
"fmt"
"io"
"os"
"strings"

Expand Down Expand Up @@ -158,26 +159,31 @@ func newStatusCmd() *cobra.Command {
if err != nil {
return fmt.Errorf("migrate status: %w", err)
}
if st.Current == "" {
fmt.Println("No migrations applied.")
} else {
fmt.Printf("Current: %s\n", st.Current)
}
if len(st.Pending) > 0 {
fmt.Printf("Pending: %v\n", st.Pending)
} else {
fmt.Println("No pending migrations.")
}
if st.Dirty {
fmt.Println("WARNING: database is in dirty state!")
}
writeStatus(cmd.OutOrStdout(), st)
return nil
},
}
sharedFlags(cmd)
return cmd
}

func writeStatus(out io.Writer, st interfaces.MigrationStatus) {
if st.Current == "" {
fmt.Fprintln(out, "No migrations applied.")
} else {
fmt.Fprintf(out, "Current: %s\n", st.Current)
}
if len(st.Pending) > 0 {
fmt.Fprintf(out, "Pending: %v\n", st.Pending)
} else {
fmt.Fprintln(out, "No pending migrations.")
}
fmt.Fprintf(out, "Dirty: %t\n", st.Dirty)
if st.Dirty {
fmt.Fprintln(out, "WARNING: database is in dirty state!")
}
}

func newGotoCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "goto <version>",
Expand Down
25 changes: 25 additions & 0 deletions pkg/cli/root_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package cli

import (
"bytes"
"database/sql"
"os"
"path/filepath"
"strings"
"testing"

"github.com/GoCodeAlone/workflow-plugin-migrations/pkg/testharness"
"github.com/GoCodeAlone/workflow/interfaces"
_ "github.com/jackc/pgx/v5/stdlib"
)

Expand Down Expand Up @@ -40,6 +42,29 @@ func TestRootIncludesRepairDirtyCommand(t *testing.T) {
}
}

func TestWriteStatusIncludesExplicitDirtyFlag(t *testing.T) {
var out bytes.Buffer
writeStatus(&out, interfaces.MigrationStatus{
Current: "202604270001",
Dirty: false,
})

got := out.String()
if !strings.Contains(got, "Dirty: false") {
t.Fatalf("status output = %q; want explicit clean dirty flag", got)
}

out.Reset()
writeStatus(&out, interfaces.MigrationStatus{
Current: "202604270001",
Dirty: true,
})
got = out.String()
if !strings.Contains(got, "Dirty: true") || !strings.Contains(got, "WARNING: database is in dirty state!") {
t.Fatalf("status output = %q; want explicit dirty flag and warning", got)
}
}

func TestRootIncludesValidateUpgradeCommand(t *testing.T) {
root := NewRoot()

Expand Down
Loading