fix(datasource): persist disabled deletion sync setting
parent
53cb613419
commit
7a910aa05a
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue