From 412dcc41c662c9b45698959e0c3c37db5b8dc9d3 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Fri, 21 Aug 2026 16:24:51 +0800 Subject: [PATCH] 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. --- .../application/repository/datasource_repo.go | 11 ++++-- .../repository/datasource_repo_test.go | 36 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/internal/application/repository/datasource_repo.go b/internal/application/repository/datasource_repo.go index 3cf70eb1..c3b2f58e 100644 --- a/internal/application/repository/datasource_repo.go +++ b/internal/application/repository/datasource_repo.go @@ -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 diff --git a/internal/application/repository/datasource_repo_test.go b/internal/application/repository/datasource_repo_test.go index e430fa0e..77ed065c 100644 --- a/internal/application/repository/datasource_repo_test.go +++ b/internal/application/repository/datasource_repo_test.go @@ -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) {