Repository: supabase/cli
Pull request #914 (2023-03-07 05:02:16)

fix: db tests path in windows container
## What kind of change does this PR introduce?

Bug fix https://github.com/supabase/cli/issues/843

## What is the current behavior?

`filepath.Join` returns back slash on windows

## What is the new behavior?

Always construct path using forward slash inside container.

## Additional context

Add any other context or screenshots.


coveralls: ## Pull Request Test Coverage Report for [Build 4350822215](https://coveralls.io/builds/57598506)

* **2** of **2**   **(100.0%)**  changed or added relevant lines in **1** file are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage increased (+**0.006%**) to **62.737%**

---



|  Totals | [![Coverage Status](https://coveralls.io/builds/57598506/badge)](https://coveralls.io/builds/57598506) |
| :-- | --: |
| Change from base [Build 4350678145](https://coveralls.io/builds/57597915): |  0.006% |
| Covered Lines: | 3805 |
| Relevant Lines: | 6065 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Patch:
diff --git a/internal/utils/connect.go b/internal/utils/connect.go
index 93376f21..f61cccee 100644
--- a/internal/utils/connect.go
+++ b/internal/utils/connect.go
@@ -52,7 +52,10 @@ func ConnectRemotePostgres(ctx context.Context, config pgconn.Config, options ..
 	})
 	// Use port 6543 for connection pooling
 	conn, err := ConnectByUrl(ctx, ToPostgresURL(config), opts...)
-	if !pgconn.Timeout(err) && !isDialError(err) && !ProjectHostPattern.MatchString(config.Host) {
+	if !pgconn.Timeout(err) && !isDialError(err) {
+		return conn, err
+	}
+	if !ProjectHostPattern.MatchString(config.Host) || config.Port != 6543 {
 		return conn, err
 	}
 	// Fallback to 5432 when pgbouncer is unavailable


Test patch:
diff --git a/internal/db/test/test.go b/internal/db/test/test.go
index 23b47b5c..54d19281 100644
--- a/internal/db/test/test.go
+++ b/internal/db/test/test.go
@@ -8,6 +8,7 @@ import (
 	"fmt"
 	"io"
 	"os"
+	"path"
 	"path/filepath"
 
 	"github.com/docker/docker/api/types"
@@ -46,7 +47,8 @@ func pgProve(ctx context.Context, dstPath string, fsys afero.Fs) error {
 	}
 
 	// Passing in script string means command line args must be set manually, ie. "$@"
-	args := "set -- " + dstPath + "/" + utils.DbTestsDir + ";"
+	testsPath := path.Join(dstPath, filepath.Dir(utils.DbTestsDir), filepath.Base(utils.DbTestsDir))
+	args := "set -- " + testsPath + ";"
 	// Requires unix path inside container
 	cmd := []string{"/bin/bash", "-c", args + testScript}
 	out, err := utils.DockerExecOnce(ctx, utils.DbId, nil, cmd)
diff --git a/internal/utils/connect_test.go b/internal/utils/connect_test.go
index 50d122ba..b4c185e2 100644
--- a/internal/utils/connect_test.go
+++ b/internal/utils/connect_test.go
@@ -15,8 +15,8 @@ import (
 )
 
 var dbConfig = pgconn.Config{
-	Host:     "localhost",
-	Port:     5432,
+	Host:     GetSupabaseDbHost(apitest.RandomProjectRef()),
+	Port:     6543,
 	User:     "admin",
 	Password: "password",
 	Database: "postgres",
@@ -49,12 +49,11 @@ func TestConnectRemotePostgres(t *testing.T) {
 
 	t.Run("fallback to postgres port on timeout", func(t *testing.T) {
 		DNSResolver.Value = DNS_OVER_HTTPS
-		const host = "localhost"
 		// Setup http mock
 		defer gock.OffAll()
 		gock.New("https://1.1.1.1").
 			Get("/dns-query").
-			MatchParam("name", host).
+			MatchParam("name", dbConfig.Host).
 			MatchHeader("accept", "application/dns-json").
 			Reply(http.StatusOK).
 			JSON(&dnsResponse{Answer: []dnsAnswer{
@@ -62,7 +61,7 @@ func TestConnectRemotePostgres(t *testing.T) {
 			}})
 		gock.New("https://1.1.1.1").
 			Get("/dns-query").
-			MatchParam("name", host).
+			MatchParam("name", dbConfig.Host).
 			MatchHeader("accept", "application/dns-json").
 			Reply(http.StatusOK).
 			JSON(&dnsResponse{Answer: []dnsAnswer{
@@ -71,7 +70,7 @@ func TestConnectRemotePostgres(t *testing.T) {
 		// pgx makes 2 calls to resolve ip for each connect request
 		gock.New("https://1.1.1.1").
 			Get("/dns-query").
-			MatchParam("name", host).
+			MatchParam("name", dbConfig.Host).
 			MatchHeader("accept", "application/dns-json").
 			Reply(http.StatusOK).
 			JSON(&dnsResponse{Answer: []dnsAnswer{
@@ -79,7 +78,7 @@ func TestConnectRemotePostgres(t *testing.T) {
 			}})
 		gock.New("https://1.1.1.1").
 			Get("/dns-query").
-			MatchParam("name", host).
+			MatchParam("name", dbConfig.Host).
 			MatchHeader("accept", "application/dns-json").
 			Reply(http.StatusOK).
 			JSON(&dnsResponse{Answer: []dnsAnswer{