feat: add download_spatial target and DuckDB spatial extension handling

This commit is contained in:
begoniezhao
2026-01-05 14:52:11 +08:00
parent f07259a423
commit 1b0edccfc4
5 changed files with 41 additions and 23 deletions
+3
View File
@@ -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 \
+33
View File
@@ -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()
}
+2
View File
@@ -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
-1
View File
@@ -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{
+3 -22
View File
@@ -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