Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: account for remote config when pushing #2952

Merged
merged 3 commits into from
Dec 7, 2024
Merged
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
23 changes: 22 additions & 1 deletion internal/db/reset/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions internal/functions/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion internal/gen/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ",")

Expand Down
5 changes: 2 additions & 3 deletions internal/projects/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ 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)
}

var projects []linkedProject
for _, project := range *resp.JSON200 {
projects = append(projects, linkedProject{
V1ProjectWithDatabaseResponse: project,
Linked: project.Id == projectRef,
Linked: project.Id == flags.ProjectRef,
})
}

Expand Down
5 changes: 3 additions & 2 deletions internal/seed/buckets/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion internal/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions internal/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion internal/storage/cp/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 6 additions & 10 deletions internal/utils/flags/db_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
}
Expand Down
35 changes: 16 additions & 19 deletions internal/utils/flags/project_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion pkg/config/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
Loading