diff --git a/libsql/internal/http/hranaV2/hranaV2.go b/libsql/internal/http/hranaV2/hranaV2.go index feacdc7..0104911 100644 --- a/libsql/internal/http/hranaV2/hranaV2.go +++ b/libsql/internal/http/hranaV2/hranaV2.go @@ -89,6 +89,15 @@ type hranaV2Conn struct { streamClosed bool } +func (h *hranaV2Conn) Ping() error { + return h.PingContext(context.Background()) +} + +func (h *hranaV2Conn) PingContext(ctx context.Context) error { + _, err := h.executeStmt(ctx, "SELECT 1", nil, false) + return err +} + func (h *hranaV2Conn) Prepare(query string) (driver.Stmt, error) { return h.PrepareContext(context.Background(), query) } diff --git a/tests/http/driver_test.go b/tests/http/driver_test.go index c0e6339..a26527a 100644 --- a/tests/http/driver_test.go +++ b/tests/http/driver_test.go @@ -239,6 +239,24 @@ func (t Tx) prepareInsertStmt() PreparedStmt { return PreparedStmt{stmt, t.t} } +func TestPing(t *testing.T) { + t.Parallel() + db := getDb(T{t}) + + // This ping should succeed because the database is up and running + db.t.FatalOnError(db.Ping()) + + t.Cleanup(func() { + db.Close() + + // This ping should return an error because the database is already closed + err := db.Ping() + if err == nil { + db.t.Fatal("db.Ping succeeded when it should have failed") + } + }) +} + func TestExecAndQuery(t *testing.T) { t.Parallel() db := getDb(T{t})