Skip to content

Commit efa43f3

Browse files
etzelcsweatybridge
andauthored
fix: use custom network for local database when starting pg-meta (#2064)
* use custom network for local database * Update internal/gen/types/typescript/typescript.go * fix: save original url * chore: update mocks for new pgx --------- Co-authored-by: Christoph Etzel <christoph.etzel@smart-cybersecurity.de> Co-authored-by: Han Qiao <sweatybridge@gmail.com> Co-authored-by: Qiao Han <qiao@supabase.io>
1 parent 58e86b4 commit efa43f3

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

internal/gen/types/typescript/typescript.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
)
1818

1919
func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, schemas []string, postgrestV9Compat bool, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
20+
originalURL := utils.ToPostgresURL(dbConfig)
2021
// Add default schemas if --schema flag is not specified
2122
if len(schemas) == 0 {
2223
schemas = utils.RemoveDuplicates(append([]string{"public"}, utils.Config.Api.Schemas...))
@@ -39,6 +40,7 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, schemas
3940
return nil
4041
}
4142

43+
networkID := "host"
4244
if utils.IsLocalDatabase(dbConfig) {
4345
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
4446
return err
@@ -47,15 +49,20 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, schemas
4749
if strings.Contains(utils.Config.Api.Image, "v9") {
4850
postgrestV9Compat = true
4951
}
50-
}
5152

52-
fmt.Fprintln(os.Stderr, "Connecting to", dbConfig.Host, dbConfig.Port)
53+
// Use custom network when connecting to local database
54+
dbConfig.Host = utils.DbAliases[0]
55+
dbConfig.Port = 5432
56+
networkID = utils.NetId
57+
}
5358
// pg-meta does not set username as the default database, ie. postgres
5459
if len(dbConfig.Database) == 0 {
5560
dbConfig.Database = "postgres"
5661
}
62+
63+
fmt.Fprintln(os.Stderr, "Connecting to", dbConfig.Host, dbConfig.Port)
5764
escaped := utils.ToPostgresURL(dbConfig)
58-
if require, err := isRequireSSL(ctx, escaped, options...); err != nil {
65+
if require, err := isRequireSSL(ctx, originalURL, options...); err != nil {
5966
return err
6067
} else if require {
6168
// node-postgres does not support sslmode=prefer
@@ -75,7 +82,7 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, schemas
7582
Cmd: []string{"node", "dist/server/server.js"},
7683
},
7784
container.HostConfig{
78-
NetworkMode: container.NetworkMode("host"),
85+
NetworkMode: container.NetworkMode(networkID),
7986
},
8087
network.NetworkingConfig{},
8188
"",

internal/testing/pgtest/mock.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ type MockConn struct {
3232
func (r *MockConn) getStartupMessage(config *pgx.ConnConfig) []pgmock.Step {
3333
var steps []pgmock.Step
3434
// Add auth message
35+
params := map[string]string{"user": config.User}
36+
if len(config.Database) > 0 {
37+
params["database"] = config.Database
38+
}
3539
steps = append(
3640
steps,
3741
pgmock.ExpectMessage(&pgproto3.StartupMessage{
3842
ProtocolVersion: pgproto3.ProtocolVersionNumber,
39-
Parameters: map[string]string{"database": config.Database, "user": config.User},
43+
Parameters: params,
4044
}),
4145
pgmock.SendMessage(&pgproto3.AuthenticationOk{}),
4246
)

0 commit comments

Comments
 (0)