Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat: add download_spatial target and DuckDB spatial extension handling

begoniezhao1b0edccfc47f
diff --git a/Makefile b/Makefile
index 49943514d..fe4885099 100644
--- a/Makefile
+++ b/Makefile
@@ -227,6 +227,9 @@ build-prod:
LDFLAGS="-X 'github.com/Tencent/WeKnora/internal/handler.Version=$$VERSION' -X 'github.com/Tencent/WeKnora/internal/handler.CommitID=$$COMMIT_ID' -X 'github.com/Tencent/WeKnora/internal/handler.BuildTime=$$BUILD_TIME' -X 'github.com/Tencent/WeKnora/internal/handler.GoVersion=$$GO_VERSION'"; \
go build -ldflags="-w -s $$LDFLAGS" -o $(BINARY_NAME) $(MAIN_PATH)
+download_spatial:
+ go run cmd/download/duckdb/duckdb.go
+
clean-db:
@echo "Cleaning database..."
@if [ $$(docker volume ls -q -f name=weknora_postgres-data) ]; then \
diff --git a/cmd/download/duckdb/duckdb.go b/cmd/download/duckdb/duckdb.go
new file mode 100644
index 000000000..838851a9f
--- /dev/null
+++ b/cmd/download/duckdb/duckdb.go
@@ -0,0 +1,33 @@
+package main
+
+import (
+ "context"
+ "database/sql"
+
+ _ "github.com/duckdb/duckdb-go/v2"
+)
+
+func downloadSpatial() {
+ ctx := context.Background()
+
+ sqlDB, err := sql.Open("duckdb", ":memory:")
+ if err != nil {
+ panic(err)
+ }
+
+ // Try to install spatial extension (may already be installed or network unavailable)
+ installSQL := "INSTALL spatial;"
+ if _, err := sqlDB.ExecContext(ctx, installSQL); err != nil {
+ panic(err)
+ }
+
+ // Try to load spatial extension
+ loadSQL := "LOAD spatial;"
+ if _, err := sqlDB.ExecContext(ctx, loadSQL); err != nil {
+ panic(err)
+ }
+}
+
+func main() {
+ downloadSpatial()
+}
diff --git a/docker/Dockerfile.app b/docker/Dockerfile.app
index a04868eec..01803fbf3 100644
--- a/docker/Dockerfile.app
+++ b/docker/Dockerfile.app
@@ -42,6 +42,7 @@ ENV BUILD_TIME=${BUILD_TIME_ARG}
ENV GO_VERSION=${GO_VERSION_ARG}
# Build the application with version info
+RUN --mount=type=cache,target=/go/pkg/mod make download_spatial
RUN --mount=type=cache,target=/go/pkg/mod make build-prod
RUN --mount=type=cache,target=/go/pkg/mod cp -r /go/pkg/mod/github.com/yanyiwu/ /app/yanyiwu/
@@ -85,6 +86,7 @@ COPY --from=builder /app/config ./config
COPY --from=builder /app/scripts ./scripts
COPY --from=builder /app/migrations ./migrations
COPY --from=builder /app/dataset/samples ./dataset/samples
+COPY --from=builder /root/.duckdb /home/appuser/.duckdb
COPY --from=builder /app/WeKnora .
# Make scripts executable
diff --git a/internal/agent/tools/data_analysis.go b/internal/agent/tools/data_analysis.go
index 864bffad4..ba9fc626d 100644
--- a/internal/agent/tools/data_analysis.go
+++ b/internal/agent/tools/data_analysis.go
@@ -11,7 +11,6 @@ import (
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/Tencent/WeKnora/internal/utils"
- _ "github.com/duckdb/duckdb-go/v2"
)
var dataAnalysisTool = BaseTool{
diff --git a/internal/container/container.go b/internal/container/container.go
index 7c5bdea62..ade5b7ab4 100644
--- a/internal/container/container.go
+++ b/internal/container/container.go
@@ -14,6 +14,7 @@ import (
"strings"
"time"
+ _ "github.com/duckdb/duckdb-go/v2"
esv7 "github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v8"
"github.com/neo4j/neo4j-go-driver/v6/neo4j"
@@ -636,30 +637,10 @@ func NewDuckDB() (*sql.DB, error) {
return nil, fmt.Errorf("failed to open duckdb: %w", err)
}
- // Install and load the spatial extension with timeout to avoid blocking startup
- // The extension installation may download from network which can be slow or hang
- installTimeout := 30 * time.Second
- ctx, cancel := context.WithTimeout(context.Background(), installTimeout)
- defer cancel()
-
- // Try to install spatial extension (may already be installed or network unavailable)
- installSQL := "INSTALL spatial;"
- if _, err := sqlDB.ExecContext(ctx, installSQL); err != nil {
- if ctx.Err() == context.DeadlineExceeded {
- logger.Warnf(context.Background(), "[DuckDB] Spatial extension installation timed out after %v, skipping", installTimeout)
- } else {
- logger.Warnf(context.Background(), "[DuckDB] Failed to install spatial extension (may already be installed): %v", err)
- }
- }
-
// Try to load spatial extension
loadSQL := "LOAD spatial;"
- if _, err := sqlDB.ExecContext(ctx, loadSQL); err != nil {
- if ctx.Err() == context.DeadlineExceeded {
- logger.Warnf(context.Background(), "[DuckDB] Spatial extension loading timed out, skipping")
- } else {
- logger.Warnf(context.Background(), "[DuckDB] Failed to load spatial extension: %v", err)
- }
+ if _, err := sqlDB.ExecContext(context.Background(), loadSQL); err != nil {
+ logger.Warnf(context.Background(), "[DuckDB] Failed to load spatial extension: %v", err)
}
return sqlDB, nil