Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(postgrest-config): add delete command to cli for convenience #3060

Merged
merged 1 commit into from
Jan 22, 2025
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
16 changes: 16 additions & 0 deletions cmd/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/supabase/cli/internal/postgresConfig/delete"
"github.com/supabase/cli/internal/postgresConfig/get"
"github.com/supabase/cli/internal/postgresConfig/update"
"github.com/supabase/cli/internal/utils/flags"
Expand Down Expand Up @@ -33,20 +34,35 @@ Custom configuration also overrides the optimizations generated based on the com
},
}

postgresConfigDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete specific Postgres database config overrides",
Long: "Delete specific config overrides, reverting them to their default values.",
RunE: func(cmd *cobra.Command, args []string) error {
return delete.Run(cmd.Context(), flags.ProjectRef, postgresConfigKeysToDelete, noRestart, afero.NewOsFs())
},
}

postgresConfigValues []string
postgresConfigUpdateReplaceMode bool
postgresConfigKeysToDelete []string
noRestart bool
)

func init() {
postgresCmd.PersistentFlags().StringVar(&flags.ProjectRef, "project-ref", "", "Project ref of the Supabase project.")
postgresCmd.AddCommand(postgresConfigGetCmd)
postgresCmd.AddCommand(postgresConfigUpdateCmd)
postgresCmd.AddCommand(postgresConfigDeleteCmd)

updateFlags := postgresConfigUpdateCmd.Flags()
updateFlags.StringSliceVar(&postgresConfigValues, "config", []string{}, "Config overrides specified as a 'key=value' pair")
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.")
updateFlags.BoolVar(&noRestart, "no-restart", false, "Do not restart the database after updating config.")

deleteFlags := postgresConfigDeleteCmd.Flags()
deleteFlags.StringSliceVar(&postgresConfigKeysToDelete, "config", []string{}, "Config keys to delete (comma-separated)")
deleteFlags.BoolVar(&noRestart, "no-restart", false, "Do not restart the database after deleting config.")

rootCmd.AddCommand(postgresCmd)
}
48 changes: 48 additions & 0 deletions internal/postgresConfig/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package delete

import (
"bytes"
"context"
"encoding/json"
"strings"

"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/postgresConfig/get"
"github.com/supabase/cli/internal/utils"
)

func Run(ctx context.Context, projectRef string, configKeys []string, noRestart bool, fsys afero.Fs) error {
// 1. Get current config
currentConfig, err := get.GetCurrentPostgresConfig(ctx, projectRef)
if err != nil {
return err
}

// 2. Remove specified keys
for _, key := range configKeys {
delete(currentConfig, strings.TrimSpace(key))
}

// 3. Update config with removed keys
if noRestart {
currentConfig["restart_database"] = false
}
bts, err := json.Marshal(currentConfig)
if err != nil {
return errors.Errorf("failed to serialize config overrides: %w", err)
}

resp, err := utils.GetSupabase().V1UpdatePostgresConfigWithBodyWithResponse(ctx, projectRef, "application/json", bytes.NewReader(bts))
if err != nil {
return errors.Errorf("failed to update config overrides: %w", err)
}
if resp.JSON200 == nil {
if resp.StatusCode() == 400 {
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))
}
return errors.Errorf("failed to update config overrides: %s (%s)", resp.Status(), string(resp.Body))
}

return get.Run(ctx, projectRef, fsys)
}
Loading