Skip to content

Commit 1b0edcc

Browse files
committed
feat: add download_spatial target and DuckDB spatial extension handling
1 parent f07259a commit 1b0edcc

File tree

5 files changed

+41
-23
lines changed

5 files changed

+41
-23
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ build-prod:
227227
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'"; \
228228
go build -ldflags="-w -s $$LDFLAGS" -o $(BINARY_NAME) $(MAIN_PATH)
229229

230+
download_spatial:
231+
go run cmd/download/duckdb/duckdb.go
232+
230233
clean-db:
231234
@echo "Cleaning database..."
232235
@if [ $$(docker volume ls -q -f name=weknora_postgres-data) ]; then \

cmd/download/duckdb/duckdb.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
7+
_ "github.com/duckdb/duckdb-go/v2"
8+
)
9+
10+
func downloadSpatial() {
11+
ctx := context.Background()
12+
13+
sqlDB, err := sql.Open("duckdb", ":memory:")
14+
if err != nil {
15+
panic(err)
16+
}
17+
18+
// Try to install spatial extension (may already be installed or network unavailable)
19+
installSQL := "INSTALL spatial;"
20+
if _, err := sqlDB.ExecContext(ctx, installSQL); err != nil {
21+
panic(err)
22+
}
23+
24+
// Try to load spatial extension
25+
loadSQL := "LOAD spatial;"
26+
if _, err := sqlDB.ExecContext(ctx, loadSQL); err != nil {
27+
panic(err)
28+
}
29+
}
30+
31+
func main() {
32+
downloadSpatial()
33+
}

docker/Dockerfile.app

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ ENV BUILD_TIME=${BUILD_TIME_ARG}
4242
ENV GO_VERSION=${GO_VERSION_ARG}
4343

4444
# Build the application with version info
45+
RUN --mount=type=cache,target=/go/pkg/mod make download_spatial
4546
RUN --mount=type=cache,target=/go/pkg/mod make build-prod
4647
RUN --mount=type=cache,target=/go/pkg/mod cp -r /go/pkg/mod/github.com/yanyiwu/ /app/yanyiwu/
4748

@@ -85,6 +86,7 @@ COPY --from=builder /app/config ./config
8586
COPY --from=builder /app/scripts ./scripts
8687
COPY --from=builder /app/migrations ./migrations
8788
COPY --from=builder /app/dataset/samples ./dataset/samples
89+
COPY --from=builder /root/.duckdb /home/appuser/.duckdb
8890
COPY --from=builder /app/WeKnora .
8991

9092
# Make scripts executable

internal/agent/tools/data_analysis.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/Tencent/WeKnora/internal/types"
1212
"github.com/Tencent/WeKnora/internal/types/interfaces"
1313
"github.com/Tencent/WeKnora/internal/utils"
14-
_ "github.com/duckdb/duckdb-go/v2"
1514
)
1615

1716
var dataAnalysisTool = BaseTool{

internal/container/container.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515
"time"
1616

17+
_ "github.com/duckdb/duckdb-go/v2"
1718
esv7 "github.com/elastic/go-elasticsearch/v7"
1819
"github.com/elastic/go-elasticsearch/v8"
1920
"github.com/neo4j/neo4j-go-driver/v6/neo4j"
@@ -636,30 +637,10 @@ func NewDuckDB() (*sql.DB, error) {
636637
return nil, fmt.Errorf("failed to open duckdb: %w", err)
637638
}
638639

639-
// Install and load the spatial extension with timeout to avoid blocking startup
640-
// The extension installation may download from network which can be slow or hang
641-
installTimeout := 30 * time.Second
642-
ctx, cancel := context.WithTimeout(context.Background(), installTimeout)
643-
defer cancel()
644-
645-
// Try to install spatial extension (may already be installed or network unavailable)
646-
installSQL := "INSTALL spatial;"
647-
if _, err := sqlDB.ExecContext(ctx, installSQL); err != nil {
648-
if ctx.Err() == context.DeadlineExceeded {
649-
logger.Warnf(context.Background(), "[DuckDB] Spatial extension installation timed out after %v, skipping", installTimeout)
650-
} else {
651-
logger.Warnf(context.Background(), "[DuckDB] Failed to install spatial extension (may already be installed): %v", err)
652-
}
653-
}
654-
655640
// Try to load spatial extension
656641
loadSQL := "LOAD spatial;"
657-
if _, err := sqlDB.ExecContext(ctx, loadSQL); err != nil {
658-
if ctx.Err() == context.DeadlineExceeded {
659-
logger.Warnf(context.Background(), "[DuckDB] Spatial extension loading timed out, skipping")
660-
} else {
661-
logger.Warnf(context.Background(), "[DuckDB] Failed to load spatial extension: %v", err)
662-
}
642+
if _, err := sqlDB.ExecContext(context.Background(), loadSQL); err != nil {
643+
logger.Warnf(context.Background(), "[DuckDB] Failed to load spatial extension: %v", err)
663644
}
664645

665646
return sqlDB, nil

0 commit comments

Comments
 (0)