github.com/supabase/cli@v1.168.1/internal/postgresConfig/update/update.go (about)

     1  package update
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"strconv"
     8  	"strings"
     9  
    10  	"github.com/go-errors/errors"
    11  	"github.com/spf13/afero"
    12  	"github.com/supabase/cli/internal/postgresConfig/get"
    13  	"github.com/supabase/cli/internal/utils"
    14  )
    15  
    16  func Run(ctx context.Context, projectRef string, values []string, replaceOverrides bool, fsys afero.Fs) error {
    17  	// 1. Prepare config overrides
    18  	newConfigOverrides := make(map[string]string)
    19  	for _, config := range values {
    20  		splits := strings.Split(config, "=")
    21  		if len(splits) != 2 {
    22  			return errors.Errorf("expected config value in key:value format, received: '%s'", config)
    23  		}
    24  		newConfigOverrides[splits[0]] = splits[1]
    25  	}
    26  	// 2. If not in replace mode, retrieve current overrides
    27  	finalOverrides := make(map[string]interface{})
    28  	{
    29  		if !replaceOverrides {
    30  			config, err := get.GetCurrentPostgresConfig(ctx, projectRef)
    31  			if err != nil {
    32  				return err
    33  			}
    34  			finalOverrides = config
    35  		}
    36  	}
    37  	// 3. Create the list of final overrides
    38  	{
    39  		for k, v := range newConfigOverrides {
    40  			// this is hacky - if we're able to convert the value to an integer, we do so
    41  			// if we start supporting config fields with e.g. floating pt overrides this'll need to be updated
    42  			attemptedConvert, err := strconv.Atoi(v)
    43  			if err != nil {
    44  				finalOverrides[k] = v
    45  			} else {
    46  				finalOverrides[k] = attemptedConvert
    47  			}
    48  		}
    49  	}
    50  	// 4. update config overrides and print out final result
    51  	{
    52  		bts, err := json.Marshal(finalOverrides)
    53  		if err != nil {
    54  			return errors.Errorf("failed to serialize config overrides: %w", err)
    55  		}
    56  		resp, err := utils.GetSupabase().UpdateConfigWithBodyWithResponse(ctx, projectRef, "application/json", bytes.NewReader(bts))
    57  		if err != nil {
    58  			return errors.Errorf("failed to update config overrides: %w", err)
    59  		}
    60  		if resp.JSON200 == nil {
    61  			if resp.StatusCode() == 400 {
    62  				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))
    63  			}
    64  			return errors.Errorf("failed to update config overrides: %s (%s)", resp.Status(), string(resp.Body))
    65  		}
    66  	}
    67  	return get.Run(ctx, projectRef, fsys)
    68  }