feat: add download_spatial target and DuckDB spatial extension handling
This commit is contained in:
@@ -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'"; \
|
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)
|
go build -ldflags="-w -s $$LDFLAGS" -o $(BINARY_NAME) $(MAIN_PATH)
|
||||||
|
|
||||||
|
download_spatial:
|
||||||
|
go run cmd/download/duckdb/duckdb.go
|
||||||
|
|
||||||
clean-db:
|
clean-db:
|
||||||
@echo "Cleaning database..."
|
@echo "Cleaning database..."
|
||||||
@if [ $$(docker volume ls -q -f name=weknora_postgres-data) ]; then \
|
@if [ $$(docker volume ls -q -f name=weknora_postgres-data) ]; then \
|
||||||
|
|||||||
@@ -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()
|
||||||
|
}
|
||||||
@@ -42,6 +42,7 @@ ENV BUILD_TIME=${BUILD_TIME_ARG}
|
|||||||
ENV GO_VERSION=${GO_VERSION_ARG}
|
ENV GO_VERSION=${GO_VERSION_ARG}
|
||||||
|
|
||||||
# Build the application with version info
|
# 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 make build-prod
|
||||||
RUN --mount=type=cache,target=/go/pkg/mod cp -r /go/pkg/mod/github.com/yanyiwu/ /app/yanyiwu/
|
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/scripts ./scripts
|
||||||
COPY --from=builder /app/migrations ./migrations
|
COPY --from=builder /app/migrations ./migrations
|
||||||
COPY --from=builder /app/dataset/samples ./dataset/samples
|
COPY --from=builder /app/dataset/samples ./dataset/samples
|
||||||
|
COPY --from=builder /root/.duckdb /home/appuser/.duckdb
|
||||||
COPY --from=builder /app/WeKnora .
|
COPY --from=builder /app/WeKnora .
|
||||||
|
|
||||||
# Make scripts executable
|
# Make scripts executable
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||||
"github.com/Tencent/WeKnora/internal/utils"
|
"github.com/Tencent/WeKnora/internal/utils"
|
||||||
_ "github.com/duckdb/duckdb-go/v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var dataAnalysisTool = BaseTool{
|
var dataAnalysisTool = BaseTool{
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
_ "github.com/duckdb/duckdb-go/v2"
|
||||||
esv7 "github.com/elastic/go-elasticsearch/v7"
|
esv7 "github.com/elastic/go-elasticsearch/v7"
|
||||||
"github.com/elastic/go-elasticsearch/v8"
|
"github.com/elastic/go-elasticsearch/v8"
|
||||||
"github.com/neo4j/neo4j-go-driver/v6/neo4j"
|
"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)
|
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
|
// Try to load spatial extension
|
||||||
loadSQL := "LOAD spatial;"
|
loadSQL := "LOAD spatial;"
|
||||||
if _, err := sqlDB.ExecContext(ctx, loadSQL); err != nil {
|
if _, err := sqlDB.ExecContext(context.Background(), loadSQL); err != nil {
|
||||||
if ctx.Err() == context.DeadlineExceeded {
|
logger.Warnf(context.Background(), "[DuckDB] Failed to load spatial extension: %v", err)
|
||||||
logger.Warnf(context.Background(), "[DuckDB] Spatial extension loading timed out, skipping")
|
|
||||||
} else {
|
|
||||||
logger.Warnf(context.Background(), "[DuckDB] Failed to load spatial extension: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return sqlDB, nil
|
return sqlDB, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user