Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(faq): preserve disabled state when importing entries (#4581)
Jarvis9981c9fedd7e
diff --git a/internal/application/repository/chunk.go b/internal/application/repository/chunk.goindex ee27e8414..b6ad7b3da 100644--- a/internal/application/repository/chunk.go+++ b/internal/application/repository/chunk.go@@ -67,6 +67,18 @@ func (r *chunkRepository) CreateChunks(ctx context.Context, chunks []*types.Chun}}+ // GORM substitutes default:true for a zero bool even with Select("*").+ // A non-nil pointer makes both true and false explicit on insert, while+ // embedding the original chunk keeps generated seq_ids and timestamps on it.+ type chunkInsert struct {+ *types.Chunk+ IsEnabled *bool+ }+ rows := make([]chunkInsert, len(chunks))+ for i, chunk := range chunks {+ rows[i] = chunkInsert{Chunk: chunk, IsEnabled: &chunk.IsEnabled}+ }+return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {// SQLite doesn't support autoIncrement on non-PK columns, so SeqIDs are// pre-assigned from MAX(seq_id). Doing it inside the write transaction@@ -78,10 +90,8 @@ func (r *chunkRepository) CreateChunks(ctx context.Context, chunks []*types.Chun}}- // Select("*") ensures zero-value fields (IsEnabled=false, Flags=0) are- // explicitly inserted, bypassing GORM's default value behavior.// SeqID=0 is skipped by GORM automatically (autoIncrement tag).- return tx.Select("*").CreateInBatches(chunks, createChunksBatchSize).Error+ return tx.Table("chunks").Select("*").CreateInBatches(rows, createChunksBatchSize).Error})}diff --git a/internal/application/repository/chunk_insert_test.go b/internal/application/repository/chunk_insert_test.gonew file mode 100644index 000000000..ddc632cfa--- /dev/null+++ b/internal/application/repository/chunk_insert_test.go@@ -0,0 +1,55 @@+package repository++import (+ "context"+ "database/sql/driver"+ "testing"++ "github.com/DATA-DOG/go-sqlmock"+ "github.com/Tencent/WeKnora/internal/types"+ "github.com/stretchr/testify/require"+ "gorm.io/driver/postgres"+ "gorm.io/gorm"+)++func TestCreateChunks_PostgresPreservesEnabledStateAndReturnsSeqIDs(t *testing.T) {+ sqlDB, mock, err := sqlmock.New()+ require.NoError(t, err)+ t.Cleanup(func() { _ = sqlDB.Close() })+ db, err := gorm.Open(postgres.New(postgres.Config{Conn: sqlDB}), &gorm.Config{})+ require.NoError(t, err)++ chunks := []*types.Chunk{+ makeChunk("kb", "faq", types.ChunkTypeFAQ),+ makeChunk("kb", "faq", types.ChunkTypeFAQ),+ }+ chunks[1].IsEnabled = false+ // Check the bound values for both rows, not only the caller's structs.+ stmt := &gorm.Statement{DB: db}+ require.NoError(t, stmt.Parse(&types.Chunk{}))+ var args []driver.Value+ for _, chunk := range chunks {+ for _, name := range stmt.Schema.DBNames {+ if name == "seq_id" {+ continue // PostgreSQL generates these and returns them to the embedded chunks.+ }+ if name == "is_enabled" {+ args = append(args, chunk.IsEnabled)+ } else {+ args = append(args, sqlmock.AnyArg())+ }+ }+ }+ mock.ExpectBegin()+ mock.ExpectQuery(`INSERT INTO "chunks" .*RETURNING "seq_id"`).WithArgs(args...).+ WillReturnRows(sqlmock.NewRows([]string{"seq_id"}).AddRow(101).AddRow(102))+ mock.ExpectCommit()++ require.NoError(t, NewChunkRepository(db).CreateChunks(context.Background(), chunks))+ require.EqualValues(t, 101, chunks[0].SeqID)+ require.EqualValues(t, 102, chunks[1].SeqID)+ require.True(t, chunks[0].IsEnabled)+ require.False(t, chunks[1].IsEnabled)+ require.False(t, chunks[0].CreatedAt.IsZero())+ require.NoError(t, mock.ExpectationsWereMet())+}diff --git a/internal/application/repository/chunk_sqlite_test.go b/internal/application/repository/chunk_sqlite_test.goindex 93c2f0a63..d3e8c2328 100644--- a/internal/application/repository/chunk_sqlite_test.go+++ b/internal/application/repository/chunk_sqlite_test.go@@ -2,6 +2,7 @@ package repositoryimport ("context"+ "fmt""testing""unicode/utf8"@@ -476,3 +477,31 @@ func TestUpdateChunkFieldsByIDs_ScopesByTenantAndSetsUpdatedAt(t *testing.T) {require.NoError(t, repo.UpdateChunkFieldsByIDs(ctx, 1, nil, map[string]interface{}{"status": 0}))require.NoError(t, repo.UpdateChunkFieldsByIDs(ctx, 1, []string{mine.ID}, nil))}++// Exercise the actual batch insert used by FAQ imports, including a second SQL batch.+func TestCreateChunks_PreservesExplicitEnabledState(t *testing.T) {+ for _, count := range []int{2, createChunksBatchSize + 2} {+ t.Run(fmt.Sprint(count), func(t *testing.T) {+ db := setupChunkTestDB(t)+ repo := NewChunkRepository(db)+ chunks := make([]*types.Chunk, count)+ for i := range chunks {+ chunks[i] = makeChunk("kb-enabled", "faq-enabled", types.ChunkTypeFAQ)+ chunks[i].IsEnabled = i%2 == 0+ chunks[i].ChunkIndex = i+ }+ require.NoError(t, repo.CreateChunks(context.Background(), chunks))+ var saved []types.Chunk+ require.NoError(t, db.Order("chunk_index").Find(&saved).Error)+ require.Len(t, saved, count)+ for i, chunk := range saved {+ assert.Equal(t, i%2 == 0, chunk.IsEnabled, "stored chunk %d", i)+ assert.Equal(t, i%2 == 0, chunks[i].IsEnabled, "caller chunk %d", i)+ assert.Equal(t, chunks[i].SeqID, chunk.SeqID)+ assert.Positive(t, chunk.SeqID)+ assert.False(t, chunks[i].CreatedAt.IsZero())+ assert.True(t, chunks[i].CreatedAt.Equal(chunk.CreatedAt))+ }+ })+ }+}diff --git a/internal/application/service/knowledge_faq_import_enabled_test.go b/internal/application/service/knowledge_faq_import_enabled_test.gonew file mode 100644index 000000000..9f654ce7e--- /dev/null+++ b/internal/application/service/knowledge_faq_import_enabled_test.go@@ -0,0 +1,69 @@+package service++import (+ "testing"++ "github.com/Tencent/WeKnora/internal/types"+ "github.com/Tencent/WeKnora/internal/types/interfaces"+ "github.com/stretchr/testify/require"+)++func TestFAQImportPreservesEnabledStateInStorageAndIndex(t *testing.T) {+ for _, mode := range []string{types.FAQBatchModeAppend, types.FAQBatchModeReplace} {+ t.Run(mode, func(t *testing.T) {+ f := newFAQWriteFixture(t)+ sqlDB, err := f.db.DB()+ require.NoError(t, err)+ t.Cleanup(func() { require.NoError(t, sqlDB.Close()) })+ // Keep the real FAQ container and tag, but start with no FAQ entries.+ require.NoError(t, f.db.Where("id = ?", "one").Delete(&types.Chunk{}).Error)+ engine := &parentChildRetrieveEngine{}+ f.svc.retrieveEngine = &fakeFanoutRegistry{+ byStore: map[string]interfaces.RetrieveEngineService{*f.kb.VectorStoreID: engine},+ }+ f.svc.modelService = parentChildModelService{embedder: parentChildEmbedder{}}+ f.svc.chunkService = &chunkService{+ chunkRepository: f.chunks, knowledgeRepo: f.svc.repo,+ kbRepository: &imageReaderKBRepo{kb: f.kb},+ }+ enabled, disabled := true, false+ entries := []types.FAQEntryPayload{+ {+ StandardQuestion: "enabled FAQ", Answers: []string{"enabled answer"},+ TagID: 11, IsEnabled: &enabled,+ },+ {+ StandardQuestion: "disabled FAQ", Answers: []string{"disabled answer"},+ TagID: 11, IsEnabled: &disabled,+ },+ {StandardQuestion: "default FAQ", Answers: []string{"default answer"}, TagID: 11},+ }+ progress := &types.FAQImportProgress{TaskID: "enabled-import", KBID: f.kb.ID}+ require.NoError(t, f.svc.executeFAQImport(f.ctx, progress.TaskID, f.kb.ID,+ &types.FAQBatchUpsertPayload{Mode: mode, Entries: entries}, f.kb.TenantID, 0, progress))+ var saved []types.Chunk+ require.NoError(t, f.db.Where("knowledge_id = ?", "faq").Order("seq_id").Find(&saved).Error)+ require.Len(t, saved, 3)+ require.Len(t, engine.indexed, 3)+ require.Len(t, progress.SuccessEntries, 3)+ // Index deduplication uses a map, so match by identity rather than slice order.+ indexedByChunkID := make(map[string]*types.IndexInfo, len(engine.indexed))+ for _, info := range engine.indexed {+ require.NotNil(t, info)+ indexedByChunkID[info.ChunkID] = info+ }+ require.Len(t, indexedByChunkID, len(saved))+ for i, wantEnabled := range []bool{true, false, true} {+ meta, err := saved[i].FAQMetadata()+ require.NoError(t, err)+ require.Equal(t, entries[i].StandardQuestion, meta.StandardQuestion)+ require.Equal(t, wantEnabled, saved[i].IsEnabled)+ info, ok := indexedByChunkID[saved[i].ID]+ require.True(t, ok, "chunk %s must be indexed", saved[i].ID)+ require.Equal(t, wantEnabled, info.IsEnabled)+ require.Equal(t, int(types.ChunkStatusIndexed), saved[i].Status)+ require.Equal(t, saved[i].SeqID, progress.SuccessEntries[i].SeqID)+ }+ })+ }+}