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:
@@ -78,6 +78,10 @@ type knowledgeService struct {
|
||||
redisClient *redis.Client
|
||||
kbShareService interfaces.KBShareService
|
||||
imageResolver *docparser.ImageResolver
|
||||
|
||||
// In-memory fallbacks for Lite mode (no Redis)
|
||||
memFAQProgress sync.Map // taskID -> *types.FAQImportProgress
|
||||
memFAQRunningImport sync.Map // kbID -> *runningFAQImportInfo
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -216,6 +220,11 @@ func (s *knowledgeService) CreateKnowledgeFromFile(ctx context.Context,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// FAQ knowledge bases should not accept file uploads — use the FAQ import API instead
|
||||
if kb.Type == types.KnowledgeBaseTypeFAQ {
|
||||
return nil, werrors.NewBadRequestError("FAQ 知识库不支持文件上传,请使用 FAQ 导入功能")
|
||||
}
|
||||
|
||||
if err := checkStorageEngineConfigured(ctx, kb); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -6350,6 +6359,12 @@ type runningFAQImportInfo struct {
|
||||
// getRunningFAQImportInfo checks if there's a running FAQ import task for the given KB
|
||||
// Returns the task info if found, nil otherwise
|
||||
func (s *knowledgeService) getRunningFAQImportInfo(ctx context.Context, kbID string) (*runningFAQImportInfo, error) {
|
||||
if s.redisClient == nil {
|
||||
if v, ok := s.memFAQRunningImport.Load(kbID); ok {
|
||||
return v.(*runningFAQImportInfo), nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
key := getFAQImportRunningKey(kbID)
|
||||
data, err := s.redisClient.Get(ctx, key).Result()
|
||||
if err != nil {
|
||||
@@ -6383,6 +6398,10 @@ func (s *knowledgeService) getRunningFAQImportTaskID(ctx context.Context, kbID s
|
||||
|
||||
// setRunningFAQImportInfo sets the running task info for a KB
|
||||
func (s *knowledgeService) setRunningFAQImportInfo(ctx context.Context, kbID string, info *runningFAQImportInfo) error {
|
||||
if s.redisClient == nil {
|
||||
s.memFAQRunningImport.Store(kbID, info)
|
||||
return nil
|
||||
}
|
||||
key := getFAQImportRunningKey(kbID)
|
||||
data, err := json.Marshal(info)
|
||||
if err != nil {
|
||||
@@ -6393,6 +6412,10 @@ func (s *knowledgeService) setRunningFAQImportInfo(ctx context.Context, kbID str
|
||||
|
||||
// clearRunningFAQImportTaskID clears the running task ID for a KB
|
||||
func (s *knowledgeService) clearRunningFAQImportTaskID(ctx context.Context, kbID string) error {
|
||||
if s.redisClient == nil {
|
||||
s.memFAQRunningImport.Delete(kbID)
|
||||
return nil
|
||||
}
|
||||
key := getFAQImportRunningKey(kbID)
|
||||
return s.redisClient.Del(ctx, key).Err()
|
||||
}
|
||||
@@ -8490,6 +8513,11 @@ func getFAQImportRunningKey(kbID string) string {
|
||||
|
||||
// saveFAQImportProgress saves the FAQ import progress to Redis
|
||||
func (s *knowledgeService) saveFAQImportProgress(ctx context.Context, progress *types.FAQImportProgress) error {
|
||||
if s.redisClient == nil {
|
||||
progress.UpdatedAt = time.Now().Unix()
|
||||
s.memFAQProgress.Store(progress.TaskID, progress)
|
||||
return nil
|
||||
}
|
||||
key := getFAQImportProgressKey(progress.TaskID)
|
||||
progress.UpdatedAt = time.Now().Unix()
|
||||
data, err := json.Marshal(progress)
|
||||
@@ -8501,6 +8529,12 @@ func (s *knowledgeService) saveFAQImportProgress(ctx context.Context, progress *
|
||||
|
||||
// GetFAQImportProgress retrieves the progress of an FAQ import task
|
||||
func (s *knowledgeService) GetFAQImportProgress(ctx context.Context, taskID string) (*types.FAQImportProgress, error) {
|
||||
if s.redisClient == nil {
|
||||
if v, ok := s.memFAQProgress.Load(taskID); ok {
|
||||
return v.(*types.FAQImportProgress), nil
|
||||
}
|
||||
return nil, werrors.NewNotFoundError("FAQ import task not found")
|
||||
}
|
||||
key := getFAQImportProgressKey(taskID)
|
||||
data, err := s.redisClient.Get(ctx, key).Bytes()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user