Skip to content

Commit 3f52227

Browse files
authored
feat(postgrest-config): add delete command to cli for convenience (#3060)
1 parent 9509bc6 commit 3f52227

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

cmd/postgres.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"github.com/spf13/afero"
55
"github.com/spf13/cobra"
6+
"github.com/supabase/cli/internal/postgresConfig/delete"
67
"github.com/supabase/cli/internal/postgresConfig/get"
78
"github.com/supabase/cli/internal/postgresConfig/update"
89
"github.com/supabase/cli/internal/utils/flags"
@@ -33,20 +34,35 @@ Custom configuration also overrides the optimizations generated based on the com
3334
},
3435
}
3536

37+
postgresConfigDeleteCmd = &cobra.Command{
38+
Use: "delete",
39+
Short: "Delete specific Postgres database config overrides",
40+
Long: "Delete specific config overrides, reverting them to their default values.",
41+
RunE: func(cmd *cobra.Command, args []string) error {
42+
return delete.Run(cmd.Context(), flags.ProjectRef, postgresConfigKeysToDelete, noRestart, afero.NewOsFs())
43+
},
44+
}
45+
3646
postgresConfigValues []string
3747
postgresConfigUpdateReplaceMode bool
48+
postgresConfigKeysToDelete []string
3849
noRestart bool
3950
)
4051

4152
func init() {
4253
postgresCmd.PersistentFlags().StringVar(&flags.ProjectRef, "project-ref", "", "Project ref of the Supabase project.")
4354
postgresCmd.AddCommand(postgresConfigGetCmd)
4455
postgresCmd.AddCommand(postgresConfigUpdateCmd)
56+
postgresCmd.AddCommand(postgresConfigDeleteCmd)
4557

4658
updateFlags := postgresConfigUpdateCmd.Flags()
4759
updateFlags.StringSliceVar(&postgresConfigValues, "config", []string{}, "Config overrides specified as a 'key=value' pair")
4860
updateFlags.BoolVar(&postgresConfigUpdateReplaceMode, "replace-existing-overrides", false, "If true, replaces all existing overrides with the ones provided. If false (default), merges existing overrides with the ones provided.")
4961
updateFlags.BoolVar(&noRestart, "no-restart", false, "Do not restart the database after updating config.")
5062

63+
deleteFlags := postgresConfigDeleteCmd.Flags()
64+
deleteFlags.StringSliceVar(&postgresConfigKeysToDelete, "config", []string{}, "Config keys to delete (comma-separated)")
65+
deleteFlags.BoolVar(&noRestart, "no-restart", false, "Do not restart the database after deleting config.")
66+
5167
rootCmd.AddCommand(postgresCmd)
5268
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package delete
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"strings"
8+
9+
"github.com/go-errors/errors"
10+
"github.com/spf13/afero"
11+
"github.com/supabase/cli/internal/postgresConfig/get"
12+
"github.com/supabase/cli/internal/utils"
13+
)
14+
15+
func Run(ctx context.Context, projectRef string, configKeys []string, noRestart bool, fsys afero.Fs) error {
16+
// 1. Get current config
17+
currentConfig, err := get.GetCurrentPostgresConfig(ctx, projectRef)
18+
if err != nil {
19+
return err
20+
}
21+
22+
// 2. Remove specified keys
23+
for _, key := range configKeys {
24+
delete(currentConfig, strings.TrimSpace(key))
25+
}
26+
27+
// 3. Update config with removed keys
28+
if noRestart {
29+
currentConfig["restart_database"] = false
30+
}
31+
bts, err := json.Marshal(currentConfig)
32+
if err != nil {
33+
return errors.Errorf("failed to serialize config overrides: %w", err)
34+
}
35+
36+
resp, err := utils.GetSupabase().V1UpdatePostgresConfigWithBodyWithResponse(ctx, projectRef, "application/json", bytes.NewReader(bts))
37+
if err != nil {
38+
return errors.Errorf("failed to update config overrides: %w", err)
39+
}
40+
if resp.JSON200 == nil {
41+
if resp.StatusCode() == 400 {
42+
return errors.Errorf("failed to update config overrides: %s (%s). This usually indicates that an unsupported or invalid config override was attempted. Please refer to https://supabase.com/docs/guides/platform/custom-postgres-config", resp.Status(), string(resp.Body))
43+
}
44+
return errors.Errorf("failed to update config overrides: %s (%s)", resp.Status(), string(resp.Body))
45+
}
46+
47+
return get.Run(ctx, projectRef, fsys)
48+
}

0 commit comments

Comments
 (0)