Skip to content
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
7 changes: 7 additions & 0 deletions planetscale/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const (
DatabaseEnginePostgres DatabaseEngine = "postgresql"
)

// StorageConfig represents storage size configuration for a database or branch.
type StorageConfig struct {
MinimumStorageBytes *int64 `json:"minimum_storage_bytes,omitempty"`
MaximumStorageBytes *int64 `json:"maximum_storage_bytes,omitempty"`
}

// CreateDatabaseRequest encapsulates the request for creating a new database.
type CreateDatabaseRequest struct {
Organization string
Expand All @@ -25,6 +31,7 @@ type CreateDatabaseRequest struct {
Kind DatabaseEngine `json:"kind,omitempty"`
Replicas *int `json:"replicas,omitempty"`
MajorVersion string `json:"major_version,omitempty"`
Storage *StorageConfig `json:"storage,omitempty"`
}

// DatabaseRequest encapsulates the request for getting a single database.
Expand Down
55 changes: 55 additions & 0 deletions planetscale/databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,61 @@ func TestDatabases_CreateWithReplicasZero(t *testing.T) {
c.Assert(db, qt.DeepEquals, want)
}

func TestDatabases_CreateWithStorage(t *testing.T) {
c := qt.New(t)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
c.Assert(r.Method, qt.Equals, http.MethodPost)

var body map[string]any
err := json.NewDecoder(r.Body).Decode(&body)
c.Assert(err, qt.IsNil)

storage, ok := body["storage"].(map[string]any)
c.Assert(ok, qt.IsTrue, qt.Commentf("storage field should be a nested object"))
c.Assert(storage["minimum_storage_bytes"], qt.Equals, float64(10737418240))
c.Assert(storage["maximum_storage_bytes"], qt.Equals, float64(107374182400))

out := `{"id":"planetscale-go-test-db","type":"database","name":"planetscale-go-test-db","notes":"","created_at":"2021-01-14T10:19:23.000Z","updated_at":"2021-01-14T10:19:23.000Z", "region": { "slug": "us-west", "display_name": "US West" },"state":"ready","kind":"postgresql"}`
_, err = w.Write([]byte(out))
c.Assert(err, qt.IsNil)
}))

client, err := NewClient(WithBaseURL(ts.URL))
c.Assert(err, qt.IsNil)

ctx := context.Background()
minStorage := int64(10737418240)
maxStorage := int64(107374182400)

db, err := client.Databases.Create(ctx, &CreateDatabaseRequest{
Organization: testOrg,
Region: "us-west",
Name: testDatabase,
Kind: DatabaseEnginePostgres,
Storage: &StorageConfig{
MinimumStorageBytes: &minStorage,
MaximumStorageBytes: &maxStorage,
},
})

want := &Database{
Name: testDatabase,
State: DatabaseReady,
Kind: DatabaseEnginePostgres,
Region: Region{
Slug: "us-west",
Name: "US West",
},
CreatedAt: time.Date(2021, time.January, 14, 10, 19, 23, 0, time.UTC),
UpdatedAt: time.Date(2021, time.January, 14, 10, 19, 23, 0, time.UTC),
}

c.Assert(err, qt.IsNil)
c.Assert(db, qt.DeepEquals, want)
}

func TestDatabases_Get(t *testing.T) {
c := qt.New(t)

Expand Down
3 changes: 2 additions & 1 deletion planetscale/postgres_branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type CreatePostgresBranchRequest struct {
ParentBranch string `json:"parent_branch"`
BackupID string `json:"backup_id,omitempty"`
ClusterName string `json:"cluster_name,omitempty"`
MajorVersion string `json:"major_version,omitempty"`
MajorVersion string `json:"major_version,omitempty"`
Storage *StorageConfig `json:"storage,omitempty"`
}

// ListPostgresBranchesRequest encapsulates the request to list Postgres branches for a database.
Expand Down
56 changes: 56 additions & 0 deletions planetscale/postgres_branches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package planetscale

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -324,3 +325,58 @@ func TestPostgresBranches_ListClusterSKUs(t *testing.T) {
c.Assert(err, qt.IsNil)
c.Assert(skus, qt.DeepEquals, want)
}

func TestPostgresBranches_CreateWithStorage(t *testing.T) {
c := qt.New(t)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
c.Assert(r.Method, qt.Equals, http.MethodPost)

var body map[string]any
err := json.NewDecoder(r.Body).Decode(&body)
c.Assert(err, qt.IsNil)

storage, ok := body["storage"].(map[string]any)
c.Assert(ok, qt.IsTrue, qt.Commentf("storage field should be a nested object"))
c.Assert(storage["minimum_storage_bytes"], qt.Equals, float64(10737418240))
c.Assert(storage["maximum_storage_bytes"], qt.Equals, float64(107374182400))

out := `{"id":"postgres-test-branch","name":"postgres-test-branch","created_at":"2021-01-14T10:19:23.000Z","updated_at":"2021-01-14T10:19:23.000Z", "region": {"slug": "us-west", "display_name": "US West"}}`
_, err = w.Write([]byte(out))
c.Assert(err, qt.IsNil)
}))

client, err := NewClient(WithBaseURL(ts.URL))
c.Assert(err, qt.IsNil)

ctx := context.Background()
minStorage := int64(10737418240)
maxStorage := int64(107374182400)

branch, err := client.PostgresBranches.Create(ctx, &CreatePostgresBranchRequest{
Organization: "my-org",
Database: "postgres-test-db",
Region: "us-west",
Name: testPostgresBranch,
ParentBranch: "main",
Storage: &StorageConfig{
MinimumStorageBytes: &minStorage,
MaximumStorageBytes: &maxStorage,
},
})

want := &PostgresBranch{
ID: "postgres-test-branch",
Name: testPostgresBranch,
Region: Region{
Slug: "us-west",
Name: "US West",
},
CreatedAt: time.Date(2021, time.January, 14, 10, 19, 23, 0, time.UTC),
UpdatedAt: time.Date(2021, time.January, 14, 10, 19, 23, 0, time.UTC),
}

c.Assert(err, qt.IsNil)
c.Assert(branch, qt.DeepEquals, want)
}
Loading