fix: SQLite/Lite mode compatibility for FAQ import and chunk operations
Multiple fixes for WeKnora Lite mode (SQLite, no Redis):
1. Redis nil pointer panic on FAQ import (knowledge.go)
- Add in-memory fallback (sync.Map) for FAQ import progress tracking
- Add nil guards for running task locks and progress queries
2. seq_id not auto-incrementing in SQLite (chunk.go, tag.go)
- GORM autoIncrement on non-PK columns doesn't work in SQLite
- Add AssignChunkSeqIDs() to pre-assign seq_ids before batch insert
- Add BeforeCreate hook for KnowledgeTag
- Use Unscoped() to include soft-deleted records in MAX(seq_id) query
3. NOW() function not available in SQLite (chunk.go)
- Replace raw SQL NOW() with time.Now() or datetime('now')
- Use dialector check for raw SQL that must differ between PG and SQLite
4. FAQ KB should reject file uploads (knowledge.go)
- Add kb.Type == "faq" check in CreateKnowledgeFromFile
5. Linux/macOS build compatibility (Makefile)
- Conditionally apply macOS-only linker flag based on uname
6. Add SQLite integration tests (chunk_sqlite_test.go)
- TestCreateChunks_SQLite_SeqIDAutoAssigned
- TestCreateChunks_SQLite_SeqIDContinuesFromExisting
- TestCreateChunks_SQLite_SeqIDUniqueAcrossKBs
- TestCreateChunks_SQLite_SeqIDAfterSoftDelete
- TestKnowledgeTag_SQLite_SeqIDAutoAssigned
- TestUpdateChunk_SQLite_NoNOWError
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/common"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
@@ -28,6 +29,11 @@ func (r *chunkRepository) CreateChunks(ctx context.Context, chunks []*types.Chun
|
||||
for _, chunk := range chunks {
|
||||
chunk.Content = common.CleanInvalidUTF8(chunk.Content)
|
||||
}
|
||||
// Pre-assign SeqIDs for SQLite compatibility (autoIncrement on non-PK columns
|
||||
// doesn't work in SQLite). This is a no-op if all chunks already have SeqIDs.
|
||||
if err := types.AssignChunkSeqIDs(r.db.WithContext(ctx), chunks); err != nil {
|
||||
return fmt.Errorf("failed to assign chunk seq_ids: %w", err)
|
||||
}
|
||||
// Use Select("*") to ensure all fields including zero values (IsEnabled=false, Flags=0)
|
||||
// are inserted, bypassing GORM's default value behavior for zero values
|
||||
return r.db.WithContext(ctx).Select("*").CreateInBatches(chunks, 100).Error
|
||||
@@ -379,7 +385,7 @@ func (r *chunkRepository) UpdateChunks(ctx context.Context, chunks []*types.Chun
|
||||
tag_id = CASE %s END,
|
||||
flags = CASE %s END,
|
||||
status = CASE %s END,
|
||||
updated_at = NOW()
|
||||
updated_at = datetime('now')
|
||||
WHERE id IN (%s)
|
||||
`,
|
||||
strings.Join(contentCases, " "),
|
||||
@@ -783,14 +789,18 @@ func (r *chunkRepository) UpdateChunkFlagsBatch(
|
||||
inPlaceholders[i] = "?"
|
||||
}
|
||||
|
||||
nowFunc := "NOW()"
|
||||
if r.db.Dialector.Name() == "sqlite" {
|
||||
nowFunc = "datetime('now')"
|
||||
}
|
||||
sql := fmt.Sprintf(`
|
||||
UPDATE chunks
|
||||
UPDATE chunks
|
||||
SET flags = (flags | (%s)) & ~(%s),
|
||||
updated_at = NOW()
|
||||
WHERE tenant_id = ?
|
||||
updated_at = %s
|
||||
WHERE tenant_id = ?
|
||||
AND knowledge_base_id = ?
|
||||
AND id IN (%s)
|
||||
`, setExpr, clearExpr, strings.Join(inPlaceholders, ","))
|
||||
`, setExpr, clearExpr, nowFunc, strings.Join(inPlaceholders, ","))
|
||||
|
||||
args = append(args, tenantID, kbID)
|
||||
for _, id := range allIDs {
|
||||
@@ -842,7 +852,7 @@ func (r *chunkRepository) UpdateChunkFieldsByTagID(
|
||||
|
||||
// Build update query
|
||||
updates := map[string]interface{}{
|
||||
"updated_at": "NOW()",
|
||||
"updated_at": time.Now(),
|
||||
}
|
||||
|
||||
if isEnabled != nil {
|
||||
|
||||
Reference in New Issue
Block a user