Skip to content

Commit 6ceebec

Browse files
committed
feat(engine): add the command to generate schema diff to CLI
1 parent ac5db9e commit 6ceebec

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

engine/cmd/cli/commands/clone/actions.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,25 @@ func destroy(cliCtx *cli.Context) error {
239239
return err
240240
}
241241

242+
// schemaDiff runs a request to generate schema difference between clone and its snapshot.
243+
func schemaDiff(cliCtx *cli.Context) error {
244+
dblabClient, err := commands.ClientByCLIContext(cliCtx)
245+
if err != nil {
246+
return err
247+
}
248+
249+
cloneID := cliCtx.Args().First()
250+
251+
response, err := dblabClient.SchemaDiff(cliCtx.Context, cloneID)
252+
if err != nil {
253+
return err
254+
}
255+
256+
_, err = fmt.Fprint(cliCtx.App.Writer, string(response))
257+
258+
return err
259+
}
260+
242261
// startObservation runs a request to startObservation clone.
243262
func startObservation(cliCtx *cli.Context) error {
244263
dblabClient, err := commands.ClientByCLIContext(cliCtx)

engine/cmd/cli/commands/clone/command_list.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ func CommandList() []*cli.Command {
130130
},
131131
},
132132
},
133+
{
134+
Name: "schema-diff",
135+
Usage: "generate schema difference between clone and its snapshot",
136+
ArgsUsage: "CLONE_ID",
137+
Before: checkCloneIDBefore,
138+
Action: schemaDiff,
139+
},
133140
{
134141
Name: "start-observation",
135142
Usage: "[EXPERIMENTAL] start clone state monitoring",

engine/pkg/client/dblabapi/clone.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,30 @@ func (c *Client) DestroyCloneAsync(ctx context.Context, cloneID string) error {
323323
return nil
324324
}
325325

326+
// SchemaDiff generates diff between Database Lab clone and its snapshot.
327+
func (c *Client) SchemaDiff(ctx context.Context, cloneID string) ([]byte, error) {
328+
u := c.URL(fmt.Sprintf("/clone/diff/%s", cloneID))
329+
330+
request, err := http.NewRequest(http.MethodGet, u.String(), nil)
331+
if err != nil {
332+
return nil, errors.Wrap(err, "failed to make a request")
333+
}
334+
335+
response, err := c.Do(ctx, request)
336+
if err != nil {
337+
return nil, errors.Wrap(err, "failed to get response")
338+
}
339+
340+
defer func() { _ = response.Body.Close() }()
341+
342+
data, err := io.ReadAll(response.Body)
343+
if err != nil {
344+
return nil, errors.Wrap(err, "failed to read response")
345+
}
346+
347+
return data, nil
348+
}
349+
326350
// StartObservation starts a new clone observation.
327351
func (c *Client) StartObservation(ctx context.Context, startRequest types.StartObservationRequest) (*observer.Session, error) {
328352
u := c.URL("/observation/start")

0 commit comments

Comments
 (0)