Skip to content

Commit e013933

Browse files
authored
fix: wait for healthy database on squash (#2161)
1 parent c0e9c59 commit e013933

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

internal/migration/squash/squash.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ func squashMigrations(ctx context.Context, migrations []string, fsys afero.Fs, o
8585
return err
8686
}
8787
defer utils.DockerRemove(shadow)
88+
if !start.WaitForHealthyService(ctx, shadow, start.HealthTimeout) {
89+
return errors.New(start.ErrDatabase)
90+
}
8891
conn, err := diff.ConnectShadowDatabase(ctx, 10*time.Second, options...)
8992
if err != nil {
9093
return err

internal/migration/squash/squash_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ import (
1111
"path/filepath"
1212
"strings"
1313
"testing"
14+
"time"
1415

16+
"github.com/docker/docker/api/types"
1517
"github.com/jackc/pgconn"
1618
"github.com/jackc/pgerrcode"
1719
"github.com/jackc/pgx/v4"
1820
"github.com/spf13/afero"
1921
"github.com/stretchr/testify/assert"
2022
"github.com/stretchr/testify/require"
23+
"github.com/supabase/cli/internal/db/start"
2124
"github.com/supabase/cli/internal/migration/history"
2225
"github.com/supabase/cli/internal/migration/repair"
2326
"github.com/supabase/cli/internal/testing/apitest"
@@ -51,6 +54,15 @@ func TestSquashCommand(t *testing.T) {
5154
require.NoError(t, apitest.MockDocker(utils.Docker))
5255
defer gock.OffAll()
5356
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Pg15Image), "test-shadow-db")
57+
gock.New(utils.Docker.DaemonHost()).
58+
Get("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db/json").
59+
Reply(http.StatusOK).
60+
JSON(types.ContainerJSON{ContainerJSONBase: &types.ContainerJSONBase{
61+
State: &types.ContainerState{
62+
Running: true,
63+
Health: &types.Health{Status: "healthy"},
64+
},
65+
}})
5466
gock.New(utils.Docker.DaemonHost()).
5567
Delete("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db").
5668
Reply(http.StatusOK)
@@ -194,13 +206,43 @@ func TestSquashMigrations(t *testing.T) {
194206
assert.Empty(t, apitest.ListUnmatchedRequests())
195207
})
196208

209+
t.Run("throws error on health check failure", func(t *testing.T) {
210+
start.HealthTimeout = time.Millisecond
211+
// Setup in-memory fs
212+
fsys := afero.NewMemMapFs()
213+
// Setup mock docker
214+
require.NoError(t, apitest.MockDocker(utils.Docker))
215+
defer gock.OffAll()
216+
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.Db.Image), "test-shadow-db")
217+
gock.New(utils.Docker.DaemonHost()).
218+
Get("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db/json").
219+
Reply(http.StatusServiceUnavailable)
220+
gock.New(utils.Docker.DaemonHost()).
221+
Delete("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db").
222+
Reply(http.StatusOK)
223+
// Run test
224+
err := squashMigrations(context.Background(), nil, fsys)
225+
// Check error
226+
assert.ErrorIs(t, err, start.ErrDatabase)
227+
assert.Empty(t, apitest.ListUnmatchedRequests())
228+
})
229+
197230
t.Run("throws error on shadow migrate failure", func(t *testing.T) {
198231
// Setup in-memory fs
199232
fsys := afero.NewMemMapFs()
200233
// Setup mock docker
201234
require.NoError(t, apitest.MockDocker(utils.Docker))
202235
defer gock.OffAll()
203236
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.Db.Image), "test-shadow-db")
237+
gock.New(utils.Docker.DaemonHost()).
238+
Get("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db/json").
239+
Reply(http.StatusOK).
240+
JSON(types.ContainerJSON{ContainerJSONBase: &types.ContainerJSONBase{
241+
State: &types.ContainerState{
242+
Running: true,
243+
Health: &types.Health{Status: "healthy"},
244+
},
245+
}})
204246
gock.New(utils.Docker.DaemonHost()).
205247
Delete("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db").
206248
Reply(http.StatusOK)
@@ -227,6 +269,15 @@ func TestSquashMigrations(t *testing.T) {
227269
require.NoError(t, apitest.MockDocker(utils.Docker))
228270
defer gock.OffAll()
229271
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.Db.Image), "test-shadow-db")
272+
gock.New(utils.Docker.DaemonHost()).
273+
Get("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db/json").
274+
Reply(http.StatusOK).
275+
JSON(types.ContainerJSON{ContainerJSONBase: &types.ContainerJSONBase{
276+
State: &types.ContainerState{
277+
Running: true,
278+
Health: &types.Health{Status: "healthy"},
279+
},
280+
}})
230281
gock.New(utils.Docker.DaemonHost()).
231282
Delete("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db").
232283
Reply(http.StatusOK)

0 commit comments

Comments
 (0)