Skip to content

Commit

Permalink
fix: use host network only when connecting to daemon host
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Jun 3, 2024
1 parent 36b8285 commit 5db7179
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion internal/db/diff/migra.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func DiffSchemaMigra(ctx context.Context, source, target string, schema []string
Cmd: cmd,
},
container.HostConfig{
NetworkMode: container.NetworkMode("host"),
NetworkMode: network.NetworkHost,
},
network.NetworkingConfig{},
"",
Expand Down
14 changes: 10 additions & 4 deletions internal/db/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ func Run(ctx context.Context, path string, config pgconn.Config, schema, exclude
if dryRun {
fmt.Fprintln(os.Stderr, "DRY RUN: *only* printing the pg_dump script to console.")
}
db := "remote"
if utils.IsLocalDatabase(config) {
config.Host = utils.DbAliases[0]
config.Port = 5432
}
db := "remote"
if config.Host == utils.Config.Hostname {
db = "local"
}
if dataOnly {
Expand Down Expand Up @@ -166,16 +170,18 @@ func dump(ctx context.Context, config pgconn.Config, script string, env []string
fmt.Println(expanded)
return nil
}
hostConfig := container.HostConfig{}
if config.Host == utils.Config.Hostname {
hostConfig.NetworkMode = network.NetworkHost
}
return utils.DockerRunOnceWithConfig(
ctx,
container.Config{
Image: utils.Pg15Image,
Env: allEnvs,
Cmd: []string{"bash", "-c", script, "--"},
},
container.HostConfig{
NetworkMode: container.NetworkMode("host"),
},
hostConfig,
network.NetworkingConfig{},
"",
stdout,
Expand Down
11 changes: 5 additions & 6 deletions internal/db/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ func Run(ctx context.Context, testFiles []string, config pgconn.Config, fsys afe
}()
}
// Use custom network when connecting to local database
networkID := "host"
if utils.IsLocalDatabase(config) {
config.Host = utils.DbAliases[0]
config.Port = 5432
networkID = utils.NetId
}
hostConfig := container.HostConfig{Binds: binds}
if config.Host == utils.Config.Hostname {
hostConfig.NetworkMode = network.NetworkHost
}
// Run pg_prove on volume mount
return utils.DockerRunOnceWithConfig(
Expand All @@ -87,10 +89,7 @@ func Run(ctx context.Context, testFiles []string, config pgconn.Config, fsys afe
Cmd: cmd,
WorkingDir: dstPath,
},
container.HostConfig{
NetworkMode: container.NetworkMode(networkID),
Binds: binds,
},
hostConfig,
network.NetworkingConfig{},
"",
os.Stdout,
Expand Down
10 changes: 5 additions & 5 deletions internal/gen/types/typescript/typescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ 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 @@ -53,7 +52,6 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, schemas
// 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 {
Expand All @@ -69,6 +67,10 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, schemas
escaped += "&sslmode=require"
}

hostConfig := container.HostConfig{}
if dbConfig.Host == utils.Config.Hostname {
hostConfig.NetworkMode = network.NetworkHost
}
return utils.DockerRunOnceWithConfig(
ctx,
container.Config{
Expand All @@ -81,9 +83,7 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, schemas
},
Cmd: []string{"node", "dist/server/server.js"},
},
container.HostConfig{
NetworkMode: container.NetworkMode(networkID),
},
hostConfig,
network.NetworkingConfig{},
"",
os.Stdout,
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func DockerStart(ctx context.Context, config container.Config, hostConfig contai
hostConfig.NetworkMode = container.NetworkMode(NetId)
}
// Create network with name
if hostConfig.NetworkMode.IsUserDefined() && hostConfig.NetworkMode.UserDefined() != "host" {
if hostConfig.NetworkMode.IsUserDefined() && hostConfig.NetworkMode.UserDefined() != network.NetworkHost {
if err := DockerNetworkCreateIfNotExists(ctx, hostConfig.NetworkMode.NetworkName()); err != nil {
return "", err
}
Expand Down

0 comments on commit 5db7179

Please sign in to comment.