Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(db/create): ask to wake sleeping groups before create #900

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions internal/cmd/db_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ var createCmd = &cobra.Command{
return err
}

awake, err := ensureGroupAwake(client, group)
if err != nil {
return err
}
if !awake {
return fmt.Errorf("cannot create a database in an archived group. Please wake up the group first")
}

location, err := locationFromFlag(client)
if err != nil {
return err
Expand Down
10 changes: 10 additions & 0 deletions internal/cmd/db_shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ var shellCmd = &cobra.Command{
return err
}

spinner.Stop()
awake, err := ensureGroupAwake(client, db.Group)
if err != nil {
return err
}
if !awake {
return fmt.Errorf("cannot connect to a database in an archived group. Please wake up the group first")
}
spinner.Start()

var claim *turso.PermissionsClaim
if len(flags.AttachClaims()) > 0 {
err := validateDBNames(client, flags.AttachClaims())
Expand Down
36 changes: 36 additions & 0 deletions internal/cmd/group_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"fmt"
"strings"

"github.com/tursodatabase/turso-cli/internal"
"github.com/tursodatabase/turso-cli/internal/turso"
)

func ensureGroupAwake(client *turso.Client, groupName string) (bool, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of this approach because:

  • We add an extra call on all invocations to handle an edge case that happens once in a while
  • We have to keep track of all ops that are affected by archiving and remember to add this logic to them (e.g. rotating creds, and deleting dbs also doesn't work when the group is archived)

The "better" way would be to be able to detect archived errors from API responses and handle those properly.

Not a blocker though, feel free to move forward if you don't have the time to address this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this would be better.

group, err := getGroup(client, groupName)
if err != nil {
return false, fmt.Errorf("failed to get group info: %w", err)
}

groupStatus := aggregateGroupStatus(group)
if groupStatus != "Archived 💤" {
notrab marked this conversation as resolved.
Show resolved Hide resolved
return true, nil
}

fmt.Printf("The group %s is currently archived. Do you want to wake it up? [Y/n]: ", internal.Emph(groupName))
notrab marked this conversation as resolved.
Show resolved Hide resolved
var response string
fmt.Scanln(&response)
response = strings.ToLower(strings.TrimSpace(response))
if response == "" || response == "y" || response == "yes" {
err = unarchiveGroup(client, groupName)
if err != nil {
return false, fmt.Errorf("failed to wake up group: %w", err)
}
fmt.Printf("Group %s has been woken up.\n", internal.Emph(groupName))
return true, nil
}

return false, nil
}
Loading