Skip to content

Commit

Permalink
feat: add file flag to diff command
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Aug 8, 2022
1 parent 5652eb4 commit 3b7280e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
7 changes: 5 additions & 2 deletions cmd/db.go
Expand Up @@ -57,15 +57,17 @@ var (

useMigra bool
schema []string
file string

dbDiffCmd = &cobra.Command{
Use: "diff",
Short: "Diffs the local database with current migrations, then print the diff to standard output.",
RunE: func(cmd *cobra.Command, args []string) error {
fsys := afero.NewOsFs()
if useMigra {
return diff.RunMigra(cmd.Context(), schema, afero.NewOsFs())
return diff.RunMigra(cmd.Context(), schema, file, fsys)
}
return diff.Run()
return diff.Run(file, fsys)
},
}

Expand Down Expand Up @@ -135,6 +137,7 @@ func init() {
dbBranchCmd.AddCommand(dbBranchListCmd)
dbCmd.AddCommand(dbBranchCmd)
dbDiffCmd.Flags().BoolVar(&useMigra, "use-migra", false, "Use migra to generate schema diff.")
dbDiffCmd.Flags().StringVarP(&file, "file", "f", "", "Name of the new migration file to create.")
dbDiffCmd.Flags().StringSliceVarP(&schema, "schema", "s", []string{"public"}, "The list of schema to diff (defaults to public).")
dbCmd.AddCommand(dbDiffCmd)
dbPushCmd.Flags().BoolVar(&dryRun, "dry-run", false, "Print the migrations that would be applied, but don't actually apply them.")
Expand Down
21 changes: 18 additions & 3 deletions internal/db/diff/diff.go
Expand Up @@ -17,14 +17,19 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/docker/docker/pkg/stdcopy"
"github.com/muesli/reflow/wrap"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/migration/new"
"github.com/supabase/cli/internal/utils"
)

// TODO: Handle cleanup on SIGINT/SIGTERM.
func Run() error {
func Run(file string, fsys afero.Fs) error {
// Sanity checks.
{
if err := utils.AssertSupabaseStartIsRunning(); err != nil {
if err := utils.LoadConfigFS(fsys); err != nil {
return err
}
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
return err
}
}
Expand All @@ -50,7 +55,17 @@ func Run() error {
return err
}

fmt.Println(diff)
if len(file) > 0 {
// Pipe to new migration command
r, w, err := os.Pipe()
if err != nil {
return err
}
w.WriteString(diff)
return new.Run(file, r, fsys)
} else {
fmt.Println(diff)
}
return nil
}

Expand Down
11 changes: 10 additions & 1 deletion internal/db/diff/migra.go
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/spf13/afero"
"github.com/spf13/viper"
"github.com/supabase/cli/internal/debug"
"github.com/supabase/cli/internal/migration/new"
"github.com/supabase/cli/internal/utils"
)

Expand All @@ -30,7 +31,7 @@ var (
resetShadowScript string
)

func RunMigra(ctx context.Context, schema []string, fsys afero.Fs) error {
func RunMigra(ctx context.Context, schema []string, file string, fsys afero.Fs) error {
// Sanity checks.
{
if err := utils.LoadConfigFS(fsys); err != nil {
Expand Down Expand Up @@ -77,6 +78,14 @@ func RunMigra(ctx context.Context, schema []string, fsys afero.Fs) error {

if len(out) < 2 {
fmt.Fprintln(os.Stderr, "No changes found")
} else if len(file) > 0 {
// Pipe to new migration command
r, w, err := os.Pipe()
if err != nil {
return err
}
w.WriteString(out)
return new.Run(file, r, fsys)
} else {
fmt.Println(out)
}
Expand Down

0 comments on commit 3b7280e

Please sign in to comment.