Skip to content

Commit 26aa9d5

Browse files
Merge pull request #272 from planetscale/allow-replicas-to-be-set-to-zero
Change Replicas type from int to *int in CreateDatabaseRequest
2 parents bf7ee76 + 44c0b2a commit 26aa9d5

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

planetscale/databases.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type CreateDatabaseRequest struct {
2323
Region string `json:"region,omitempty"`
2424
ClusterSize string `json:"cluster_size,omitempty"`
2525
Kind DatabaseEngine `json:"kind,omitempty"`
26-
Replicas int `json:"replicas,omitempty"`
26+
Replicas *int `json:"replicas,omitempty"`
2727
}
2828

2929
// DatabaseRequest encapsulates the request for getting a single database.

planetscale/databases_test.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,66 @@ func TestDatabases_CreateWithReplicas(t *testing.T) {
127127
org := "my-org"
128128
name := "planetscale-go-test-db"
129129
notes := "This is a test DB created from the planetscale-go API library"
130+
replicas := 3
130131

131132
db, err := client.Databases.Create(ctx, &CreateDatabaseRequest{
132133
Organization: org,
133134
Region: "us-west",
134135
Name: name,
135136
Notes: notes,
136-
Replicas: 3,
137+
Replicas: &replicas,
138+
})
139+
140+
want := &Database{
141+
Name: name,
142+
Notes: notes,
143+
State: DatabaseReady,
144+
Region: Region{
145+
Slug: "us-west",
146+
Name: "US West",
147+
},
148+
CreatedAt: time.Date(2021, time.January, 14, 10, 19, 23, 0, time.UTC),
149+
UpdatedAt: time.Date(2021, time.January, 14, 10, 19, 23, 0, time.UTC),
150+
}
151+
152+
c.Assert(err, qt.IsNil)
153+
c.Assert(db, qt.DeepEquals, want)
154+
}
155+
156+
func TestDatabases_CreateWithReplicasZero(t *testing.T) {
157+
c := qt.New(t)
158+
159+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
160+
w.WriteHeader(200)
161+
var body map[string]any
162+
err := json.NewDecoder(r.Body).Decode(&body)
163+
c.Assert(err, qt.IsNil)
164+
165+
// With omitempty and *int type, replicas field SHOULD be present when explicitly set to 0
166+
replicas, hasReplicas := body["replicas"]
167+
c.Assert(hasReplicas, qt.IsTrue, qt.Commentf("replicas field should be present when explicitly set to 0"))
168+
c.Assert(replicas, qt.Equals, float64(0))
169+
170+
out := `{"id":"planetscale-go-test-db","type":"database","name":"planetscale-go-test-db","notes":"This is a test DB created from the planetscale-go API library","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"}`
171+
_, err = w.Write([]byte(out))
172+
c.Assert(err, qt.IsNil)
173+
}))
174+
175+
client, err := NewClient(WithBaseURL(ts.URL))
176+
c.Assert(err, qt.IsNil)
177+
178+
ctx := context.Background()
179+
org := "my-org"
180+
name := "planetscale-go-test-db"
181+
notes := "This is a test DB created from the planetscale-go API library"
182+
replicas := 0
183+
184+
db, err := client.Databases.Create(ctx, &CreateDatabaseRequest{
185+
Organization: org,
186+
Region: "us-west",
187+
Name: name,
188+
Notes: notes,
189+
Replicas: &replicas,
137190
})
138191

139192
want := &Database{

0 commit comments

Comments
 (0)