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:
@@ -162,3 +162,34 @@ type Chunk struct {
|
||||
// Soft delete marker, supports data recovery
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
|
||||
}
|
||||
|
||||
// AssignChunkSeqIDs assigns sequential SeqIDs to a batch of chunks that have SeqID == 0.
|
||||
// Must be called before CreateInBatches for SQLite compatibility.
|
||||
func AssignChunkSeqIDs(tx *gorm.DB, chunks []*Chunk) error {
|
||||
needAssign := false
|
||||
for _, c := range chunks {
|
||||
if c.SeqID == 0 {
|
||||
needAssign = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !needAssign {
|
||||
return nil
|
||||
}
|
||||
|
||||
var maxSeqID *int64
|
||||
if err := tx.Unscoped().Model(&Chunk{}).Select("MAX(seq_id)").Scan(&maxSeqID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
next := int64(1)
|
||||
if maxSeqID != nil {
|
||||
next = *maxSeqID + 1
|
||||
}
|
||||
for _, c := range chunks {
|
||||
if c.SeqID == 0 {
|
||||
c.SeqID = next
|
||||
next++
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
+22
-1
@@ -1,6 +1,10 @@
|
||||
package types
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// KnowledgeTag represents a tag (category) under a specific knowledge base.
|
||||
// Tags are scoped by knowledge base (and tenant) and are used to categorize
|
||||
@@ -26,6 +30,23 @@ type KnowledgeTag struct {
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// BeforeCreate ensures SeqID is populated for databases that don't support
|
||||
// autoIncrement on non-primary-key columns (e.g. SQLite).
|
||||
func (t *KnowledgeTag) BeforeCreate(tx *gorm.DB) error {
|
||||
if t.SeqID == 0 {
|
||||
var maxSeqID *int64
|
||||
tx.Unscoped().Model(&KnowledgeTag{}).
|
||||
Select("MAX(seq_id)").
|
||||
Scan(&maxSeqID)
|
||||
if maxSeqID != nil {
|
||||
t.SeqID = *maxSeqID + 1
|
||||
} else {
|
||||
t.SeqID = 1
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// KnowledgeTagWithStats represents tag information along with usage statistics.
|
||||
type KnowledgeTagWithStats struct {
|
||||
KnowledgeTag
|
||||
|
||||
Reference in New Issue
Block a user