feat: Refactor wiki page update logic for improved version control
- Enhanced the UpdatePage and UpdateMeta methods to differentiate between user-visible content changes and bookkeeping updates, ensuring the version number is only incremented for actual content modifications. - Updated the WikiPage struct and associated interfaces to reflect the new versioning policy, improving clarity on when the version is modified. - Improved documentation for methods to clarify their intended use and behavior regarding versioning and metadata updates.
This commit is contained in:
@@ -59,19 +59,25 @@ func (r *wikiPageRepository) Update(ctx context.Context, page *types.WikiPage) e
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateMeta updates only metadata fields (links, status, source_refs) WITHOUT
|
||||
// incrementing the version number. Used for internal bookkeeping operations
|
||||
// like link maintenance, status changes, and source ref updates.
|
||||
// UpdateMeta updates bookkeeping / provenance fields WITHOUT incrementing the
|
||||
// version number. "Content" for versioning purposes is the user-visible page
|
||||
// body (title/content/summary/page_type/status); everything else — links,
|
||||
// source refs, chunk refs, page_metadata — is considered bookkeeping and is
|
||||
// refreshed here so the version counter only advances on real edits.
|
||||
//
|
||||
// Used by link maintenance, re-ingest (same-content case), and status changes.
|
||||
func (r *wikiPageRepository) UpdateMeta(ctx context.Context, page *types.WikiPage) error {
|
||||
result := r.db.WithContext(ctx).
|
||||
Model(page).
|
||||
Where("id = ?", page.ID).
|
||||
Updates(map[string]interface{}{
|
||||
"in_links": page.InLinks,
|
||||
"out_links": page.OutLinks,
|
||||
"status": page.Status,
|
||||
"source_refs": page.SourceRefs,
|
||||
"updated_at": page.UpdatedAt,
|
||||
"in_links": page.InLinks,
|
||||
"out_links": page.OutLinks,
|
||||
"status": page.Status,
|
||||
"source_refs": page.SourceRefs,
|
||||
"chunk_refs": page.ChunkRefs,
|
||||
"page_metadata": page.PageMetadata,
|
||||
"updated_at": page.UpdatedAt,
|
||||
})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
||||
@@ -77,7 +77,16 @@ func (s *wikiPageService) CreatePage(ctx context.Context, page *types.WikiPage)
|
||||
return page, nil
|
||||
}
|
||||
|
||||
// UpdatePage updates an existing wiki page
|
||||
// UpdatePage updates an existing wiki page.
|
||||
//
|
||||
// Version bump policy: the `version` column is intended to track the user-
|
||||
// visible content revision, not every row rewrite. We therefore bump it only
|
||||
// when at least one of the user-facing fields actually changes — title,
|
||||
// content, summary, page_type, or status. Bookkeeping-only writes (refreshing
|
||||
// source_refs after re-ingest when the body is identical, rebuilding the index
|
||||
// page with the same directory, cross-link injection that ends up replacing
|
||||
// nothing, etc.) still persist through `UpdateMeta` but leave `version`
|
||||
// untouched so consumers can treat a bump as a real edit signal.
|
||||
func (s *wikiPageService) UpdatePage(ctx context.Context, page *types.WikiPage) (*types.WikiPage, error) {
|
||||
existing, err := s.repo.GetBySlug(ctx, page.KnowledgeBaseID, page.Slug)
|
||||
if err != nil {
|
||||
@@ -86,7 +95,14 @@ func (s *wikiPageService) UpdatePage(ctx context.Context, page *types.WikiPage)
|
||||
|
||||
oldOutLinks := existing.OutLinks
|
||||
|
||||
// Update fields (version is incremented by the repository's optimistic lock)
|
||||
// Snapshot user-visible fields BEFORE mutation so we can decide whether
|
||||
// this is a real content change or just bookkeeping.
|
||||
contentChanged := existing.Title != page.Title ||
|
||||
existing.Content != page.Content ||
|
||||
existing.Summary != page.Summary ||
|
||||
existing.PageType != page.PageType ||
|
||||
existing.Status != page.Status
|
||||
|
||||
existing.Title = page.Title
|
||||
existing.Content = page.Content
|
||||
existing.Summary = page.Summary
|
||||
@@ -97,14 +113,25 @@ func (s *wikiPageService) UpdatePage(ctx context.Context, page *types.WikiPage)
|
||||
existing.Status = page.Status
|
||||
existing.UpdatedAt = time.Now()
|
||||
|
||||
// Re-parse outbound links
|
||||
// Outbound links are a pure derivative of content, so they only shift
|
||||
// when content shifts. Re-parse unconditionally to stay consistent with
|
||||
// the stored body.
|
||||
existing.OutLinks = s.parseOutLinks(existing.Content)
|
||||
|
||||
if err := s.repo.Update(ctx, existing); err != nil {
|
||||
return nil, fmt.Errorf("update wiki page: %w", err)
|
||||
if contentChanged {
|
||||
if err := s.repo.Update(ctx, existing); err != nil {
|
||||
return nil, fmt.Errorf("update wiki page: %w", err)
|
||||
}
|
||||
} else {
|
||||
// No user-visible change — persist bookkeeping fields but preserve
|
||||
// the version so downstream consumers can rely on it.
|
||||
if err := s.repo.UpdateMeta(ctx, existing); err != nil {
|
||||
return nil, fmt.Errorf("update wiki page meta: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Update inbound links: remove old, add new
|
||||
// Update inbound links: remove old, add new. If content didn't change,
|
||||
// oldOutLinks == existing.OutLinks and these calls are effectively no-ops.
|
||||
s.removeInLinks(ctx, existing.KnowledgeBaseID, existing.Slug, oldOutLinks)
|
||||
s.updateInLinks(ctx, existing.KnowledgeBaseID, existing.Slug, existing.OutLinks)
|
||||
|
||||
|
||||
@@ -14,13 +14,17 @@ type WikiPageService interface {
|
||||
// bidirectional link references, and syncs to chunks for retrieval.
|
||||
CreatePage(ctx context.Context, page *types.WikiPage) (*types.WikiPage, error)
|
||||
|
||||
// UpdatePage updates an existing wiki page, re-parses links,
|
||||
// updates bidirectional references, increments version, and re-syncs chunks.
|
||||
// Use for content changes visible to the user.
|
||||
// UpdatePage updates an existing wiki page, re-parses links, and updates
|
||||
// bidirectional references. The `version` field is incremented only when
|
||||
// a user-visible content field (title, content, summary, page_type,
|
||||
// status) actually differs from the stored value — bookkeeping-only
|
||||
// writes (e.g. refreshing source_refs after a same-content re-ingest) are
|
||||
// persisted without bumping the version.
|
||||
UpdatePage(ctx context.Context, page *types.WikiPage) (*types.WikiPage, error)
|
||||
|
||||
// UpdatePageMeta updates only metadata (status, source_refs) without
|
||||
// incrementing version or re-parsing links. Use for publish/archive/source ref changes.
|
||||
// UpdatePageMeta updates only metadata (status, source_refs, etc.) without
|
||||
// incrementing version or re-parsing links. Use for publish/archive/source
|
||||
// ref changes driven by internal reconciliation.
|
||||
UpdatePageMeta(ctx context.Context, page *types.WikiPage) error
|
||||
|
||||
// GetPageBySlug retrieves a wiki page by its slug within a knowledge base.
|
||||
@@ -87,10 +91,16 @@ type WikiPageRepository interface {
|
||||
// Create inserts a new wiki page record.
|
||||
Create(ctx context.Context, page *types.WikiPage) error
|
||||
|
||||
// Update updates an existing wiki page record (increments version — content changes).
|
||||
// Update rewrites a wiki page record with optimistic locking and
|
||||
// unconditionally increments `version`. Callers are responsible for
|
||||
// deciding whether the edit is user-visible — the service layer uses
|
||||
// UpdateMeta for bookkeeping-only writes instead.
|
||||
Update(ctx context.Context, page *types.WikiPage) error
|
||||
|
||||
// UpdateMeta updates only metadata fields (links, status, source_refs) without version bump.
|
||||
// UpdateMeta updates bookkeeping / provenance fields (in/out links,
|
||||
// status, source_refs, chunk_refs, page_metadata, updated_at) without
|
||||
// touching `version`. Safe for link maintenance, re-ingest with an
|
||||
// unchanged body, and status-only transitions.
|
||||
UpdateMeta(ctx context.Context, page *types.WikiPage) error
|
||||
|
||||
// GetByID retrieves a wiki page by its unique ID.
|
||||
|
||||
@@ -84,7 +84,11 @@ type WikiPage struct {
|
||||
OutLinks StringArray `json:"out_links" gorm:"type:json"`
|
||||
// Arbitrary metadata (tags, categories, dates, etc.)
|
||||
PageMetadata JSON `json:"page_metadata" gorm:"column:page_metadata;type:json"`
|
||||
// Version number, incremented on each update
|
||||
// Version number. Incremented only when a user-visible content field
|
||||
// (title, content, summary, page_type, status) actually changes; pure
|
||||
// bookkeeping writes (link maintenance, same-content re-ingest, status
|
||||
// sync from background jobs) leave it untouched so it can be used as a
|
||||
// real "the page was edited" signal.
|
||||
Version int `json:"version" gorm:"default:1"`
|
||||
// Creation time
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
|
||||
Reference in New Issue
Block a user