Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ var (
excludedContainers []string
ignoreHealthCheck bool
preview bool
skipSeed bool

startCmd = &cobra.Command{
GroupID: groupLocalDev,
Use: "start",
Short: "Start containers for Supabase local development",
RunE: func(cmd *cobra.Command, args []string) error {
validateExcludedContainers(excludedContainers)
return start.Run(cmd.Context(), afero.NewOsFs(), excludedContainers, ignoreHealthCheck)
return start.Run(cmd.Context(), afero.NewOsFs(), excludedContainers, ignoreHealthCheck, skipSeed)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return start.Run(cmd.Context(), afero.NewOsFs(), excludedContainers, ignoreHealthCheck, skipSeed)
fsys := afero.NewOsFs()
if err := flags.LoadConfig(fsys); err != nil {
return err
}
if noSeed {
utils.Config.Db.Seed.Enabled = false
}
return start.Run(cmd.Context(), fsys, excludedContainers, ignoreHealthCheck)

I'd suggest we apply an override to the loaded config struct similar to https://github.com/supabase/cli/blob/develop/cmd/db.go#L200-L202

},
}
)
Expand All @@ -58,6 +59,7 @@ func init() {
flags.StringSliceVarP(&excludedContainers, "exclude", "x", []string{}, "Names of containers to not start. ["+names+"]")
flags.BoolVar(&ignoreHealthCheck, "ignore-health-check", false, "Ignore unhealthy services and exit 0")
flags.BoolVar(&preview, "preview", false, "Connect to feature preview branch")
flags.BoolVar(&skipSeed, "no-seed", false, "Skip storage seeding on startup")
cobra.CheckErr(flags.MarkHidden("preview"))
rootCmd.AddCommand(startCmd)
}
22 changes: 15 additions & 7 deletions internal/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"github.com/supabase/cli/pkg/config"
)

func Run(ctx context.Context, fsys afero.Fs, excludedContainers []string, ignoreHealthCheck bool) error {
func Run(ctx context.Context, fsys afero.Fs, excludedContainers []string, ignoreHealthCheck bool, skipSeed bool) error {
// Sanity checks.
{
if err := flags.LoadConfig(fsys); err != nil {
Expand All @@ -68,7 +68,7 @@
Password: utils.Config.Db.Password,
Database: "postgres",
}
if err := run(ctx, fsys, excludedContainers, dbConfig); err != nil {
if err := run(ctx, fsys, excludedContainers, dbConfig, skipSeed); err != nil {
if ignoreHealthCheck && start.IsUnhealthyError(err) {
fmt.Fprintln(os.Stderr, err)
} else {
Expand Down Expand Up @@ -212,7 +212,7 @@
return service.Pull(ctx, &project, api.PullOptions{IgnoreFailures: true})
}

func run(ctx context.Context, fsys afero.Fs, excludedContainers []string, dbConfig pgconn.Config, options ...func(*pgx.ConnConfig)) error {
func run(ctx context.Context, fsys afero.Fs, excludedContainers []string, dbConfig pgconn.Config, skipSeed bool, options ...func(*pgx.ConnConfig)) error {
excluded := make(map[string]bool)
for _, name := range excludedContainers {
excluded[name] = true
Expand Down Expand Up @@ -249,7 +249,6 @@
utils.Config.Storage.ImageTransformation.Enabled && !isContainerExcluded(utils.Config.Storage.ImgProxyImage, excluded)
isS3ProtocolEnabled := utils.Config.Storage.S3Protocol != nil && utils.Config.Storage.S3Protocol.Enabled
fmt.Fprintln(os.Stderr, "Starting containers...")

workdir, err := os.Getwd()
if err != nil {
return errors.Errorf("failed to get working directory: %w", err)
Expand Down Expand Up @@ -1278,16 +1277,25 @@
}

fmt.Fprintln(os.Stderr, "Waiting for health checks...")

if utils.NoBackupVolume && slices.Contains(started, utils.StorageId) {
if err := start.WaitForHealthyService(ctx, serviceTimeout, utils.StorageId); err != nil {
return err
}
// Disable prompts when seeding
if err := buckets.Run(ctx, "", false, fsys); err != nil {
return err

// Follow db.seed.enabled by default, allow --no-seed as override
if skipSeed {
fmt.Fprintln(os.Stderr, "Skipping storage seeding (--no-seed enabled)")
} else if !utils.Config.Db.Seed.Enabled {
fmt.Fprintln(os.Stderr, "Skipping storage seeding (db.seed.enabled = false)")
} else {
if err := buckets.Run(ctx, "", false, fsys); err != nil {
return err
}
}

Comment on lines +1285 to +1296
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Follow db.seed.enabled by default, allow --no-seed as override
if skipSeed {
fmt.Fprintln(os.Stderr, "Skipping storage seeding (--no-seed enabled)")
} else if !utils.Config.Db.Seed.Enabled {
fmt.Fprintln(os.Stderr, "Skipping storage seeding (db.seed.enabled = false)")
} else {
if err := buckets.Run(ctx, "", false, fsys); err != nil {
return err
}
}
if !utils.Config.Db.Seed.Enabled {
fmt.Fprintln(os.Stderr, "Skipping storage seeding...")
} else if err := buckets.Run(ctx, "", false, fsys); err != nil {
return err
}

}
return start.WaitForHealthyService(ctx, serviceTimeout, started...)

Check failure on line 1298 in internal/start/start.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary trailing newline (whitespace)
}

func isContainerExcluded(imageName string, excluded map[string]bool) bool {
Expand Down
101 changes: 96 additions & 5 deletions internal/start/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
fsys := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fsys, utils.ConfigPath, []byte("malformed"), 0644))
// Run test
err := Run(context.Background(), fsys, []string{}, false)
err := Run(context.Background(), fsys, []string{}, false, false)
// Check error
assert.ErrorContains(t, err, "toml: expected = after a key, but the document ends there")
})
Expand All @@ -47,7 +47,7 @@
Get("/v" + utils.Docker.ClientVersion() + "/containers").
ReplyError(errors.New("network error"))
// Run test
err := Run(context.Background(), fsys, []string{}, false)
err := Run(context.Background(), fsys, []string{}, false, false)
// Check error
assert.ErrorContains(t, err, "network error")
assert.Empty(t, apitest.ListUnmatchedRequests())
Expand Down Expand Up @@ -84,7 +84,7 @@
Reply(http.StatusOK).
JSON(running)
// Run test
err := Run(context.Background(), fsys, []string{}, false)
err := Run(context.Background(), fsys, []string{}, false, false)
// Check error
assert.NoError(t, err)
assert.Empty(t, apitest.ListUnmatchedRequests())
Expand Down Expand Up @@ -202,7 +202,7 @@
Reply(http.StatusOK).
JSON([]storage.BucketResponse{})
// Run test
err := run(context.Background(), fsys, []string{}, pgconn.Config{Host: utils.DbId}, conn.Intercept)
err := run(context.Background(), fsys, []string{}, pgconn.Config{Host: utils.DbId}, false, conn.Intercept)
// Check error
assert.NoError(t, err)
assert.Empty(t, apitest.ListUnmatchedRequests())
Expand Down Expand Up @@ -248,12 +248,103 @@
// Run test
exclude := ExcludableContainers()
exclude = append(exclude, "invalid", exclude[0])
err := run(context.Background(), fsys, exclude, pgconn.Config{Host: utils.DbId})
err := run(context.Background(), fsys, exclude, pgconn.Config{Host: utils.DbId}, false)
// Check error
assert.NoError(t, err)
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("skips storage seeding when db.seed.enabled is false", func(t *testing.T) {
utils.Config.Analytics.Enabled = false
utils.Config.Api.Enabled = false
utils.Config.Auth.Enabled = false
utils.Config.Realtime.Enabled = false
utils.Config.Studio.Enabled = false
utils.Config.EdgeRuntime.Enabled = false
utils.Config.Inbucket.Enabled = false
utils.Config.Db.Pooler.Enabled = false

fsys := afero.NewMemMapFs()

// Restore global state
origSeed := utils.Config.Db.Seed.Enabled
t.Cleanup(func() {
utils.Config.Db.Seed.Enabled = origSeed
})
utils.Config.Db.Seed.Enabled = false

require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()

gock.New(utils.Docker.DaemonHost()).
Head("/_ping").
Reply(http.StatusOK)

gock.New(utils.Docker.DaemonHost()).
Post("/v" + utils.Docker.ClientVersion() + "/networks/create").
Reply(http.StatusCreated).
JSON(network.CreateResponse{})

// 🔑 REQUIRED: cache all images
for _, img := range config.Images.Services() {
service := utils.GetRegistryImageUrl(img)
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/images/" + service + "/json").
Reply(http.StatusOK).
JSON(image.InspectResponse{})
}

// ALSO mock postgres image (DB is not part of Services())
dbImage := utils.GetRegistryImageUrl(utils.Config.Db.Image)
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/images/" + dbImage + "/json").
Reply(http.StatusOK).
JSON(image.InspectResponse{})

utils.DbId = "test-postgres"
utils.Config.Db.Port = 54322
utils.Config.Db.MajorVersion = 15

gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/volumes/" + utils.DbId).
Reply(http.StatusOK).
JSON(volume.Volume{})

apitest.MockDockerStart(
utils.Docker,
utils.GetRegistryImageUrl(utils.Config.Db.Image),
utils.DbId,
)

gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/" + utils.DbId + "/json").
Reply(http.StatusOK).
JSON(container.InspectResponse{
ContainerJSONBase: &container.ContainerJSONBase{
State: &container.State{
Running: true,
Health: &container.Health{Status: types.Healthy},
},
},
})
exclude := []string{
utils.ShortContainerImageName(utils.Config.Api.KongImage),
utils.ShortContainerImageName(utils.Config.Storage.Image),
}

err := run(
context.Background(),
fsys,
exclude,
pgconn.Config{Host: utils.DbId},
false,
)

assert.NoError(t, err)
assert.Empty(t, apitest.ListUnmatchedRequests())
})

}

Check failure on line 347 in internal/start/start_test.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary trailing newline (whitespace)

func TestFormatMapForEnvConfig(t *testing.T) {
t.Run("It produces the correct format and removes the trailing comma", func(t *testing.T) {
Expand Down
Loading