@@ -11,13 +11,16 @@ import (
11
11
"path/filepath"
12
12
"strings"
13
13
"testing"
14
+ "time"
14
15
16
+ "github.com/docker/docker/api/types"
15
17
"github.com/jackc/pgconn"
16
18
"github.com/jackc/pgerrcode"
17
19
"github.com/jackc/pgx/v4"
18
20
"github.com/spf13/afero"
19
21
"github.com/stretchr/testify/assert"
20
22
"github.com/stretchr/testify/require"
23
+ "github.com/supabase/cli/internal/db/start"
21
24
"github.com/supabase/cli/internal/migration/history"
22
25
"github.com/supabase/cli/internal/migration/repair"
23
26
"github.com/supabase/cli/internal/testing/apitest"
@@ -51,6 +54,15 @@ func TestSquashCommand(t *testing.T) {
51
54
require .NoError (t , apitest .MockDocker (utils .Docker ))
52
55
defer gock .OffAll ()
53
56
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
+ }})
54
66
gock .New (utils .Docker .DaemonHost ()).
55
67
Delete ("/v" + utils .Docker .ClientVersion () + "/containers/test-shadow-db" ).
56
68
Reply (http .StatusOK )
@@ -194,13 +206,43 @@ func TestSquashMigrations(t *testing.T) {
194
206
assert .Empty (t , apitest .ListUnmatchedRequests ())
195
207
})
196
208
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
+
197
230
t .Run ("throws error on shadow migrate failure" , func (t * testing.T ) {
198
231
// Setup in-memory fs
199
232
fsys := afero .NewMemMapFs ()
200
233
// Setup mock docker
201
234
require .NoError (t , apitest .MockDocker (utils .Docker ))
202
235
defer gock .OffAll ()
203
236
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
+ }})
204
246
gock .New (utils .Docker .DaemonHost ()).
205
247
Delete ("/v" + utils .Docker .ClientVersion () + "/containers/test-shadow-db" ).
206
248
Reply (http .StatusOK )
@@ -227,6 +269,15 @@ func TestSquashMigrations(t *testing.T) {
227
269
require .NoError (t , apitest .MockDocker (utils .Docker ))
228
270
defer gock .OffAll ()
229
271
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
+ }})
230
281
gock .New (utils .Docker .DaemonHost ()).
231
282
Delete ("/v" + utils .Docker .ClientVersion () + "/containers/test-shadow-db" ).
232
283
Reply (http .StatusOK )
0 commit comments