Repository: supabase/cli
Pull request #1035 (2023-04-25 08:37:14)

fix: multiple command inconsistencies
## What kind of change does this PR introduce?

Bug fix

## What is the new behavior?

Update commands to match documentation

## Additional context

Add any other context or screenshots.



Patch:
diff --git a/internal/db/diff/diff.go b/internal/db/diff/diff.go
index 791d4126..034581a9 100644
--- a/internal/db/diff/diff.go
+++ b/internal/db/diff/diff.go
@@ -17,7 +17,7 @@ Run ` + utils.Aqua("supabase db reset") + ` to verify that the new migration doe
 
 func SaveDiff(out, file string, fsys afero.Fs) error {
 	if len(out) < 2 {
-		fmt.Fprintln(os.Stderr, "No changes found")
+		fmt.Fprintln(os.Stderr, "No schema changes found")
 	} else if len(file) > 0 {
 		path := new.GetMigrationPath(file)
 		if err := afero.WriteFile(fsys, path, []byte(out), 0644); err != nil {
diff --git a/internal/db/diff/migra.go b/internal/db/diff/migra.go
index 903ad43a..3bcc5f67 100644
--- a/internal/db/diff/migra.go
+++ b/internal/db/diff/migra.go
@@ -93,8 +93,8 @@ func LoadUserSchemas(ctx context.Context, conn *pgx.Conn, exclude ...string) ([]
 			"supabase_functions",
 			"supabase_migrations",
 		}, utils.SystemSchemas...)
-		exclude = likeEscapeSchema(exclude)
 	}
+	exclude = likeEscapeSchema(exclude)
 	rows, err := conn.Query(ctx, LIST_SCHEMAS, exclude)
 	if err != nil {
 		return nil, err
diff --git a/internal/db/dump/templates/dump_role.sh b/internal/db/dump/templates/dump_role.sh
index 522478c5..2a6f39bf 100755
--- a/internal/db/dump/templates/dump_role.sh
+++ b/internal/db/dump/templates/dump_role.sh
@@ -19,7 +19,7 @@ pg_dumpall \
 | sed -E "s/^CREATE ROLE \"($RESERVED_ROLES)\"/-- &/" \
 | sed -E "s/^ALTER ROLE \"($RESERVED_ROLES)\"/-- &/" \
 | sed -E "s/^-- (.* SET \"($ALLOWED_CONFIGS)\" .*)/\1/" \
-| sed -E "s/.* GRANTED BY \"supabase_admin\";$/-- &/"
+| sed -E "s/GRANT \".*\" TO \"($RESERVED_ROLES)\"/-- &/"
 
 # Reset session config generated by pg_dump
 echo "RESET ALL;"
diff --git a/internal/db/lint/lint.go b/internal/db/lint/lint.go
index f5855fa4..5bacb4a0 100644
--- a/internal/db/lint/lint.go
+++ b/internal/db/lint/lint.go
@@ -55,6 +55,10 @@ func Run(ctx context.Context, schema []string, level string, fsys afero.Fs, opts
 	if err != nil {
 		return err
 	}
+	if len(result) == 0 {
+		fmt.Fprintln(os.Stderr, "\nNo schema errors found")
+		return nil
+	}
 	return printResultJSON(result, toEnum(level), os.Stdout)
 }
 
diff --git a/internal/db/remote/commit/commit.go b/internal/db/remote/commit/commit.go
index eb60bf9c..7340c2db 100644
--- a/internal/db/remote/commit/commit.go
+++ b/internal/db/remote/commit/commit.go
@@ -85,7 +85,7 @@ func fetchRemote(p utils.Program, ctx context.Context, schema []string, timestam
 		return err
 	}
 	if len(output) == 0 {
-		return errors.New("no changes found")
+		return errors.New("no schema changes found")
 	}
 	return afero.WriteFile(fsys, path, []byte(output), 0644)
 }
diff --git a/internal/db/start/start.go b/internal/db/start/start.go
index aeabcb7f..59e66ce2 100644
--- a/internal/db/start/start.go
+++ b/internal/db/start/start.go
@@ -146,8 +146,8 @@ func SetupDatabase(ctx context.Context, fsys afero.Fs, w io.Writer, options ...f
 		return err
 	}
 	defer conn.Close(context.Background())
-	fmt.Fprintln(w, "Setting up local schema...")
 	if roles, err := fsys.Open(utils.CustomRolesPath); err == nil {
+		fmt.Fprintln(w, "Creating custom roles "+utils.Bold(utils.CustomRolesPath)+"...")
 		if err := apply.BatchExecDDL(ctx, conn, roles); err != nil {
 			return err
 		}
diff --git a/internal/functions/serve/serve.go b/internal/functions/serve/serve.go
index 9afc80a4..a4b214b6 100644
--- a/internal/functions/serve/serve.go
+++ b/internal/functions/serve/serve.go
@@ -6,6 +6,7 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
+	"io"
 	"os"
 	"path/filepath"
 	"strings"
@@ -237,7 +238,7 @@ func runServeAll(ctx context.Context, envFilePath string, noVerifyJWT *bool, imp
 		Force:         true,
 	})
 	// 3. Serve and log to console
-	if err := ServeFunctions(ctx, envFilePath, noVerifyJWT, importMapPath, fsys); err != nil {
+	if err := ServeFunctions(ctx, envFilePath, noVerifyJWT, importMapPath, os.Stderr, fsys); err != nil {
 		return err
 	}
 	if err := utils.DockerStreamLogs(ctx, utils.DenoRelayId, os.Stdout, os.Stderr); err != nil {
@@ -247,7 +248,7 @@ func runServeAll(ctx context.Context, envFilePath string, noVerifyJWT *bool, imp
 	return nil
 }
 
-func ServeFunctions(ctx context.Context, envFilePath string, noVerifyJWT *bool, importMapPath string, fsys afero.Fs) error {
+func ServeFunctions(ctx context.Context, envFilePath string, noVerifyJWT *bool, importMapPath string, w io.Writer, fsys afero.Fs) error {
 	// 1. Load default values
 	if envFilePath == "" {
 		if f, err := fsys.Stat(utils.FallbackEnvFilePath); err == nil && !f.IsDir() {
@@ -313,7 +314,7 @@ func ServeFunctions(ctx context.Context, envFilePath string, noVerifyJWT *bool,
 	env = append(env, "SUPABASE_INTERNAL_FUNCTIONS_CONFIG="+functionsConfigString)
 
 	// 4. Start container
-	fmt.Println("Setting up Edge Functions runtime...")
+	fmt.Fprintln(w, "Setting up Edge Functions runtime...")
 
 	var cmdString string
 	{
diff --git a/internal/start/start.go b/internal/start/start.go
index 32fda6d6..c67304ce 100644
--- a/internal/start/start.go
+++ b/internal/start/start.go
@@ -534,7 +534,7 @@ EOF
 
 	// Start all functions.
 	if !isContainerExcluded(utils.EdgeRuntimeImage, excluded) {
-		if err := serve.ServeFunctions(ctx, "", nil, "", fsys); err != nil {
+		if err := serve.ServeFunctions(ctx, "", nil, "", w, fsys); err != nil {
 			return err
 		}
 		started = append(started, utils.DenoRelayId)
@@ -608,7 +608,7 @@ EOF
 	}
 
 	// Setup database after all services are up
-	return start.SetupDatabase(ctx, fsys, w, options...)
+	return start.SetupDatabase(ctx, fsys, os.Stderr, options...)
 }
 
 func isContainerExcluded(imageName string, excluded map[string]bool) bool {
diff --git a/internal/utils/docker.go b/internal/utils/docker.go
index fd81fcda..bfc3e69c 100644
--- a/internal/utils/docker.go
+++ b/internal/utils/docker.go
@@ -355,34 +355,43 @@ func DockerStreamLogs(ctx context.Context, container string, stdout, stderr io.W
 
 // Exec a command once inside a container, returning stdout and throwing error on non-zero exit code.
 func DockerExecOnce(ctx context.Context, container string, env []string, cmd []string) (string, error) {
+	stderr := io.Discard
+	if viper.GetBool("DEBUG") {
+		stderr = os.Stderr
+	}
+	var out bytes.Buffer
+	err := DockerExecOnceWithStream(ctx, container, env, cmd, &out, stderr)
+	return out.String(), err
+}
+
+func DockerExecOnceWithStream(ctx context.Context, container string, env, cmd []string, stdout, stderr io.Writer) error {
 	// Reset shadow database
 	exec, err := Docker.ContainerExecCreate(ctx, container, types.ExecConfig{
 		Env:          env,
 		Cmd:          cmd,
-		AttachStderr: viper.GetBool("DEBUG"),
+		AttachStderr: true,
 		AttachStdout: true,
 	})
 	if err != nil {
-		return "", err
+		return err
 	}
 	// Read exec output
 	resp, err := Docker.ContainerExecAttach(ctx, exec.ID, types.ExecStartCheck{})
 	if err != nil {
-		return "", err
+		return err
 	}
 	defer resp.Close()
 	// Capture error details
-	var out bytes.Buffer
-	if _, err := stdcopy.StdCopy(&out, os.Stderr, resp.Reader); err != nil {
-		return "", err
+	if _, err := stdcopy.StdCopy(stdout, stderr, resp.Reader); err != nil {
+		return err
 	}
 	// Get the exit code
 	iresp, err := Docker.ContainerExecInspect(ctx, exec.ID)
 	if err != nil {
-		return "", err
+		return err
 	}
 	if iresp.ExitCode > 0 {
 		err = errors.New("error executing command")
 	}
-	return out.String(), err
+	return err
 }
diff --git a/internal/utils/misc.go b/internal/utils/misc.go
index 6b0a6f4e..31b816c9 100644
--- a/internal/utils/misc.go
+++ b/internal/utils/misc.go
@@ -160,6 +160,7 @@ var (
 		"pgsodium_keyholder",
 		"pgsodium_keyiduser",
 		"pgsodium_keymaker",
+		"pgtle_admin",
 	}
 	AllowedConfigs = []string{
 		// Ref: https://github.com/supabase/postgres/blob/develop/ansible/files/postgresql_config/supautils.conf.j2#L10


Test patch:
diff --git a/internal/db/test/test.go b/internal/db/test/test.go
index 54d19281..fe793000 100644
--- a/internal/db/test/test.go
+++ b/internal/db/test/test.go
@@ -5,7 +5,6 @@ import (
 	"bytes"
 	"context"
 	_ "embed"
-	"fmt"
 	"io"
 	"os"
 	"path"
@@ -51,9 +50,7 @@ func pgProve(ctx context.Context, dstPath string, fsys afero.Fs) error {
 	args := "set -- " + testsPath + ";"
 	// Requires unix path inside container
 	cmd := []string{"/bin/bash", "-c", args + testScript}
-	out, err := utils.DockerExecOnce(ctx, utils.DbId, nil, cmd)
-	fmt.Print(out)
-	return err
+	return utils.DockerExecOnceWithStream(ctx, utils.DbId, nil, cmd, os.Stdout, os.Stderr)
 }
 
 // Ref 1: https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07