Skip to content

Commit 7cd3e65

Browse files
fix: improve sqlite support
1 parent 40de03d commit 7cd3e65

6 files changed

Lines changed: 58 additions & 4 deletions

File tree

assets/demo.gif

-1.88 MB
Binary file not shown.

cmd/app.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var appAddCmd = &cobra.Command{
5858
Short: "Add a new app to monitor",
5959
Long: `Add a new application to monitor with its database connection.
6060
61-
Supported database types: postgres, mysql, mongodb
61+
Supported database types: postgres, mysql, mongodb, sqlite
6262
6363
Examples:
6464
dashmin app add myapp postgres "postgres://readonly:password@localhost:5432/myapp?sslmode=disable"
@@ -74,10 +74,11 @@ Examples:
7474
"postgres": true,
7575
"mysql": true,
7676
"mongodb": true,
77+
"sqlite": true,
7778
}
7879

7980
if !validTypes[dbType] {
80-
return fmt.Errorf("invalid database type '%s'. Supported: postgres, mysql, mongodb", dbType)
81+
return fmt.Errorf("invalid database type '%s'. Supported: postgres, mysql, mongodb, sqlite", dbType)
8182
}
8283

8384
cfg, err := config.Load()
@@ -265,6 +266,8 @@ Examples:
265266
fmt.Printf(" user:password@tcp(host:port)/database\n")
266267
case "mongodb":
267268
fmt.Printf(" mongodb://user:password@host:port/database\n")
269+
case "sqlite":
270+
fmt.Printf(" sqlite:///path/to/database.db\n")
268271
}
269272
fmt.Printf(" - Check network connectivity\n")
270273
fmt.Printf(" - Verify credentials are correct\n")
@@ -277,7 +280,7 @@ Examples:
277280
fmt.Printf("Testing basic query...\n")
278281
var testQuery string
279282
switch app.Type {
280-
case "postgres", "mysql":
283+
case "postgres", "mysql", "sqlite":
281284
testQuery = "SELECT 1 as test"
282285
case "mongodb":
283286
testQuery = "test.count({})"

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/charmbracelet/lipgloss v1.1.0
88
github.com/go-sql-driver/mysql v1.9.3
99
github.com/jackc/pgx/v5 v5.7.5
10+
github.com/mattn/go-sqlite3 v1.14.33
1011
github.com/spf13/cobra v1.10.1
1112
go.mongodb.org/mongo-driver v1.17.4
1213
golang.org/x/sync v0.19.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2J
5151
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
5252
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
5353
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
54+
github.com/mattn/go-sqlite3 v1.14.33 h1:A5blZ5ulQo2AtayQ9/limgHEkFreKj1Dv226a1K73s0=
55+
github.com/mattn/go-sqlite3 v1.14.33/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
5456
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
5557
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
5658
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=

internal/db/connections.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
_ "github.com/go-sql-driver/mysql"
1212
_ "github.com/jackc/pgx/v5/stdlib"
13+
_ "github.com/mattn/go-sqlite3"
1314
"go.mongodb.org/mongo-driver/bson"
1415
"go.mongodb.org/mongo-driver/mongo"
1516
"go.mongodb.org/mongo-driver/mongo/options"
@@ -196,6 +197,24 @@ func ConnectMongoDB(connectionString string) (Connection, error) {
196197
return &MongoConnection{client: client, dbName: dbName}, nil
197198
}
198199

200+
// ConnectSQLite connects to SQLite using a file path
201+
// Connection string format: sqlite:///path/to/database.db
202+
func ConnectSQLite(connectionString string) (Connection, error) {
203+
// Remove the sqlite:// prefix if present
204+
dbPath := strings.TrimPrefix(connectionString, "sqlite://")
205+
206+
db, err := sql.Open("sqlite3", dbPath)
207+
if err != nil {
208+
return nil, fmt.Errorf("failed to connect to sqlite: %w", err)
209+
}
210+
211+
if err := db.Ping(); err != nil {
212+
return nil, fmt.Errorf("failed to ping sqlite: %w", err)
213+
}
214+
215+
return &SQLConnection{db: db}, nil
216+
}
217+
199218
// ConnectByType connects to a database given its type and connection string
200219
func ConnectByType(dbType, connectionString string) (Connection, error) {
201220
switch dbType {
@@ -205,6 +224,8 @@ func ConnectByType(dbType, connectionString string) (Connection, error) {
205224
return ConnectMySQL(connectionString)
206225
case "mongodb":
207226
return ConnectMongoDB(connectionString)
227+
case "sqlite":
228+
return ConnectSQLite(connectionString)
208229
default:
209230
return nil, fmt.Errorf("unsupported database type: %s", dbType)
210231
}

internal/db/schema.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ func GetDatabaseSchema(conn Connection, dbType string) (string, error) {
1111
return getPostgresSchema(conn)
1212
case "mysql":
1313
return getMySQLSchema(conn)
14+
case "sqlite":
15+
return getSQLiteSchema(conn)
1416
case "mongodb":
1517
return getMongoDBSchema(conn)
1618
default:
@@ -66,6 +68,31 @@ func getMySQLSchema(conn Connection) (string, error) {
6668
return formatSchemaResult(result, "MySQL"), nil
6769
}
6870

71+
func getSQLiteSchema(conn Connection) (string, error) {
72+
query := `
73+
SELECT
74+
m.name as table_name,
75+
p.name as column_name,
76+
p.type as data_type,
77+
CASE WHEN p."notnull" = 0 THEN 'YES' ELSE 'NO' END as is_nullable
78+
FROM sqlite_master m
79+
JOIN pragma_table_info(m.name) p
80+
WHERE m.type = 'table' AND m.name NOT LIKE 'sqlite_%'
81+
ORDER BY m.name, p.cid
82+
`
83+
84+
result, err := conn.Query(query)
85+
if err != nil {
86+
return "", fmt.Errorf("failed to get SQLite schema: %w", err)
87+
}
88+
89+
if result.Error != nil {
90+
return "", fmt.Errorf("schema query error: %w", result.Error)
91+
}
92+
93+
return formatSchemaResult(result, "SQLite"), nil
94+
}
95+
6996
func getMongoDBSchema(conn Connection) (string, error) {
7097
return `MongoDB collections (common field patterns):
7198
- users: {_id, email, name, status, created_at/createdAt, updated_at/updatedAt}
@@ -112,4 +139,4 @@ func formatSchemaResult(result *Result, dbType string) string {
112139
}
113140

114141
return schema.String()
115-
}
142+
}

0 commit comments

Comments
 (0)