Skip to content

Commit c9410bb

Browse files
authored
Fix bug 'not being able to create db in existing fly group' and improve errors (#949)
Free tier user was not able to create a DB in an existing fly group after AWS rollout. Logic after this PR: - If location flag is NOT provided, and user has no groups, defaults to `closestLocation`. `isValidLocation` check is made. - If location flag is NOT provided, and user has groups, defaults to `group.Primary`. `isValidLocation` check is NOT made. - If location flag IS provided, and user has no groups, uses that location. `isValidLocation` check is made. - If location flag IS provided, and user HAS groups, uses that location, but the group must also have that location. `isValidLocation` check is NOT made. Also improve the error messages a bit
2 parents 8bf4d82 + 2fe4a96 commit c9410bb

1 file changed

Lines changed: 36 additions & 21 deletions

File tree

internal/cmd/db_create.go

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,18 @@ func CreateDatabase(name string) error {
6666
return err
6767
}
6868

69-
group, err := groupFromFlag(client)
69+
groups, err := client.Groups.List()
70+
if err != nil {
71+
return err
72+
}
73+
74+
group, err := groupFromFlag(groups)
7075
if err != nil {
7176
return err
7277
}
7378
groupName := group.Name
7479

75-
location, err := locationFromFlag(client)
80+
location, err := locationFromFlag(client, group, groups)
7681
if err != nil {
7782
return err
7883
}
@@ -88,7 +93,7 @@ func CreateDatabase(name string) error {
8893
version = "canary"
8994
}
9095

91-
if err := ensureGroup(client, groupName, location, version); err != nil {
96+
if err := ensureGroup(client, groupName, groups, location, version); err != nil {
9297
return err
9398
}
9499

@@ -114,9 +119,9 @@ func CreateDatabase(name string) error {
114119
return nil
115120
}
116121

117-
func ensureGroup(client *turso.Client, group, location, version string) error {
118-
if ok, err := shouldCreateGroup(client, group, location); !ok {
119-
return err
122+
func ensureGroup(client *turso.Client, group string, groups []turso.Group, location, version string) error {
123+
if !shouldAutoCreateGroup(group, groups) {
124+
return nil
120125
}
121126
if err := createGroup(client, group, location, version); err != nil {
122127
return err
@@ -137,22 +142,18 @@ func getDatabaseName(args []string) (string, error) {
137142
}
138143

139144
// Returns (group, error)
140-
func groupFromFlag(client *turso.Client) (turso.Group, error) {
141-
groups, err := getGroups(client)
142-
if err != nil {
143-
return turso.Group{}, err
144-
}
145+
func groupFromFlag(groups []turso.Group) (turso.Group, error) {
145146

146147
if groupFlag != "" {
147148
if !groupExists(groups, groupFlag) {
148-
return turso.Group{}, fmt.Errorf("group %s does not exist", groupFlag)
149+
return turso.Group{}, fmt.Errorf("group %s does not exist. Please double-check the name. You can run 'turso group list' to get a list of your groups, or 'turso group create' to make a new one", groupFlag)
149150
}
150151
for _, group := range groups {
151152
if group.Name == groupFlag {
152153
return group, nil
153154
}
154155
}
155-
return turso.Group{}, fmt.Errorf("group %s does not exist", groupFlag)
156+
return turso.Group{}, fmt.Errorf("group %s does not exist. Please double-check the name. You can run 'turso group list' to get a list of your groups, or 'turso group create' to make a new one", groupFlag)
156157
}
157158

158159
switch {
@@ -175,22 +176,36 @@ func groupExists(groups []turso.Group, name string) bool {
175176
return false
176177
}
177178

178-
func locationFromFlag(client *turso.Client) (string, error) {
179+
func locationFromFlag(client *turso.Client, group turso.Group, groups []turso.Group) (string, error) {
179180
loc := locationFlag
181+
groupWillBeAutoCreated := shouldAutoCreateGroup(group.Name, groups)
180182
if loc == "" {
181-
loc, _ = closestLocation(client)
183+
if groupWillBeAutoCreated {
184+
loc, _ = closestLocation(client)
185+
} else {
186+
loc = group.Primary
187+
}
188+
}
189+
if !groupWillBeAutoCreated {
190+
var groupContainsLocation bool
191+
for _, l := range group.Locations {
192+
if l == loc {
193+
groupContainsLocation = true
194+
}
195+
}
196+
if !groupContainsLocation {
197+
return "", fmt.Errorf("location '%s' is not valid for group '%s'. The group has the following locations: %v. You can use 'turso group locations add' to add a new location to the group", loc, group.Name, strings.Join(group.Locations, ", "))
198+
}
199+
200+
return loc, nil
182201
}
183202
if !isValidLocation(client, loc) {
184203
return "", fmt.Errorf("location '%s' is not valid", loc)
185204
}
186205
return loc, nil
187206
}
188207

189-
func shouldCreateGroup(client *turso.Client, name, location string) (bool, error) {
190-
groups, err := getGroups(client)
191-
if err != nil {
192-
return false, err
193-
}
208+
func shouldAutoCreateGroup(name string, groups []turso.Group) bool {
194209
// we only create the default group automatically
195-
return name == "default" && len(groups) == 0, nil
210+
return name == "default" && len(groups) == 0
196211
}

0 commit comments

Comments
 (0)