Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(datasource): persist disabled deletion sync setting

liguowei7a910aa05a7c
diff --git a/internal/application/repository/datasource_repo.go b/internal/application/repository/datasource_repo.go
index 4771aa153..3cf70eb13 100644
--- a/internal/application/repository/datasource_repo.go
+++ b/internal/application/repository/datasource_repo.go
@@ -25,10 +25,17 @@ func (r *DataSourceRepository) Create(ctx context.Context, ds *types.DataSource)
if ds == nil {
return errors.New("data source is nil")
}
- if err := r.db.WithContext(ctx).Create(ds).Error; err != nil {
- return err
- }
- return nil
+ // GORM may omit a false value for a field with a database default. Preserve
+ // the caller's choice before Create potentially hydrates that default.
+ syncDeletions := ds.SyncDeletions
+ return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
+ if err := tx.Create(ds).Error; err != nil {
+ return err
+ }
+ return tx.Model(&types.DataSource{}).
+ Where("id = ?", ds.ID).
+ UpdateColumn("sync_deletions", syncDeletions).Error
+ })
}
// FindByID retrieves a data source by ID
@@ -73,12 +80,16 @@ func (r *DataSourceRepository) Update(ctx context.Context, ds *types.DataSource)
if ds.ID == "" {
return errors.New("data source id is empty")
}
- if err := r.db.WithContext(ctx).
- Model(ds).
- Updates(ds).Error; err != nil {
- return err
- }
- return nil
+ return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
+ if err := tx.Model(ds).Updates(ds).Error; err != nil {
+ return err
+ }
+ // GORM Updates(struct) deliberately skips zero values, which would make
+ // a user-selected sync_deletions=false impossible to persist.
+ return tx.Model(&types.DataSource{}).
+ Where("id = ?", ds.ID).
+ UpdateColumn("sync_deletions", ds.SyncDeletions).Error
+ })
}
// UpdateSyncState updates only fields managed by sync execution. GORM's
diff --git a/internal/application/repository/datasource_repo_test.go b/internal/application/repository/datasource_repo_test.go
index e3ab5c260..e430fa0e3 100644
--- a/internal/application/repository/datasource_repo_test.go
+++ b/internal/application/repository/datasource_repo_test.go
@@ -51,6 +51,49 @@ func TestDataSourceRepositoryUpdateSyncStateClearsErrorMessage(t *testing.T) {
require.NotNil(t, stored.LastSyncAt)
}
+func TestDataSourceRepositoryUpdatePersistsDisabledSyncDeletions(t *testing.T) {
+ db := setupDataSourceRepoTestDB(t)
+ repo := NewDataSourceRepository(db)
+ ctx := context.Background()
+
+ ds := &types.DataSource{
+ ID: "ds-sync-deletions",
+ TenantID: 1,
+ KnowledgeBaseID: "kb-1",
+ Name: "Feishu",
+ Type: types.ConnectorTypeFeishu,
+ SyncDeletions: true,
+ }
+ require.NoError(t, repo.Create(ctx, ds))
+
+ ds.SyncDeletions = false
+ require.NoError(t, repo.Update(ctx, ds))
+
+ var stored types.DataSource
+ require.NoError(t, db.First(&stored, "id = ?", ds.ID).Error)
+ assert.False(t, stored.SyncDeletions)
+}
+
+func TestDataSourceRepositoryCreatePersistsDisabledSyncDeletions(t *testing.T) {
+ db := setupDataSourceRepoTestDB(t)
+ repo := NewDataSourceRepository(db)
+ ctx := context.Background()
+
+ ds := &types.DataSource{
+ ID: "ds-create-sync-deletions",
+ TenantID: 1,
+ KnowledgeBaseID: "kb-1",
+ Name: "Feishu",
+ Type: types.ConnectorTypeFeishu,
+ SyncDeletions: false,
+ }
+ require.NoError(t, repo.Create(ctx, ds))
+
+ var stored types.DataSource
+ require.NoError(t, db.First(&stored, "id = ?", ds.ID).Error)
+ assert.False(t, stored.SyncDeletions)
+}
+
func TestDataSourceRepositoryDeleteSoftDeletesOnSQLite(t *testing.T) {
db := setupDataSourceRepoTestDB(t)
repo := NewDataSourceRepository(db)