feat: implement automatic soft-delete filtering in database queries

- Added automatic filtering for `deleted_at IS NULL` in database queries to ensure only active records are returned.
- Updated the `database_query.go` documentation to reflect the new soft-delete feature and its implications for query construction.
- Enhanced the SQL validation process to include soft-delete conditions, improving data integrity and security.

These changes enhance the robustness of database interactions by enforcing soft-delete logic across relevant queries.
This commit is contained in:
wizardchen
2026-03-03 22:10:46 +08:00
committed by lyingbug
parent e2234e4960
commit 58783ec96c
2 changed files with 77 additions and 18 deletions
+6 -3
View File
@@ -18,6 +18,7 @@ var databaseQueryTool = BaseTool{
## Security Features
- Automatic tenant_id injection: All queries are automatically filtered by the logged-in user's tenant_id
- Automatic soft-delete filtering: All queries are automatically filtered to include only records with deleted_at IS NULL
- Read-only queries: Only SELECT statements are allowed
- Safe tables: Only allow queries on authorized tables (knowledge_bases, knowledges, chunks)
@@ -30,7 +31,7 @@ var databaseQueryTool = BaseTool{
- tenant_id (INTEGER): Owner tenant ID
- embedding_model_id, summary_model_id, rerank_model_id (VARCHAR): Model IDs
- vlm_config (JSON): Includes VLM settings such as enabled flag and model_id
- created_at, updated_at (TIMESTAMP)
- created_at, updated_at, deleted_at (TIMESTAMP)
### knowledges (documents)
- id (VARCHAR): Document ID
@@ -44,7 +45,7 @@ var databaseQueryTool = BaseTool{
- enable_status (VARCHAR): Enable status (enabled/disabled)
- file_name, file_type (VARCHAR): File information
- file_size, storage_size (BIGINT): Size in bytes
- created_at, updated_at, processed_at (TIMESTAMP)
- created_at, updated_at, processed_at, deleted_at (TIMESTAMP)
@@ -57,7 +58,7 @@ var databaseQueryTool = BaseTool{
- chunk_index (INTEGER): Index in document
- is_enabled (BOOLEAN): Enable status
- chunk_type (VARCHAR): Type (text/image/table)
- created_at, updated_at (TIMESTAMP)
- created_at, updated_at, deleted_at (TIMESTAMP)
## Usage Examples
@@ -88,6 +89,7 @@ Join knowledge bases and documents:
## Important Notes
- DO NOT include tenant_id in WHERE clause - it's automatically added
- DO NOT include deleted_at filtering manually unless needed - default query already enforces deleted_at IS NULL
- Only SELECT queries are allowed
- Limit results with LIMIT clause for better performance
- Use appropriate JOINs when querying across tables
@@ -257,6 +259,7 @@ func (t *DatabaseQueryTool) validateAndSecureSQL(sqlQuery string, tenantID uint6
securedSQL, validationResult, err := utils.ValidateAndSecureSQL(
sqlQuery,
utils.WithSecurityDefaults(tenantID),
utils.WithSoftDeleteFilter("knowledge_bases", "knowledges", "chunks"),
utils.WithInjectionRiskCheck(),
)
if err != nil {