Skip to content

Commit

Permalink
fix: update postgres image if linked project is healthy
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Nov 16, 2024
1 parent 44a1309 commit f4b4819
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
9 changes: 6 additions & 3 deletions internal/link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Run(ctx context.Context, projectRef string, fsys afero.Fs, options ...func(
fmt.Fprintln(utils.GetDebugLogger(), err)
}

if err := checkRemoteProjectStatus(ctx, projectRef); err != nil {
if err := checkRemoteProjectStatus(ctx, projectRef, fsys); err != nil {
return err
}

Expand Down Expand Up @@ -254,7 +254,7 @@ func updatePoolerConfig(config api.SupavisorConfigResponse) {

var errProjectPaused = errors.New("project is paused")

func checkRemoteProjectStatus(ctx context.Context, projectRef string) error {
func checkRemoteProjectStatus(ctx context.Context, projectRef string, fsys afero.Fs) error {
resp, err := utils.GetSupabase().V1GetProjectWithResponse(ctx, projectRef)
if err != nil {
return errors.Errorf("failed to retrieve remote project status: %w", err)
Expand All @@ -274,7 +274,10 @@ func checkRemoteProjectStatus(ctx context.Context, projectRef string) error {
utils.CmdSuggestion = fmt.Sprintf("An admin must unpause it from the Supabase dashboard at %s", utils.Aqua(fmt.Sprintf("%s/project/%s", utils.GetSupabaseDashboardURL(), projectRef)))
return errors.New(errProjectPaused)
case api.V1ProjectResponseStatusACTIVEHEALTHY:
// Project is in the desired state, do nothing
// Update postgres image version to match remote
if version := resp.JSON200.Database.Version; len(version) > 0 {
return utils.WriteFile(utils.PostgresVersionPath, []byte(version), fsys)
}
default:
fmt.Fprintf(os.Stderr, "%s: Project status is %s instead of Active Healthy. Some operations might fail.\n", utils.Yellow("WARNING"), resp.JSON200.Status)
}
Expand Down
46 changes: 41 additions & 5 deletions internal/link/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ func TestLinkCommand(t *testing.T) {
gock.New(utils.DefaultApiHost).
Get("/v1/projects/" + project).
Reply(200).
JSON(api.V1ProjectResponse{Status: api.V1ProjectResponseStatusACTIVEHEALTHY})
JSON(api.V1ProjectResponse{
Status: api.V1ProjectResponseStatusACTIVEHEALTHY,
Database: &api.V1DatabaseResponse{},
})
gock.New(utils.DefaultApiHost).
Get("/v1/projects/" + project + "/api-keys").
Reply(200).
Expand Down Expand Up @@ -130,7 +133,10 @@ func TestLinkCommand(t *testing.T) {
gock.New(utils.DefaultApiHost).
Get("/v1/projects/" + project).
Reply(200).
JSON(api.V1ProjectResponse{Status: api.V1ProjectResponseStatusACTIVEHEALTHY})
JSON(api.V1ProjectResponse{
Status: api.V1ProjectResponseStatusACTIVEHEALTHY,
Database: &api.V1DatabaseResponse{},
})
gock.New(utils.DefaultApiHost).
Get("/v1/projects/" + project + "/api-keys").
Reply(200).
Expand Down Expand Up @@ -175,7 +181,10 @@ func TestLinkCommand(t *testing.T) {
gock.New(utils.DefaultApiHost).
Get("/v1/projects/" + project).
Reply(200).
JSON(api.V1ProjectResponse{Status: api.V1ProjectResponseStatusACTIVEHEALTHY})
JSON(api.V1ProjectResponse{
Status: api.V1ProjectResponseStatusACTIVEHEALTHY,
Database: &api.V1DatabaseResponse{},
})
gock.New(utils.DefaultApiHost).
Get("/v1/projects/" + project + "/api-keys").
Reply(200).
Expand Down Expand Up @@ -215,21 +224,48 @@ func TestLinkCommand(t *testing.T) {
func TestStatusCheck(t *testing.T) {
project := "test-project"

t.Run("updates postgres version when healthy", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Flush pending mocks after test execution
defer gock.OffAll()
// Mock project status
gock.New(utils.DefaultApiHost).
Get("/v1/projects/" + project).
Reply(http.StatusOK).
JSON(api.V1ProjectResponse{
Status: api.V1ProjectResponseStatusACTIVEHEALTHY,
Database: &api.V1DatabaseResponse{Version: "15.6.1.139"},
})
// Run test
err := checkRemoteProjectStatus(context.Background(), project, fsys)
// Check error
assert.NoError(t, err)
version, err := afero.ReadFile(fsys, utils.PostgresVersionPath)
assert.NoError(t, err)
assert.Equal(t, "15.6.1.139", string(version))
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("ignores project not found", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Flush pending mocks after test execution
defer gock.OffAll()
// Mock project status
gock.New(utils.DefaultApiHost).
Get("/v1/projects/" + project).
Reply(http.StatusNotFound)
// Run test
err := checkRemoteProjectStatus(context.Background(), project)
err := checkRemoteProjectStatus(context.Background(), project, fsys)
// Check error
assert.NoError(t, err)
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("throws error on project inactive", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Flush pending mocks after test execution
defer gock.OffAll()
// Mock project status
Expand All @@ -238,7 +274,7 @@ func TestStatusCheck(t *testing.T) {
Reply(http.StatusOK).
JSON(api.V1ProjectResponse{Status: api.V1ProjectResponseStatusINACTIVE})
// Run test
err := checkRemoteProjectStatus(context.Background(), project)
err := checkRemoteProjectStatus(context.Background(), project, fsys)
// Check error
assert.ErrorIs(t, err, errProjectPaused)
assert.Empty(t, apitest.ListUnmatchedRequests())
Expand Down

0 comments on commit f4b4819

Please sign in to comment.