fix(datasource): fail sync when all fetched items fail
This commit is contained in:
@@ -81,6 +81,32 @@ func (r *DataSourceRepository) Update(ctx context.Context, ds *types.DataSource)
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateSyncState updates only fields managed by sync execution. GORM's
|
||||
// Updates(struct) skips zero values, so use a map here to persist cleared error
|
||||
// messages without broadening the generic Update method.
|
||||
func (r *DataSourceRepository) UpdateSyncState(ctx context.Context, ds *types.DataSource) error {
|
||||
if ds == nil {
|
||||
return errors.New("data source is nil")
|
||||
}
|
||||
if ds.ID == "" {
|
||||
return errors.New("data source id is empty")
|
||||
}
|
||||
if err := r.db.WithContext(ctx).
|
||||
Model(&types.DataSource{}).
|
||||
Where("id = ?", ds.ID).
|
||||
Updates(map[string]interface{}{
|
||||
"status": ds.Status,
|
||||
"last_sync_at": ds.LastSyncAt,
|
||||
"last_sync_cursor": ds.LastSyncCursor,
|
||||
"last_sync_result": ds.LastSyncResult,
|
||||
"error_message": ds.ErrorMessage,
|
||||
"updated_at": time.Now().UTC(),
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete performs a soft delete
|
||||
func (r *DataSourceRepository) Delete(ctx context.Context, id string) error {
|
||||
if id == "" {
|
||||
@@ -221,6 +247,36 @@ func (r *SyncLogRepository) Update(ctx context.Context, log *types.SyncLog) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateResult updates only fields produced by sync execution. Use an explicit
|
||||
// map so empty error messages are written when a later sync succeeds.
|
||||
func (r *SyncLogRepository) UpdateResult(ctx context.Context, log *types.SyncLog) error {
|
||||
if log == nil {
|
||||
return errors.New("sync log is nil")
|
||||
}
|
||||
if log.ID == "" {
|
||||
return errors.New("sync log id is empty")
|
||||
}
|
||||
if err := r.db.WithContext(ctx).
|
||||
Model(&types.SyncLog{}).
|
||||
Where("id = ?", log.ID).
|
||||
Updates(map[string]interface{}{
|
||||
"status": log.Status,
|
||||
"finished_at": log.FinishedAt,
|
||||
"items_total": log.ItemsTotal,
|
||||
"items_created": log.ItemsCreated,
|
||||
"items_updated": log.ItemsUpdated,
|
||||
"items_deleted": log.ItemsDeleted,
|
||||
"items_skipped": log.ItemsSkipped,
|
||||
"items_failed": log.ItemsFailed,
|
||||
"error_message": log.ErrorMessage,
|
||||
"result": log.Result,
|
||||
"updated_at": time.Now().UTC(),
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CancelPendingByDataSource marks all non-terminal sync logs for a data source as canceled.
|
||||
func (r *SyncLogRepository) CancelPendingByDataSource(ctx context.Context, dsID string) error {
|
||||
if dsID == "" {
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func setupDataSourceRepoTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.AutoMigrate(&types.DataSource{}, &types.SyncLog{}))
|
||||
return db
|
||||
}
|
||||
|
||||
func TestDataSourceRepositoryUpdateSyncStateClearsErrorMessage(t *testing.T) {
|
||||
db := setupDataSourceRepoTestDB(t)
|
||||
repo := NewDataSourceRepository(db)
|
||||
now := time.Now().UTC()
|
||||
result := types.JSON(`{"total":0}`)
|
||||
|
||||
ds := &types.DataSource{
|
||||
ID: "ds-1",
|
||||
TenantID: 1,
|
||||
KnowledgeBaseID: "kb-1",
|
||||
Name: "Feishu",
|
||||
Type: types.ConnectorTypeFeishu,
|
||||
Status: types.DataSourceStatusError,
|
||||
ErrorMessage: "previous failure",
|
||||
}
|
||||
require.NoError(t, repo.Create(context.Background(), ds))
|
||||
|
||||
ds.Status = types.DataSourceStatusActive
|
||||
ds.ErrorMessage = ""
|
||||
ds.LastSyncAt = &now
|
||||
ds.LastSyncResult = result
|
||||
require.NoError(t, repo.UpdateSyncState(context.Background(), ds))
|
||||
|
||||
var stored types.DataSource
|
||||
require.NoError(t, db.First(&stored, "id = ?", ds.ID).Error)
|
||||
assert.Equal(t, types.DataSourceStatusActive, stored.Status)
|
||||
assert.Empty(t, stored.ErrorMessage)
|
||||
assert.Equal(t, result.ToString(), stored.LastSyncResult.ToString())
|
||||
require.NotNil(t, stored.LastSyncAt)
|
||||
}
|
||||
|
||||
func TestSyncLogRepositoryUpdateResultClearsErrorMessage(t *testing.T) {
|
||||
db := setupDataSourceRepoTestDB(t)
|
||||
repo := NewSyncLogRepository(db)
|
||||
finishedAt := time.Now().UTC()
|
||||
result := types.JSON(`{"total":0}`)
|
||||
|
||||
log := &types.SyncLog{
|
||||
ID: "log-1",
|
||||
DataSourceID: "ds-1",
|
||||
TenantID: 1,
|
||||
Status: types.SyncLogStatusFailed,
|
||||
ErrorMessage: "previous failure",
|
||||
ItemsTotal: 1,
|
||||
ItemsFailed: 1,
|
||||
}
|
||||
require.NoError(t, repo.Create(context.Background(), log))
|
||||
|
||||
log.Status = types.SyncLogStatusSuccess
|
||||
log.ErrorMessage = ""
|
||||
log.FinishedAt = &finishedAt
|
||||
log.ItemsTotal = 0
|
||||
log.ItemsFailed = 0
|
||||
log.Result = result
|
||||
require.NoError(t, repo.UpdateResult(context.Background(), log))
|
||||
|
||||
var stored types.SyncLog
|
||||
require.NoError(t, db.First(&stored, "id = ?", log.ID).Error)
|
||||
assert.Equal(t, types.SyncLogStatusSuccess, stored.Status)
|
||||
assert.Empty(t, stored.ErrorMessage)
|
||||
assert.Zero(t, stored.ItemsTotal)
|
||||
assert.Zero(t, stored.ItemsFailed)
|
||||
assert.Equal(t, result.ToString(), stored.Result.ToString())
|
||||
require.NotNil(t, stored.FinishedAt)
|
||||
}
|
||||
Reference in New Issue
Block a user