Skip to content
Merged
37 changes: 30 additions & 7 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/supabase/cli/legacy/branch/delete"
"github.com/supabase/cli/legacy/branch/list"
"github.com/supabase/cli/legacy/branch/switch_"
legacy "github.com/supabase/cli/legacy/diff"
"github.com/supabase/cli/pkg/migration"
)

Expand Down Expand Up @@ -85,24 +84,36 @@ var (
usePgAdmin bool
usePgSchema bool
usePgDelta bool
diffFrom string
diffTo string
outputPath string
schema []string
file string

dbDiffCmd = &cobra.Command{
Use: "diff",
Short: "Diffs the local database for schema changes",
RunE: func(cmd *cobra.Command, args []string) error {
if len(diffFrom) > 0 || len(diffTo) > 0 {
switch {
case len(diffFrom) == 0 || len(diffTo) == 0:
return fmt.Errorf("must set both --from and --to when using explicit diff mode")
default:
return diff.RunExplicit(cmd.Context(), diffFrom, diffTo, schema, outputPath, afero.NewOsFs())
}
}
useDelta := shouldUsePgDelta()
if usePgAdmin {
return legacy.RunPgAdmin(cmd.Context(), schema, file, flags.DbConfig, afero.NewOsFs())
return diff.RunPgAdmin(cmd.Context(), schema, file, flags.DbConfig, afero.NewOsFs())
}
differ := diff.DiffSchemaMigra
if usePgSchema {
differ = diff.DiffPgSchema
fmt.Fprintln(os.Stderr, utils.Yellow("WARNING:"), "--use-pg-schema flag is experimental and may not include all entities, such as views and grants.")
} else if usePgDelta {
} else if useDelta {
differ = diff.DiffPgDelta
}
return diff.Run(cmd.Context(), schema, file, flags.DbConfig, differ, afero.NewOsFs())
return diff.Run(cmd.Context(), schema, file, flags.DbConfig, differ, useDelta, afero.NewOsFs())
},
}

Expand Down Expand Up @@ -161,7 +172,8 @@ var (
if len(args) > 0 {
name = args[0]
}
return pull.Run(cmd.Context(), schema, flags.DbConfig, name, afero.NewOsFs())
useDelta := shouldUsePgDelta()
return pull.Run(cmd.Context(), schema, flags.DbConfig, name, useDelta, afero.NewOsFs())
},
PostRun: func(cmd *cobra.Command, args []string) {
fmt.Println("Finished " + utils.Aqua("supabase db pull") + ".")
Expand All @@ -180,7 +192,7 @@ var (
Short: "Show changes on the remote database",
Long: "Show changes on the remote database since last migration.",
RunE: func(cmd *cobra.Command, args []string) error {
return diff.Run(cmd.Context(), schema, file, flags.DbConfig, diff.DiffSchemaMigra, afero.NewOsFs())
return diff.Run(cmd.Context(), schema, file, flags.DbConfig, diff.DiffSchemaMigra, false, afero.NewOsFs())
},
}

Expand All @@ -189,7 +201,8 @@ var (
Use: "commit",
Short: "Commit remote changes as a new migration",
RunE: func(cmd *cobra.Command, args []string) error {
return pull.Run(cmd.Context(), schema, flags.DbConfig, "remote_commit", afero.NewOsFs())
useDelta := shouldUsePgDelta()
return pull.Run(cmd.Context(), schema, flags.DbConfig, "remote_commit", useDelta, afero.NewOsFs())
},
}

Expand Down Expand Up @@ -332,6 +345,10 @@ without the envelope.`,
}
)

func shouldUsePgDelta() bool {
return utils.IsPgDeltaEnabled() || usePgDelta || viper.GetBool("EXPERIMENTAL_PG_DELTA")
}

func init() {
// Build branch command
dbBranchCmd.AddCommand(dbBranchCreateCmd)
Expand All @@ -346,6 +363,9 @@ func init() {
diffFlags.BoolVar(&usePgSchema, "use-pg-schema", false, "Use pg-schema-diff to generate schema diff.")
diffFlags.BoolVar(&usePgDelta, "use-pg-delta", false, "Use pg-delta to generate schema diff.")
dbDiffCmd.MarkFlagsMutuallyExclusive("use-migra", "use-pgadmin", "use-pg-schema", "use-pg-delta")
diffFlags.StringVar(&diffFrom, "from", "", "Diff from local, linked, migrations, or a Postgres URL.")
diffFlags.StringVar(&diffTo, "to", "", "Diff to local, linked, migrations, or a Postgres URL.")
diffFlags.StringVarP(&outputPath, "output", "o", "", "Write explicit diff output to a file path.")
diffFlags.String("db-url", "", "Diffs against the database specified by the connection string (must be percent-encoded).")
diffFlags.Bool("linked", false, "Diffs local migration files against the linked project.")
diffFlags.Bool("local", true, "Diffs local migration files against the local database.")
Expand Down Expand Up @@ -388,6 +408,9 @@ func init() {
dbCmd.AddCommand(dbPushCmd)
// Build pull command
pullFlags := dbPullCmd.Flags()
// This flag activates declarative pull output through pg-delta instead of the
// legacy migration SQL pull path.
pullFlags.BoolVar(&usePgDelta, "use-pg-delta", false, "Use pg-delta to pull declarative schema.")
pullFlags.StringSliceVarP(&schema, "schema", "s", []string{}, "Comma separated list of schema to include.")
pullFlags.String("db-url", "", "Pulls from the database specified by the connection string (must be percent-encoded).")
pullFlags.Bool("linked", true, "Pulls from the linked project.")
Expand Down
Loading
Loading