Skip to content

Commit e4deb7e

Browse files
authored
Merge pull request #83 from planetscale/iheanyi/add-promote-branch
Add `Promote` function to `DatabaseBranches`
2 parents 7ca559d + 4b6b068 commit e4deb7e

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

Diff for: planetscale/branches.go

+28
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type DatabaseBranch struct {
1414
Name string `json:"name"`
1515
ParentBranch string `json:"parent_branch"`
1616
Region Region `json:"region"`
17+
Production bool `json:"production"`
1718
CreatedAt time.Time `json:"created_at"`
1819
UpdatedAt time.Time `json:"updated_at"`
1920
Status string `json:"status,omitempty"`
@@ -86,6 +87,14 @@ type RefreshSchemaRequest struct {
8687
Branch string `json:"-"`
8788
}
8889

90+
// PromoteBranchRequest encapsulates the request for promoting a branch to
91+
// production.
92+
type PromoteBranchRequest struct {
93+
Organization string `json:"-"`
94+
Database string `json:"-"`
95+
Branch string `json:"branch"`
96+
}
97+
8998
// DatabaseBranchesService is an interface for communicating with the PlanetScale
9099
// Database Branch API endpoint.
91100
type DatabaseBranchesService interface {
@@ -97,6 +106,7 @@ type DatabaseBranchesService interface {
97106
Diff(context.Context, *DiffBranchRequest) ([]*Diff, error)
98107
Schema(context.Context, *BranchSchemaRequest) ([]*Diff, error)
99108
RefreshSchema(context.Context, *RefreshSchemaRequest) error
109+
Promote(context.Context, *PromoteBranchRequest) (*DatabaseBranch, error)
100110
}
101111

102112
type databaseBranchesService struct {
@@ -243,6 +253,24 @@ func (d *databaseBranchesService) RefreshSchema(ctx context.Context, refreshReq
243253
return nil
244254
}
245255

256+
// PromoteBranch promotes a database's branch from a development branch to a
257+
// production branch.
258+
func (d *databaseBranchesService) Promote(ctx context.Context, promoteReq *PromoteBranchRequest) (*DatabaseBranch, error) {
259+
path := fmt.Sprintf("%s/%s/promote-branch", databasesAPIPath(promoteReq.Organization), promoteReq.Database)
260+
req, err := d.client.newRequest(http.MethodPost, path, promoteReq)
261+
if err != nil {
262+
return nil, errors.Wrap(err, "error creating request for branch promotion")
263+
}
264+
265+
branch := &DatabaseBranch{}
266+
err = d.client.do(ctx, req, &branch)
267+
if err != nil {
268+
return nil, err
269+
}
270+
271+
return branch, nil
272+
}
273+
246274
func databaseBranchesAPIPath(org, db string) string {
247275
return fmt.Sprintf("%s/%s/branches", databasesAPIPath(org), db)
248276
}

Diff for: planetscale/branches_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package planetscale
22

33
import (
44
"context"
5+
"encoding/json"
56
"net/http"
67
"net/http/httptest"
78
"testing"
@@ -259,3 +260,45 @@ func TestBranches_RefreshSchema(t *testing.T) {
259260
})
260261
c.Assert(err, qt.IsNil)
261262
}
263+
264+
func TestBranches_Promote(t *testing.T) {
265+
c := qt.New(t)
266+
267+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
268+
c.Assert(http.MethodPost, qt.Equals, r.Method)
269+
w.WriteHeader(200)
270+
271+
payload := make(map[string]interface{})
272+
decoder := json.NewDecoder(r.Body)
273+
err := decoder.Decode(&payload)
274+
c.Assert(err, qt.IsNil)
275+
276+
c.Assert(payload["branch"], qt.Equals, "planetscale-go-test-db-branch")
277+
278+
out := `{"id":"planetscale-go-test-db-branch","type":"database_branch","name":"planetscale-go-test-db-branch","created_at":"2021-01-14T10:19:23.000Z","updated_at":"2021-01-14T10:19:23.000Z"}`
279+
_, err = w.Write([]byte(out))
280+
c.Assert(err, qt.IsNil)
281+
}))
282+
283+
client, err := NewClient(WithBaseURL(ts.URL))
284+
c.Assert(err, qt.IsNil)
285+
286+
ctx := context.Background()
287+
org := "my-org"
288+
name := "planetscale-go-test-db"
289+
290+
db, err := client.DatabaseBranches.Promote(ctx, &PromoteBranchRequest{
291+
Organization: org,
292+
Database: name,
293+
Branch: "planetscale-go-test-db-branch",
294+
})
295+
296+
want := &DatabaseBranch{
297+
Name: testBranch,
298+
CreatedAt: time.Date(2021, time.January, 14, 10, 19, 23, 000, time.UTC),
299+
UpdatedAt: time.Date(2021, time.January, 14, 10, 19, 23, 000, time.UTC),
300+
}
301+
302+
c.Assert(err, qt.IsNil)
303+
c.Assert(db, qt.DeepEquals, want)
304+
}

0 commit comments

Comments
 (0)