Skip to content

Commit

Permalink
fix(db): reset command attempting to restart disabled services
Browse files Browse the repository at this point in the history
Fixes #2658
  • Loading branch information
avallete committed Sep 9, 2024
1 parent de3fe90 commit 3dd5b93
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
32 changes: 26 additions & 6 deletions internal/db/reset/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ func Run(ctx context.Context, version string, config pgconn.Config, fsys afero.F
return err
}
// Seed objects from supabase/buckets directory
if err := start.WaitForHealthyService(ctx, 30*time.Second, utils.StorageId); err != nil {
return err
}
if err := buckets.Run(ctx, "", false, fsys); err != nil {
return err
if utils.Config.Storage.Enabled {
if err := start.WaitForHealthyService(ctx, 30*time.Second, utils.StorageId); err != nil {
return err
}
if err := buckets.Run(ctx, "", false, fsys); err != nil {
return err
}
}
branch := keys.GetGitBranch(fsys)
fmt.Fprintln(os.Stderr, "Finished "+utils.Aqua("supabase db reset")+" on branch "+utils.Aqua(branch)+".")
Expand Down Expand Up @@ -204,8 +206,11 @@ func RestartDatabase(ctx context.Context, w io.Writer) error {
}

func restartServices(ctx context.Context) error {
debug := utils.GetDebugLogger()
// No need to restart PostgREST because it automatically reconnects and listens for schema changes
services := listServicesToRestart()
fmt.Fprintf(debug, "Enabled services to restart: %v\n", services)

result := utils.WaitAll(services, func(id string) error {
if err := utils.Docker.ContainerRestart(ctx, id, container.StopOptions{}); err != nil && !errdefs.IsNotFound(err) {
return errors.Errorf("Failed to restart %s: %w", id, err)
Expand All @@ -217,7 +222,22 @@ func restartServices(ctx context.Context) error {
}

func listServicesToRestart() []string {
return []string{utils.StorageId, utils.GotrueId, utils.RealtimeId, utils.PoolerId}
var services []string

if utils.Config.Storage.Enabled {
services = append(services, utils.StorageId)
}
if utils.Config.Realtime.Enabled {
services = append(services, utils.RealtimeId)
}
if utils.Config.Db.Pooler.Enabled {
services = append(services, utils.PoolerId)
}
if utils.Config.Auth.Enabled {
services = append(services, utils.GotrueId)
}

return services
}

func resetRemote(ctx context.Context, version string, config pgconn.Config, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
Expand Down
38 changes: 38 additions & 0 deletions internal/db/reset/reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,44 @@ func TestRestartDatabase(t *testing.T) {
assert.ErrorContains(t, err, "test-reset container is not running: exited")
assert.Empty(t, apitest.ListUnmatchedRequests())
})
t.Run("restarts only enabled services", func(t *testing.T) {
utils.DbId = "test-reset"
utils.Config.Storage.Enabled = false
utils.Config.Auth.Enabled = true
utils.Config.Realtime.Enabled = true
utils.Config.Db.Pooler.Enabled = false
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
// Restarts postgres
gock.New(utils.Docker.DaemonHost()).
Post("/v" + utils.Docker.ClientVersion() + "/containers/" + utils.DbId + "/restart").
Reply(http.StatusOK)
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/" + utils.DbId + "/json").
Reply(http.StatusOK).
JSON(types.ContainerJSON{ContainerJSONBase: &types.ContainerJSONBase{
State: &types.ContainerState{
Running: true,
Health: &types.Health{Status: "healthy"},
},
}})
// Restarts enabled services
utils.StorageId = "test-storage"
utils.PoolerId = "test-pooler"
utils.GotrueId = "test-auth"
utils.RealtimeId = "test-realtime"
for _, container := range []string{utils.GotrueId, utils.RealtimeId} {
gock.New(utils.Docker.DaemonHost()).
Post("/v" + utils.Docker.ClientVersion() + "/containers/" + container + "/restart").
Reply(http.StatusOK)
}
// Run test
err := RestartDatabase(context.Background(), io.Discard)
// Check error
assert.NoError(t, err)
assert.Empty(t, apitest.ListUnmatchedRequests())
})
}

var escapedSchemas = append(migration.ManagedSchemas, "extensions", "public")
Expand Down

0 comments on commit 3dd5b93

Please sign in to comment.