Skip to content

Commit

Permalink
fix: use custom network for local database when starting pg-meta (#2064)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Han Qiao <[email protected]>
Co-authored-by: Qiao Han <[email protected]>
  • Loading branch information
4 people authored Mar 21, 2024
1 parent 58e86b4 commit efa43f3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
15 changes: 11 additions & 4 deletions internal/gen/types/typescript/typescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

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

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

fmt.Fprintln(os.Stderr, "Connecting to", dbConfig.Host, dbConfig.Port)
// Use custom network when connecting to local database
dbConfig.Host = utils.DbAliases[0]
dbConfig.Port = 5432
networkID = utils.NetId
}
// pg-meta does not set username as the default database, ie. postgres
if len(dbConfig.Database) == 0 {
dbConfig.Database = "postgres"
}

fmt.Fprintln(os.Stderr, "Connecting to", dbConfig.Host, dbConfig.Port)
escaped := utils.ToPostgresURL(dbConfig)
if require, err := isRequireSSL(ctx, escaped, options...); err != nil {
if require, err := isRequireSSL(ctx, originalURL, options...); err != nil {
return err
} else if require {
// node-postgres does not support sslmode=prefer
Expand All @@ -75,7 +82,7 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, schemas
Cmd: []string{"node", "dist/server/server.js"},
},
container.HostConfig{
NetworkMode: container.NetworkMode("host"),
NetworkMode: container.NetworkMode(networkID),
},
network.NetworkingConfig{},
"",
Expand Down
6 changes: 5 additions & 1 deletion internal/testing/pgtest/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ type MockConn struct {
func (r *MockConn) getStartupMessage(config *pgx.ConnConfig) []pgmock.Step {
var steps []pgmock.Step
// Add auth message
params := map[string]string{"user": config.User}
if len(config.Database) > 0 {
params["database"] = config.Database
}
steps = append(
steps,
pgmock.ExpectMessage(&pgproto3.StartupMessage{
ProtocolVersion: pgproto3.ProtocolVersionNumber,
Parameters: map[string]string{"database": config.Database, "user": config.User},
Parameters: params,
}),
pgmock.SendMessage(&pgproto3.AuthenticationOk{}),
)
Expand Down

0 comments on commit efa43f3

Please sign in to comment.