diff --git a/internal/db/reset/reset.go b/internal/db/reset/reset.go index a475ca80d..3830a019d 100644 --- a/internal/db/reset/reset.go +++ b/internal/db/reset/reset.go @@ -23,9 +23,11 @@ import ( "github.com/supabase/cli/internal/db/start" "github.com/supabase/cli/internal/gen/keys" "github.com/supabase/cli/internal/migration/apply" + "github.com/supabase/cli/internal/migration/list" "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" ) @@ -241,7 +243,26 @@ func resetRemote(ctx context.Context, version string, config pgconn.Config, fsys if err := migration.DropUserSchemas(ctx, conn); err != nil { return err } - return apply.MigrateAndSeed(ctx, version, conn, fsys) + migrations, err := list.LoadPartialMigrations(version, fsys) + if err != nil { + return err + } + 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 { + // Skip because --no-seed flag is set + return nil + } + seeds, err := migration.GetPendingSeeds(ctx, remote.Db.Seed.SqlPaths, conn, afero.NewIOFS(fsys)) + if err != nil { + return err + } + return migration.SeedData(ctx, seeds, conn, afero.NewIOFS(fsys)) } func LikeEscapeSchema(schemas []string) (result []string) { diff --git a/internal/functions/deploy/deploy.go b/internal/functions/deploy/deploy.go index 6713d47d3..529b78976 100644 --- a/internal/functions/deploy/deploy.go +++ b/internal/functions/deploy/deploy.go @@ -10,6 +10,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/cast" "github.com/supabase/cli/pkg/config" "github.com/supabase/cli/pkg/function" @@ -59,7 +60,8 @@ func GetFunctionSlugs(fsys afero.Fs) (slugs []string, err error) { } } // Add all function slugs declared in config file - for slug := range utils.Config.Functions { + remote, _ := utils.Config.GetRemoteByProjectRef(flags.ProjectRef) + for slug := range remote.Functions { slugs = append(slugs, slug) } return slugs, nil @@ -78,9 +80,10 @@ 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 := utils.Config.Functions[name] + function := remote.Functions[name] // Precedence order: flag > config > fallback functionDir := filepath.Join(utils.FunctionsDir, name) if len(function.Entrypoint) == 0 { diff --git a/internal/gen/types/types.go b/internal/gen/types/types.go index 9ffc7dc72..b399cfb5c 100644 --- a/internal/gen/types/types.go +++ b/internal/gen/types/types.go @@ -31,7 +31,8 @@ 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 { - schemas = utils.RemoveDuplicates(append([]string{"public"}, utils.Config.Api.Schemas...)) + remote, _ := utils.Config.GetRemoteByProjectRef(projectId) + schemas = utils.RemoveDuplicates(append([]string{"public"}, remote.Api.Schemas...)) } included := strings.Join(schemas, ",") diff --git a/internal/projects/list/list.go b/internal/projects/list/list.go index d211f71e9..ef356dfcf 100644 --- a/internal/projects/list/list.go +++ b/internal/projects/list/list.go @@ -29,8 +29,7 @@ func Run(ctx context.Context, fsys afero.Fs) error { return errors.New("Unexpected error retrieving projects: " + string(resp.Body)) } - projectRef, err := flags.LoadProjectRef(fsys) - if err != nil && err != utils.ErrNotLinked { + if err := flags.LoadProjectRef(fsys); err != nil && err != utils.ErrNotLinked { fmt.Fprintln(os.Stderr, err) } @@ -38,7 +37,7 @@ func Run(ctx context.Context, fsys afero.Fs) error { for _, project := range *resp.JSON200 { projects = append(projects, linkedProject{ V1ProjectWithDatabaseResponse: project, - Linked: project.Id == projectRef, + Linked: project.Id == flags.ProjectRef, }) } diff --git a/internal/seed/buckets/buckets.go b/internal/seed/buckets/buckets.go index 365c3a395..b460fdc79 100644 --- a/internal/seed/buckets/buckets.go +++ b/internal/seed/buckets/buckets.go @@ -26,8 +26,9 @@ func Run(ctx context.Context, projectRef string, interactive bool, fsys afero.Fs } return shouldOverwrite } - if err := api.UpsertBuckets(ctx, utils.Config.Storage.Buckets, filter); err != nil { + remote, _ := utils.Config.GetRemoteByProjectRef(projectRef) + if err := api.UpsertBuckets(ctx, remote.Storage.Buckets, filter); err != nil { return err } - return api.UpsertObjects(ctx, utils.Config.Storage.Buckets, utils.NewRootFS(fsys)) + return api.UpsertObjects(ctx, remote.Storage.Buckets, utils.NewRootFS(fsys)) } diff --git a/internal/services/services.go b/internal/services/services.go index b9bfae6e6..ebc08efca 100644 --- a/internal/services/services.go +++ b/internal/services/services.go @@ -16,7 +16,7 @@ import ( ) func Run(ctx context.Context, fsys afero.Fs) error { - if _, err := flags.LoadProjectRef(fsys); err != nil && !errors.Is(err, utils.ErrNotLinked) { + if err := flags.LoadProjectRef(fsys); err != nil && !errors.Is(err, utils.ErrNotLinked) { fmt.Fprintln(os.Stderr, err) } if err := utils.Config.Load("", utils.NewRootFS(fsys)); err != nil && !errors.Is(err, os.ErrNotExist) { diff --git a/internal/start/start.go b/internal/start/start.go index 5877b47e4..4315d31b7 100644 --- a/internal/start/start.go +++ b/internal/start/start.go @@ -37,7 +37,6 @@ import ( func Run(ctx context.Context, fsys afero.Fs, excludedContainers []string, ignoreHealthCheck bool) error { // Sanity checks. { - _, _ = flags.LoadProjectRef(fsys) if err := utils.LoadConfigFS(fsys); err != nil { return err } @@ -48,7 +47,9 @@ func Run(ctx context.Context, fsys afero.Fs, excludedContainers []string, ignore } else if !errors.Is(err, utils.ErrNotRunning) { return err } - _ = services.CheckVersions(ctx, fsys) + if err := flags.LoadProjectRef(fsys); err == nil { + _ = services.CheckVersions(ctx, fsys) + } } if err := utils.RunProgram(ctx, func(p utils.Program, ctx context.Context) error { diff --git a/internal/storage/cp/cp.go b/internal/storage/cp/cp.go index 8aed7a6fa..e55120a1c 100644 --- a/internal/storage/cp/cp.go +++ b/internal/storage/cp/cp.go @@ -115,6 +115,7 @@ func UploadStorageObjectAll(ctx context.Context, api storage.StorageAPI, remoteP return err } } + remote, _ := utils.Config.GetRemoteByProjectRef(flags.ProjectRef) // Overwrites existing object when using --recursive flag opts = append(opts, func(fo *storage.FileOptions) { fo.Overwrite = true @@ -153,7 +154,7 @@ func UploadStorageObjectAll(ctx context.Context, api storage.StorageAPI, remoteP // Retry after creating bucket if bucket, prefix := client.SplitBucketPrefix(dstPath); len(prefix) > 0 { body := storage.CreateBucketRequest{Name: bucket} - if config, ok := utils.Config.Storage.Buckets[bucket]; ok { + if config, ok := remote.Storage.Buckets[bucket]; ok { body.Public = config.Public body.FileSizeLimit = int64(config.FileSizeLimit) body.AllowedMimeTypes = config.AllowedMimeTypes diff --git a/internal/utils/flags/db_url.go b/internal/utils/flags/db_url.go index d089f2d13..013453b86 100644 --- a/internal/utils/flags/db_url.go +++ b/internal/utils/flags/db_url.go @@ -50,10 +50,8 @@ func ParseDatabaseConfig(flagSet *pflag.FlagSet, fsys afero.Fs) error { // Update connection config switch connType { case direct: - if err := utils.Config.Load("", utils.NewRootFS(fsys)); err != nil { - if !errors.Is(err, os.ErrNotExist) { - return err - } + if err := utils.Config.Load("", utils.NewRootFS(fsys)); err != nil && !errors.Is(err, os.ErrNotExist) { + return err } if flag := flagSet.Lookup("db-url"); flag != nil { config, err := pgconn.ParseConfig(flag.Value.String()) @@ -76,25 +74,23 @@ func ParseDatabaseConfig(flagSet *pflag.FlagSet, fsys afero.Fs) error { if err := utils.LoadConfigFS(fsys); err != nil { return err } - projectRef, err := LoadProjectRef(fsys) - if err != nil { + if err := LoadProjectRef(fsys); err != nil { return err } - DbConfig = NewDbConfigWithPassword(projectRef) + DbConfig = NewDbConfigWithPassword(ProjectRef) case proxy: token, err := utils.LoadAccessTokenFS(fsys) if err != nil { return err } - projectRef, err := LoadProjectRef(fsys) - if err != nil { + if err := LoadProjectRef(fsys); err != nil { return err } DbConfig.Host = utils.GetSupabaseAPIHost() DbConfig.Port = 443 DbConfig.User = "postgres" DbConfig.Password = token - DbConfig.Database = projectRef + DbConfig.Database = ProjectRef } return nil } diff --git a/internal/utils/flags/project_ref.go b/internal/utils/flags/project_ref.go index 437bbb668..73df72353 100644 --- a/internal/utils/flags/project_ref.go +++ b/internal/utils/flags/project_ref.go @@ -16,12 +16,7 @@ import ( var ProjectRef string func ParseProjectRef(ctx context.Context, fsys afero.Fs) error { - // Flag takes highest precedence - if len(ProjectRef) > 0 { - return utils.AssertProjectRefIsValid(ProjectRef) - } - // Followed by linked ref file - if _, err := LoadProjectRef(fsys); !errors.Is(err, utils.ErrNotLinked) { + if err := LoadProjectRef(fsys); !errors.Is(err, utils.ErrNotLinked) { return err } // Prompt as the last resort @@ -55,20 +50,22 @@ func PromptProjectRef(ctx context.Context, title string) error { return nil } -func LoadProjectRef(fsys afero.Fs) (string, error) { +func LoadProjectRef(fsys afero.Fs) error { + // Flag takes highest precedence + if len(ProjectRef) > 0 { + return utils.AssertProjectRefIsValid(ProjectRef) + } // Env var takes precedence over ref file - ProjectRef = viper.GetString("PROJECT_ID") - if len(ProjectRef) == 0 { - projectRefBytes, err := afero.ReadFile(fsys, utils.ProjectRefPath) - if errors.Is(err, os.ErrNotExist) { - return "", errors.New(utils.ErrNotLinked) - } else if err != nil { - return "", errors.Errorf("failed to load project ref: %w", err) - } - ProjectRef = string(bytes.TrimSpace(projectRefBytes)) + if ProjectRef = viper.GetString("PROJECT_ID"); len(ProjectRef) > 0 { + return utils.AssertProjectRefIsValid(ProjectRef) } - if err := utils.AssertProjectRefIsValid(ProjectRef); err != nil { - return "", err + // Load from local file last + projectRefBytes, err := afero.ReadFile(fsys, utils.ProjectRefPath) + if errors.Is(err, os.ErrNotExist) { + return errors.New(utils.ErrNotLinked) + } else if err != nil { + return errors.Errorf("failed to load project ref: %w", err) } - return ProjectRef, nil + ProjectRef = string(bytes.TrimSpace(projectRefBytes)) + return utils.AssertProjectRefIsValid(ProjectRef) } diff --git a/pkg/config/auth.go b/pkg/config/auth.go index 10611c23b..5cb551cd1 100644 --- a/pkg/config/auth.go +++ b/pkg/config/auth.go @@ -48,7 +48,7 @@ type ( Enabled bool `toml:"enabled"` Image string `toml:"-"` - SiteUrl string `toml:"site_url"` + SiteUrl string `toml:"site_url" mapstructure:"site_url"` AdditionalRedirectUrls []string `toml:"additional_redirect_urls"` JwtExpiry uint `toml:"jwt_expiry"` EnableRefreshTokenRotation bool `toml:"enable_refresh_token_rotation"`