Skip to content

Commit

Permalink
fix: unmarshal remote override into base config (#3028)
Browse files Browse the repository at this point in the history
* fix: unmarshal remote override into base config

* chore: account for project ref flag when loading config

* fix: restore project id from base config
  • Loading branch information
sweatybridge authored Jan 10, 2025
1 parent 9898451 commit 88cd609
Show file tree
Hide file tree
Showing 31 changed files with 124 additions and 127 deletions.
4 changes: 1 addition & 3 deletions cmd/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/supabase/cli/internal/link"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"golang.org/x/term"
)
Expand All @@ -31,8 +30,7 @@ var (
return err
}
fsys := afero.NewOsFs()
utils.Config.ProjectId = flags.ProjectRef
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}
return link.Run(ctx, flags.ProjectRef, fsys)
Expand Down
2 changes: 1 addition & 1 deletion internal/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func Run(ctx context.Context, starter StarterTemplate, fsys afero.Fs, options ..
return err
}
// 4. Link project
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}
link.LinkServices(ctx, flags.ProjectRef, tenant.NewApiKey(keys).Anon, fsys)
Expand Down
10 changes: 7 additions & 3 deletions internal/config/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ import (

"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/config"
)

func Run(ctx context.Context, ref string, fsys afero.Fs) error {
utils.Config.ProjectId = ref
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}
client := config.NewConfigUpdater(*utils.GetSupabase())
remote, _ := utils.Config.GetRemoteByProjectRef(ref)
remote, err := utils.Config.GetRemoteByProjectRef(ref)
if err != nil {
// Use base config when no remote is declared
remote.ProjectId = ref
}
fmt.Fprintln(os.Stderr, "Pushing config to project:", remote.ProjectId)
console := utils.NewConsole()
keep := func(name string) bool {
Expand Down
3 changes: 2 additions & 1 deletion internal/db/branch/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
)

var (
Expand All @@ -22,7 +23,7 @@ var (
)

func Run(branch string, fsys afero.Fs) error {
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/db/branch/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
)

func Run(branch string, fsys afero.Fs) error {
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/db/branch/switch_/switch_.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (
"github.com/spf13/afero"
"github.com/supabase/cli/internal/db/reset"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
)

func Run(ctx context.Context, target string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
// 1. Sanity checks
{
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/db/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/supabase/cli/internal/db/start"
"github.com/supabase/cli/internal/gen/keys"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/migration"
"github.com/supabase/cli/pkg/parser"
)
Expand All @@ -32,7 +33,7 @@ type DiffFunc func(context.Context, string, string, []string) (string, error)

func Run(ctx context.Context, schema []string, file string, config pgconn.Config, differ DiffFunc, fsys afero.Fs, options ...func(*pgx.ConnConfig)) (err error) {
// Sanity checks.
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}
if utils.IsLocalDatabase(config) {
Expand Down
3 changes: 2 additions & 1 deletion internal/db/diff/pgadmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/supabase/cli/internal/db/start"
"github.com/supabase/cli/internal/migration/new"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/config"
)

Expand All @@ -35,7 +36,7 @@ func SaveDiff(out, file string, fsys afero.Fs) error {
func RunPgAdmin(ctx context.Context, schema []string, file string, config pgconn.Config, fsys afero.Fs) error {
// Sanity checks.
{
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/db/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/supabase/cli/internal/migration/new"
"github.com/supabase/cli/internal/migration/repair"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/migration"
)

Expand All @@ -35,7 +36,7 @@ var (

func Run(ctx context.Context, schema []string, config pgconn.Config, name string, fsys afero.Fs, options ...func(*pgx.ConnConfig)) error {
// 1. Sanity checks.
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}
// 2. Check postgres connection
Expand Down
7 changes: 4 additions & 3 deletions internal/db/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ func Run(ctx context.Context, dryRun, ignoreVersionMismatch bool, includeRoles,
}
var seeds []migration.SeedFile
if includeSeed {
if remote, _ := utils.Config.GetRemoteByProjectRef(flags.ProjectRef); !remote.Db.Seed.Enabled {
fmt.Fprintln(os.Stderr, "Skipping seed because it is disabled in config.toml for project:", remote.ProjectId)
} else if seeds, err = migration.GetPendingSeeds(ctx, remote.Db.Seed.SqlPaths, conn, afero.NewIOFS(fsys)); err != nil {
// TODO: flag should override config but we don't resolve glob paths when seed is disabled.
if !utils.Config.Db.Seed.Enabled {
fmt.Fprintln(os.Stderr, "Skipping seed because it is disabled in config.toml for project:", flags.ProjectRef)
} else if seeds, err = migration.GetPendingSeeds(ctx, utils.Config.Db.Seed.SqlPaths, conn, afero.NewIOFS(fsys)); err != nil {
return err
}
}
Expand Down
3 changes: 2 additions & 1 deletion internal/db/remote/changes/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"github.com/spf13/afero"
"github.com/supabase/cli/internal/db/diff"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/migration"
)

var output string

func Run(ctx context.Context, schema []string, config pgconn.Config, fsys afero.Fs) error {
// Sanity checks.
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}

Expand Down
3 changes: 2 additions & 1 deletion internal/db/remote/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import (
"github.com/supabase/cli/internal/migration/list"
"github.com/supabase/cli/internal/migration/repair"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/migration"
)

func Run(ctx context.Context, schema []string, config pgconn.Config, fsys afero.Fs) error {
// Sanity checks.
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}

Expand Down
9 changes: 2 additions & 7 deletions internal/db/reset/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/supabase/cli/internal/migration/repair"
"github.com/supabase/cli/internal/seed/buckets"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/migration"
)

Expand Down Expand Up @@ -250,15 +249,11 @@ func resetRemote(ctx context.Context, version string, config pgconn.Config, fsys
if err := migration.ApplyMigrations(ctx, migrations, conn, afero.NewIOFS(fsys)); err != nil {
return err
}
remote, _ := utils.Config.GetRemoteByProjectRef(flags.ProjectRef)
if !remote.Db.Seed.Enabled {
fmt.Fprintln(os.Stderr, "Skipping seed because it is disabled in config.toml for project:", remote.ProjectId)
return nil
} else if !utils.Config.Db.Seed.Enabled {
if !utils.Config.Db.Seed.Enabled {
// Skip because --no-seed flag is set
return nil
}
seeds, err := migration.GetPendingSeeds(ctx, remote.Db.Seed.SqlPaths, conn, afero.NewIOFS(fsys))
seeds, err := migration.GetPendingSeeds(ctx, utils.Config.Db.Seed.SqlPaths, conn, afero.NewIOFS(fsys))
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/db/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/supabase/cli/internal/migration/apply"
"github.com/supabase/cli/internal/status"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/migration"
)

Expand All @@ -39,7 +40,7 @@ var (
)

func Run(ctx context.Context, fromBackup string, fsys afero.Fs) error {
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}
if err := utils.AssertSupabaseDbIsRunning(); err == nil {
Expand Down
8 changes: 3 additions & 5 deletions internal/functions/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

func Run(ctx context.Context, slugs []string, projectRef string, noVerifyJWT *bool, importMapPath string, fsys afero.Fs) error {
// Load function config and project id
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
} else if len(slugs) > 0 {
for _, s := range slugs {
Expand Down Expand Up @@ -60,8 +60,7 @@ func GetFunctionSlugs(fsys afero.Fs) (slugs []string, err error) {
}
}
// Add all function slugs declared in config file
remote, _ := utils.Config.GetRemoteByProjectRef(flags.ProjectRef)
for slug := range remote.Functions {
for slug := range utils.Config.Functions {
slugs = append(slugs, slug)
}
return slugs, nil
Expand All @@ -82,10 +81,9 @@ func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool,
if len(importMapPath) > 0 && !filepath.IsAbs(importMapPath) {
importMapPath = filepath.Join(utils.CurrentDirAbs, importMapPath)
}
remote, _ := utils.Config.GetRemoteByProjectRef(flags.ProjectRef)
functionConfig := make(config.FunctionConfig, len(slugs))
for _, name := range slugs {
function := remote.Functions[name]
function := utils.Config.Functions[name]
// Precedence order: flag > config > fallback
functionDir := filepath.Join(utils.FunctionsDir, name)
if len(function.Entrypoint) == 0 {
Expand Down
3 changes: 2 additions & 1 deletion internal/functions/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/api"
)

Expand Down Expand Up @@ -112,7 +113,7 @@ func Run(ctx context.Context, slug string, projectRef string, useLegacyBundle bo
return RunLegacy(ctx, slug, projectRef, fsys)
}
// 1. Sanity check
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}
// 2. Download eszip to temp file
Expand Down
3 changes: 2 additions & 1 deletion internal/functions/new/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
)

var (
Expand Down Expand Up @@ -45,7 +46,7 @@ func Run(ctx context.Context, slug string, fsys afero.Fs) error {
}

// Load config if available
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
utils.CmdSuggestion = ""
}

Expand Down
3 changes: 2 additions & 1 deletion internal/functions/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/supabase/cli/internal/functions/deploy"
"github.com/supabase/cli/internal/secrets/set"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
)

type InspectMode string
Expand Down Expand Up @@ -70,7 +71,7 @@ var (

func Run(ctx context.Context, envFilePath string, noVerifyJWT *bool, importMapPath string, runtimeOption RuntimeOption, fsys afero.Fs) error {
// 1. Sanity checks.
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/gen/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, lang str
originalURL := utils.ToPostgresURL(dbConfig)
// Add default schemas if --schema flag is not specified
if len(schemas) == 0 {
remote, _ := utils.Config.GetRemoteByProjectRef(projectId)
schemas = utils.RemoveDuplicates(append([]string{"public"}, remote.Api.Schemas...))
schemas = utils.RemoveDuplicates(append([]string{"public"}, utils.Config.Api.Schemas...))
}
included := strings.Join(schemas, ",")

Expand Down
3 changes: 2 additions & 1 deletion internal/migration/squash/squash.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/supabase/cli/internal/migration/list"
"github.com/supabase/cli/internal/migration/repair"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/migration"
)

Expand All @@ -34,7 +35,7 @@ func Run(ctx context.Context, version string, config pgconn.Config, fsys afero.F
return err
}
}
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}
// 1. Squash local migrations
Expand Down
5 changes: 2 additions & 3 deletions internal/seed/buckets/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ func Run(ctx context.Context, projectRef string, interactive bool, fsys afero.Fs
}
return shouldOverwrite
}
remote, _ := utils.Config.GetRemoteByProjectRef(projectRef)
if err := api.UpsertBuckets(ctx, remote.Storage.Buckets, filter); err != nil {
if err := api.UpsertBuckets(ctx, utils.Config.Storage.Buckets, filter); err != nil {
return err
}
return api.UpsertObjects(ctx, remote.Storage.Buckets, utils.NewRootFS(fsys))
return api.UpsertObjects(ctx, utils.Config.Storage.Buckets, utils.NewRootFS(fsys))
}
2 changes: 1 addition & 1 deletion internal/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
func Run(ctx context.Context, fsys afero.Fs, excludedContainers []string, ignoreHealthCheck bool) error {
// Sanity checks.
{
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}
if err := utils.AssertSupabaseDbIsRunning(); err == nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/fetcher"
)

Expand Down Expand Up @@ -67,7 +68,7 @@ func (c *CustomName) toValues(exclude ...string) map[string]string {

func Run(ctx context.Context, names CustomName, format string, fsys afero.Fs) error {
// Sanity checks.
if err := utils.LoadConfigFS(fsys); err != nil {
if err := flags.LoadConfig(fsys); err != nil {
return err
}
if err := assertContainerHealthy(ctx, utils.DbId); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/docker/docker/api/types/volume"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
)

func Run(ctx context.Context, backup bool, projectId string, all bool, fsys afero.Fs) error {
Expand All @@ -17,7 +18,7 @@ func Run(ctx context.Context, backup bool, projectId string, all bool, fsys afer
// Sanity checks.
if len(projectId) > 0 {
utils.Config.ProjectId = projectId
} else if err := utils.LoadConfigFS(fsys); err != nil {
} else if err := flags.LoadConfig(fsys); err != nil {
return err
}
searchProjectIdFilter = utils.Config.ProjectId
Expand Down
Loading

0 comments on commit 88cd609

Please sign in to comment.