github.com/supabase/cli@v1.168.1/internal/db/branch/delete/delete.go (about)

     1  package delete
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"io"
     8  	"path/filepath"
     9  
    10  	"github.com/docker/docker/api/types"
    11  	"github.com/docker/docker/pkg/stdcopy"
    12  	"github.com/go-errors/errors"
    13  	"github.com/spf13/afero"
    14  	"github.com/supabase/cli/internal/utils"
    15  )
    16  
    17  func Run(branch string, fsys afero.Fs) error {
    18  	if err := utils.LoadConfigFS(fsys); err != nil {
    19  		return err
    20  	}
    21  	if err := utils.AssertSupabaseDbIsRunning(); err != nil {
    22  		return err
    23  	}
    24  
    25  	// TODO: update branch history atomically with database
    26  	if err := deleteBranchDir(branch, fsys); err != nil {
    27  		return err
    28  	}
    29  
    30  	ctx := context.Background()
    31  	if err := deleteBranchPG(ctx, branch); err != nil {
    32  		return err
    33  	}
    34  
    35  	fmt.Println("Deleted branch " + utils.Aqua(branch) + ".")
    36  	return nil
    37  }
    38  
    39  func deleteBranchDir(branch string, fsys afero.Fs) error {
    40  	if currBranch, _ := utils.GetCurrentBranchFS(fsys); branch == currBranch {
    41  		return errors.New("Cannot delete current branch.")
    42  	}
    43  
    44  	if utils.IsBranchNameReserved(branch) {
    45  		return errors.New("Cannot delete branch " + utils.Aqua(branch) + ": branch name is reserved.")
    46  	}
    47  
    48  	branchPath := filepath.Join(filepath.Dir(utils.CurrBranchPath), branch)
    49  	if _, err := afero.ReadDir(fsys, branchPath); err != nil {
    50  		return errors.New("Branch " + utils.Aqua(branch) + " does not exist.")
    51  	}
    52  
    53  	if err := fsys.RemoveAll(branchPath); err != nil {
    54  		return errors.Errorf("Failed deleting branch %s: %w", utils.Aqua(branch), err)
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  func deleteBranchPG(ctx context.Context, branch string) error {
    61  	exec, err := utils.Docker.ContainerExecCreate(ctx, utils.DbId, types.ExecConfig{
    62  		Cmd:          []string{"dropdb", "--username", "postgres", "--host", "127.0.0.1", branch},
    63  		AttachStderr: true,
    64  		AttachStdout: true,
    65  	})
    66  	if err != nil {
    67  		return err
    68  	}
    69  	// Read exec output
    70  	resp, err := utils.Docker.ContainerExecAttach(ctx, exec.ID, types.ExecStartCheck{})
    71  	if err != nil {
    72  		return err
    73  	}
    74  	defer resp.Close()
    75  	// Capture error details
    76  	var errBuf bytes.Buffer
    77  	if _, err := stdcopy.StdCopy(io.Discard, &errBuf, resp.Reader); err != nil {
    78  		return err
    79  	}
    80  	// Get the exit code
    81  	iresp, err := utils.Docker.ContainerExecInspect(ctx, exec.ID)
    82  	if err != nil {
    83  		return err
    84  	}
    85  	if iresp.ExitCode > 0 {
    86  		return errors.New("Error deleting branch: " + errBuf.String())
    87  	}
    88  	return nil
    89  }