-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add remote utils for api config
- Loading branch information
Showing
5 changed files
with
422 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package config | ||
|
||
import ( | ||
"strings" | ||
|
||
v1API "github.com/supabase/cli/pkg/api" | ||
) | ||
|
||
type ( | ||
api struct { | ||
Enabled bool `toml:"enabled"` | ||
Image string `toml:"-"` | ||
KongImage string `toml:"-"` | ||
Port uint16 `toml:"port"` | ||
Schemas []string `toml:"schemas"` | ||
ExtraSearchPath []string `toml:"extra_search_path"` | ||
MaxRows uint `toml:"max_rows"` | ||
Tls tlsKong `toml:"tls"` | ||
// TODO: replace [auth|studio].api_url | ||
ExternalUrl string `toml:"external_url"` | ||
} | ||
|
||
tlsKong struct { | ||
Enabled bool `toml:"enabled"` | ||
} | ||
) | ||
|
||
func (a *api) ToUpdatePostgrestConfigBody() v1API.UpdatePostgrestConfigBody { | ||
body := v1API.UpdatePostgrestConfigBody{} | ||
|
||
// Convert Schemas to a comma-separated string | ||
if len(a.Schemas) > 0 { | ||
schemas := strings.Join(a.Schemas, ",") | ||
body.DbSchema = &schemas | ||
} | ||
|
||
// Convert ExtraSearchPath to a comma-separated string | ||
if len(a.ExtraSearchPath) > 0 { | ||
extraSearchPath := strings.Join(a.ExtraSearchPath, ",") | ||
body.DbExtraSearchPath = &extraSearchPath | ||
} | ||
|
||
// Convert MaxRows to int pointer | ||
if a.MaxRows > 0 { | ||
maxRows := int(a.MaxRows) | ||
body.MaxRows = &maxRows | ||
} | ||
|
||
// Note: DbPool is not present in the Api struct, so it's not set here | ||
return body | ||
} | ||
|
||
func (a *api) FromRemoteApiConfig(remoteConfig v1API.PostgrestConfigWithJWTSecretResponse) api { | ||
result := *a | ||
// Update Schemas if present in remoteConfig | ||
result.Schemas = strings.Split(remoteConfig.DbSchema, ",") | ||
|
||
// Update ExtraSearchPath if present in remoteConfig | ||
result.ExtraSearchPath = strings.Split(remoteConfig.DbExtraSearchPath, ",") | ||
|
||
// Update MaxRows if present in remoteConfig | ||
result.MaxRows = uint(remoteConfig.MaxRows) | ||
|
||
return result | ||
} | ||
|
||
func (a *api) DiffWithRemote(remoteConfig v1API.PostgrestConfigWithJWTSecretResponse) []byte { | ||
// Convert the config values into easily comparable remoteConfig values | ||
currentValue := ToTomlBytes(a) | ||
remoteCompare := ToTomlBytes(a.FromRemoteApiConfig(remoteConfig)) | ||
return Diff("remote[api]", remoteCompare, "local[api]", currentValue) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
v1API "github.com/supabase/cli/pkg/api" | ||
) | ||
|
||
func TestApiToUpdatePostgrestConfigBody(t *testing.T) { | ||
t.Run("converts all fields correctly", func(t *testing.T) { | ||
api := &api{ | ||
Schemas: []string{"public", "private"}, | ||
ExtraSearchPath: []string{"extensions", "public"}, | ||
MaxRows: 1000, | ||
} | ||
|
||
body := api.ToUpdatePostgrestConfigBody() | ||
|
||
assert.Equal(t, "public,private", *body.DbSchema) | ||
assert.Equal(t, "extensions,public", *body.DbExtraSearchPath) | ||
assert.Equal(t, 1000, *body.MaxRows) | ||
}) | ||
|
||
t.Run("handles empty fields", func(t *testing.T) { | ||
api := &api{} | ||
|
||
body := api.ToUpdatePostgrestConfigBody() | ||
|
||
assert.Nil(t, body.DbSchema) | ||
assert.Nil(t, body.DbExtraSearchPath) | ||
assert.Nil(t, body.MaxRows) | ||
}) | ||
} | ||
|
||
func TestApiDiffWithRemote(t *testing.T) { | ||
t.Run("detects differences", func(t *testing.T) { | ||
api := &api{ | ||
Schemas: []string{"public", "private"}, | ||
ExtraSearchPath: []string{"extensions", "public"}, | ||
MaxRows: 1000, | ||
} | ||
|
||
remoteConfig := v1API.PostgrestConfigWithJWTSecretResponse{ | ||
DbSchema: "public", | ||
DbExtraSearchPath: "public", | ||
MaxRows: 500, | ||
} | ||
|
||
diff := api.DiffWithRemote(remoteConfig) | ||
|
||
assert.Contains(t, string(diff), "-schemas = [\"public\"]") | ||
assert.Contains(t, string(diff), "+schemas = [\"public\", \"private\"]") | ||
assert.Contains(t, string(diff), "-extra_search_path = [\"public\"]") | ||
assert.Contains(t, string(diff), "+extra_search_path = [\"extensions\", \"public\"]") | ||
assert.Contains(t, string(diff), "-max_rows = 500") | ||
assert.Contains(t, string(diff), "+max_rows = 1000") | ||
}) | ||
|
||
t.Run("handles no differences", func(t *testing.T) { | ||
api := &api{ | ||
Schemas: []string{"public"}, | ||
ExtraSearchPath: []string{"public"}, | ||
MaxRows: 500, | ||
} | ||
|
||
remoteConfig := v1API.PostgrestConfigWithJWTSecretResponse{ | ||
DbSchema: "public", | ||
DbExtraSearchPath: "public", | ||
MaxRows: 500, | ||
} | ||
|
||
diff := api.DiffWithRemote(remoteConfig) | ||
|
||
assert.Empty(t, diff) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.