Skip to content

Commit

Permalink
fix: restore project id from base config
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Jan 10, 2025
1 parent 49ea4f2 commit dc45095
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
6 changes: 5 additions & 1 deletion internal/config/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ func Run(ctx context.Context, ref string, fsys afero.Fs) error {
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
1 change: 1 addition & 0 deletions internal/db/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func Run(ctx context.Context, dryRun, ignoreVersionMismatch bool, includeRoles,
}
var seeds []migration.SeedFile
if includeSeed {
// 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 {
Expand Down
30 changes: 20 additions & 10 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,16 +407,21 @@ func (c *config) loadFromReader(v *viper.Viper, r io.Reader) error {
return errors.Errorf("failed to merge config: %w", err)
}
// Find [remotes.*] block to override base config
baseId := v.GetString("project_id")
idToName := map[string]string{baseId: "base"}
for name, remote := range v.GetStringMap("remotes") {
if m, ok := remote.(map[string]any); ok && m["project_id"] == c.ProjectId {
fmt.Fprintln(os.Stderr, "Loading remote override:", name)
// On remotes branches set seed as disabled by default
v.Set("db.seed.enabled", false)
// TODO: warn duplicate project_id in remotes
delete(m, "project_id")
if err := v.MergeConfigMap(m); err != nil {
projectId := v.GetString(fmt.Sprintf("remotes.%s.project_id", name))
// Track remote project_id to check for duplication
if other, exists := idToName[projectId]; exists {
return errors.Errorf("duplicate project_id for [remotes.%s] and %s", name, other)
}
idToName[projectId] = fmt.Sprintf("[remotes.%s]", name)
if projectId == c.ProjectId {
fmt.Fprintln(os.Stderr, "Loading config override:", idToName[projectId])
if err := v.MergeConfigMap(remote.(map[string]any)); err != nil {
return err
}
v.Set("project_id", baseId)
}
}
// Manually parse [functions.*] to empty struct for backwards compatibility
Expand Down Expand Up @@ -1308,11 +1313,16 @@ func (c *baseConfig) GetServiceImages() []string {
}

// Retrieve the final base config to use taking into account the remotes override
// Pre: config must be loaded after setting config.ProjectID = "ref"
func (c *config) GetRemoteByProjectRef(projectRef string) (baseConfig, error) {
// Config must be loaded after setting config.ProjectID = "ref"
base := c.baseConfig.Clone()
base.ProjectId = projectRef
return base, nil
for _, remote := range c.Remotes {
if remote.ProjectId == projectRef {
base.ProjectId = projectRef
return base, nil
}
}
return base, errors.Errorf("no remote found for project_id: %s", projectRef)
}

func ToTomlBytes(config any) ([]byte, error) {
Expand Down

0 comments on commit dc45095

Please sign in to comment.