Skip to content

Commit ac1c59a

Browse files
authored
feat: allow configuring local health check timeout (#4699)
1 parent 05d91ef commit ac1c59a

File tree

11 files changed

+14
-14
lines changed

11 files changed

+14
-14
lines changed

internal/db/diff/diff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func DiffDatabase(ctx context.Context, schema []string, config pgconn.Config, w
143143
return "", err
144144
}
145145
defer utils.DockerRemove(shadow)
146-
if err := start.WaitForHealthyService(ctx, start.HealthTimeout, shadow); err != nil {
146+
if err := start.WaitForHealthyService(ctx, utils.Config.Db.HealthTimeout, shadow); err != nil {
147147
return "", err
148148
}
149149
if err := MigrateShadowDatabase(ctx, shadow, fsys, options...); err != nil {

internal/db/diff/diff_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/spf13/afero"
2020
"github.com/stretchr/testify/assert"
2121
"github.com/stretchr/testify/require"
22-
"github.com/supabase/cli/internal/db/start"
2322
"github.com/supabase/cli/internal/testing/apitest"
2423
"github.com/supabase/cli/internal/testing/fstest"
2524
"github.com/supabase/cli/internal/testing/helper"
@@ -212,7 +211,7 @@ func TestDiffDatabase(t *testing.T) {
212211
})
213212

214213
t.Run("throws error on health check failure", func(t *testing.T) {
215-
start.HealthTimeout = time.Millisecond
214+
utils.Config.Db.HealthTimeout = time.Millisecond
216215
// Setup in-memory fs
217216
fsys := afero.NewMemMapFs()
218217
// Setup mock docker

internal/db/diff/pgadmin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func run(p utils.Program, ctx context.Context, schema []string, config pgconn.Co
5858
return err
5959
}
6060
defer utils.DockerRemove(shadow)
61-
if err := start.WaitForHealthyService(ctx, start.HealthTimeout, shadow); err != nil {
61+
if err := start.WaitForHealthyService(ctx, utils.Config.Db.HealthTimeout, shadow); err != nil {
6262
return err
6363
}
6464
if err := MigrateShadowDatabase(ctx, shadow, fsys); err != nil {

internal/db/reset/reset.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func resetDatabase15(ctx context.Context, version string, fsys afero.Fs, options
131131
if _, err := utils.DockerStart(ctx, config, hostConfig, networkingConfig, utils.DbId); err != nil {
132132
return err
133133
}
134-
if err := start.WaitForHealthyService(ctx, start.HealthTimeout, utils.DbId); err != nil {
134+
if err := start.WaitForHealthyService(ctx, utils.Config.Db.HealthTimeout, utils.DbId); err != nil {
135135
return err
136136
}
137137
if err := start.SetupLocalDatabase(ctx, version, fsys, os.Stderr, options...); err != nil {
@@ -215,7 +215,7 @@ func RestartDatabase(ctx context.Context, w io.Writer) error {
215215
if err := utils.Docker.ContainerRestart(ctx, utils.DbId, container.StopOptions{}); err != nil {
216216
return errors.Errorf("failed to restart container: %w", err)
217217
}
218-
if err := start.WaitForHealthyService(ctx, start.HealthTimeout, utils.DbId); err != nil {
218+
if err := start.WaitForHealthyService(ctx, utils.Config.Db.HealthTimeout, utils.DbId); err != nil {
219219
return err
220220
}
221221
return restartServices(ctx)

internal/db/reset/reset_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"net/http"
88
"testing"
9-
"time"
109

1110
"github.com/docker/docker/api/types"
1211
"github.com/docker/docker/api/types/container"
@@ -16,7 +15,6 @@ import (
1615
"github.com/spf13/afero"
1716
"github.com/stretchr/testify/assert"
1817
"github.com/stretchr/testify/require"
19-
"github.com/supabase/cli/internal/db/start"
2018
"github.com/supabase/cli/internal/testing/apitest"
2119
"github.com/supabase/cli/internal/testing/fstest"
2220
"github.com/supabase/cli/internal/utils"
@@ -376,7 +374,6 @@ func TestRestartDatabase(t *testing.T) {
376374

377375
t.Run("throws error on health check timeout", func(t *testing.T) {
378376
utils.DbId = "test-reset"
379-
start.HealthTimeout = 0 * time.Second
380377
// Setup mock docker
381378
require.NoError(t, apitest.MockDocker(utils.Docker))
382379
defer gock.OffAll()

internal/db/start/start.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
)
3131

3232
var (
33-
HealthTimeout = 120 * time.Second
3433
//go:embed templates/schema.sql
3534
initialSchema string
3635
//go:embed templates/webhook.sql
@@ -177,7 +176,7 @@ EOF`}
177176
return err
178177
}
179178
// Ignore health check because restoring a large backup may take longer than 2 minutes
180-
if err := WaitForHealthyService(ctx, HealthTimeout, utils.DbId); err != nil && len(fromBackup) == 0 {
179+
if err := WaitForHealthyService(ctx, utils.Config.Db.HealthTimeout, utils.DbId); err != nil && len(fromBackup) == 0 {
181180
return err
182181
}
183182
// Initialize if we are on PG14 and there's no existing db volume

internal/migration/squash/squash.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func squashMigrations(ctx context.Context, migrations []string, fsys afero.Fs, o
8585
return err
8686
}
8787
defer utils.DockerRemove(shadow)
88-
if err := start.WaitForHealthyService(ctx, start.HealthTimeout, shadow); err != nil {
88+
if err := start.WaitForHealthyService(ctx, utils.Config.Db.HealthTimeout, shadow); err != nil {
8989
return err
9090
}
9191
conn, err := diff.ConnectShadowDatabase(ctx, 10*time.Second, options...)

internal/migration/squash/squash_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/spf13/afero"
2323
"github.com/stretchr/testify/assert"
2424
"github.com/stretchr/testify/require"
25-
"github.com/supabase/cli/internal/db/start"
2625
"github.com/supabase/cli/internal/migration/repair"
2726
"github.com/supabase/cli/internal/testing/apitest"
2827
"github.com/supabase/cli/internal/testing/fstest"
@@ -214,7 +213,7 @@ func TestSquashMigrations(t *testing.T) {
214213
})
215214

216215
t.Run("throws error on health check failure", func(t *testing.T) {
217-
start.HealthTimeout = time.Millisecond
216+
utils.Config.Db.HealthTimeout = time.Millisecond
218217
// Setup in-memory fs
219218
fsys := afero.NewMemMapFs()
220219
// Setup mock docker

pkg/config/db.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"bytes"
55
"slices"
6+
"time"
67

78
"github.com/go-errors/errors"
89
v1API "github.com/supabase/cli/pkg/api"
@@ -78,6 +79,7 @@ type (
7879
Image string `toml:"-"`
7980
Port uint16 `toml:"port"`
8081
ShadowPort uint16 `toml:"shadow_port"`
82+
HealthTimeout time.Duration `toml:"health_timeout"`
8183
MajorVersion uint `toml:"major_version"`
8284
Password string `toml:"-"`
8385
RootKey Secret `toml:"root_key"`

pkg/config/templates/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ enabled = false
2929
port = 54322
3030
# Port used by db diff command to initialize the shadow database.
3131
shadow_port = 54320
32+
# Maximum amount of time to wait for health check when starting the local database.
33+
health_timeout = "2m"
3234
# The database major version to use. This has to be the same as your remote database's. Run `SHOW
3335
# server_version;` on the remote database to check.
3436
major_version = 17

0 commit comments

Comments
 (0)