fix(datasource): restore sync_deletions on the in-memory object after create
GORM hydrates default:true back onto the struct during Create, so the HTTP 201 body (and a later Update on the same pointer) could disagree with the row just written.main
parent
7a910aa05a
commit
412dcc41c6
|
|
@ -25,10 +25,13 @@ func (r *DataSourceRepository) Create(ctx context.Context, ds *types.DataSource)
|
|||
if ds == nil {
|
||||
return errors.New("data source is 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.
|
||||
// GORM treats false as the zero value of bool. For a field tagged
|
||||
// default:true it replaces both the INSERT value and the in-memory field
|
||||
// with true, so a caller-selected false would be lost. Capture it, force
|
||||
// the column write, then restore the struct so Create's return value (and
|
||||
// the HTTP 201 body) match the database.
|
||||
syncDeletions := ds.SyncDeletions
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Create(ds).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -36,6 +39,8 @@ func (r *DataSourceRepository) Create(ctx context.Context, ds *types.DataSource)
|
|||
Where("id = ?", ds.ID).
|
||||
UpdateColumn("sync_deletions", syncDeletions).Error
|
||||
})
|
||||
ds.SyncDeletions = syncDeletions
|
||||
return err
|
||||
}
|
||||
|
||||
// FindByID retrieves a data source by ID
|
||||
|
|
|
|||
|
|
@ -68,10 +68,17 @@ func TestDataSourceRepositoryUpdatePersistsDisabledSyncDeletions(t *testing.T) {
|
|||
|
||||
ds.SyncDeletions = false
|
||||
require.NoError(t, repo.Update(ctx, ds))
|
||||
assert.False(t, ds.SyncDeletions)
|
||||
|
||||
var stored types.DataSource
|
||||
require.NoError(t, db.First(&stored, "id = ?", ds.ID).Error)
|
||||
assert.False(t, stored.SyncDeletions)
|
||||
|
||||
ds.SyncDeletions = true
|
||||
require.NoError(t, repo.Update(ctx, ds))
|
||||
assert.True(t, ds.SyncDeletions)
|
||||
require.NoError(t, db.First(&stored, "id = ?", ds.ID).Error)
|
||||
assert.True(t, stored.SyncDeletions)
|
||||
}
|
||||
|
||||
func TestDataSourceRepositoryCreatePersistsDisabledSyncDeletions(t *testing.T) {
|
||||
|
|
@ -88,10 +95,39 @@ func TestDataSourceRepositoryCreatePersistsDisabledSyncDeletions(t *testing.T) {
|
|||
SyncDeletions: false,
|
||||
}
|
||||
require.NoError(t, repo.Create(ctx, ds))
|
||||
assert.False(t, ds.SyncDeletions, "Create must not leave the in-memory field hydrated to the GORM default")
|
||||
|
||||
var stored types.DataSource
|
||||
require.NoError(t, db.First(&stored, "id = ?", ds.ID).Error)
|
||||
assert.False(t, stored.SyncDeletions)
|
||||
|
||||
// A later Updates() on the same pointer must not persist the hydrated default.
|
||||
ds.Name = "Renamed"
|
||||
require.NoError(t, repo.Update(ctx, ds))
|
||||
require.NoError(t, db.First(&stored, "id = ?", ds.ID).Error)
|
||||
assert.False(t, stored.SyncDeletions)
|
||||
assert.Equal(t, "Renamed", stored.Name)
|
||||
}
|
||||
|
||||
func TestDataSourceRepositoryCreatePersistsEnabledSyncDeletions(t *testing.T) {
|
||||
db := setupDataSourceRepoTestDB(t)
|
||||
repo := NewDataSourceRepository(db)
|
||||
ctx := context.Background()
|
||||
|
||||
ds := &types.DataSource{
|
||||
ID: "ds-create-sync-deletions-enabled",
|
||||
TenantID: 1,
|
||||
KnowledgeBaseID: "kb-1",
|
||||
Name: "Feishu",
|
||||
Type: types.ConnectorTypeFeishu,
|
||||
SyncDeletions: true,
|
||||
}
|
||||
require.NoError(t, repo.Create(ctx, ds))
|
||||
assert.True(t, ds.SyncDeletions)
|
||||
|
||||
var stored types.DataSource
|
||||
require.NoError(t, db.First(&stored, "id = ?", ds.ID).Error)
|
||||
assert.True(t, stored.SyncDeletions)
|
||||
}
|
||||
|
||||
func TestDataSourceRepositoryDeleteSoftDeletesOnSQLite(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue