Skip to content

Commit 9ec5224

Browse files
authored
Show database type in db commands (#1034)
Adds `Type` field to `db show` and `db list` commands to distinguish between `tursodb` and `sqlite` ### DB Show <img width="977" height="298" alt="image" src="https://github.com/user-attachments/assets/1488aa04-4e3d-41f5-8e20-583d1333c11d" /> ### DB List <img width="977" height="265" alt="image" src="https://github.com/user-attachments/assets/a56b0817-e6c3-420f-be78-2de25b346c3a" />
2 parents 92e0f38 + e876d79 commit 9ec5224

4 files changed

Lines changed: 134 additions & 0 deletions

File tree

internal/cmd/db_show.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/dustin/go-humanize"
9+
"github.com/google/uuid"
910

1011
"github.com/spf13/cobra"
1112
"github.com/tursodatabase/turso-cli/internal"
@@ -113,6 +114,7 @@ var showCmd = &cobra.Command{
113114
fmt.Println("Archived: ", formatBool(db.Sleeping))
114115
fmt.Println("Bytes Synced: ", humanize.Bytes(dbUsage.Usage.BytesSynced))
115116
fmt.Println("Is Schema: ", formatBool(db.IsSchema))
117+
fmt.Println("Type: ", databaseType(dbUsage.UUID))
116118
fmt.Println("Delete Protection: ", formatBool(config.IsDeleteProtected()))
117119
if db.Schema != "" {
118120
fmt.Println("Schema: ", db.Schema)
@@ -129,3 +131,18 @@ var showCmd = &cobra.Command{
129131
return nil
130132
},
131133
}
134+
135+
func isTursoDB(dbUUID string) bool {
136+
id, err := uuid.Parse(dbUUID)
137+
if err != nil {
138+
return false
139+
}
140+
return id[5] == 0x10
141+
}
142+
143+
func databaseType(dbUUID string) string {
144+
if isTursoDB(dbUUID) {
145+
return "Turso"
146+
}
147+
return "SQLite"
148+
}

internal/cmd/db_show_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cmd
2+
3+
import "testing"
4+
5+
func TestIsTursoDB(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
uuid string
9+
turso bool
10+
}{
11+
{
12+
name: "type byte is 0x10",
13+
uuid: "019db7a2-9210-79e5-afed-0b1755901d50",
14+
turso: true,
15+
},
16+
{
17+
name: "type byte is not 0x10",
18+
uuid: "00000000-1100-0000-0000-000000000000",
19+
turso: false,
20+
},
21+
{
22+
name: "invalid uuid",
23+
uuid: "not-a-uuid",
24+
turso: false,
25+
},
26+
{
27+
name: "empty uuid",
28+
uuid: "",
29+
turso: false,
30+
},
31+
}
32+
33+
for _, tt := range tests {
34+
t.Run(tt.name, func(t *testing.T) {
35+
got := isTursoDB(tt.uuid)
36+
if got != tt.turso {
37+
t.Fatalf("isTursoDB(%q) = %v, want %v", tt.uuid, got, tt.turso)
38+
}
39+
})
40+
}
41+
}
42+
43+
func TestDatabaseType(t *testing.T) {
44+
tests := []struct {
45+
uuid string
46+
want string
47+
}{
48+
{
49+
uuid: "019db7a2-9210-79e5-afed-0b1755901d50",
50+
want: "Turso",
51+
},
52+
{
53+
uuid: "00000000-1100-0000-0000-000000000000",
54+
want: "SQLite",
55+
},
56+
{
57+
uuid: "",
58+
want: "SQLite",
59+
},
60+
}
61+
62+
for _, tt := range tests {
63+
t.Run(tt.want, func(t *testing.T) {
64+
got := databaseType(tt.uuid)
65+
if got != tt.want {
66+
t.Fatalf("databaseType(%q) = %q, want %q", tt.uuid, got, tt.want)
67+
}
68+
})
69+
}
70+
}

internal/cmd/list_databases.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,14 @@ func (m dbListModel) View() string {
116116

117117
for _, database := range m.databases {
118118
row := []string{database.Name}
119+
row = append(row, databaseType(database.ID))
119120
row = append(row, formatGroup(database.Group))
120121
row = append(row, getDatabaseUrl(&database))
121122
data = append(data, row)
122123
}
123124

124125
headers = append(headers, "NAME")
126+
headers = append(headers, "TYPE")
125127
headers = append(headers, "GROUP")
126128
headers = append(headers, "URL")
127129

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/tursodatabase/turso-cli/internal/turso"
8+
)
9+
10+
func TestDbListModelViewIncludesTypeColumn(t *testing.T) {
11+
model := dbListModel{
12+
databases: []turso.Database{
13+
{
14+
Name: "sqlite-db",
15+
ID: "00000000-1100-0000-0000-000000000000",
16+
Hostname: "sqlite-db.example.com",
17+
},
18+
{
19+
Name: "turso-db",
20+
ID: "019db7a2-9210-79e5-afed-0b1755901d50",
21+
Group: "default",
22+
Hostname: "turso-db.example.com",
23+
},
24+
},
25+
}
26+
27+
view := model.View()
28+
lines := strings.Split(strings.TrimSpace(view), "\n")
29+
expected := [][]string{
30+
{"NAME", "TYPE", "GROUP", "URL"},
31+
{"sqlite-db", "SQLite", "-", "libsql://sqlite-db.example.com"},
32+
{"turso-db", "Turso", "default", "libsql://turso-db.example.com"},
33+
}
34+
35+
if len(lines) != len(expected) {
36+
t.Fatalf("expected %d lines, got %d:\n%s", len(expected), len(lines), view)
37+
}
38+
39+
for i, want := range expected {
40+
got := strings.Fields(lines[i])
41+
if strings.Join(got, " ") != strings.Join(want, " ") {
42+
t.Fatalf("line %d fields = %q, want %q:\n%s", i, got, want, view)
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)