Skip to content

Add post revision history#507

Merged
compscidr merged 4 commits intomainfrom
feature/post-revision-history
Mar 16, 2026
Merged

Add post revision history#507
compscidr merged 4 commits intomainfrom
feature/post-revision-history

Conversation

@compscidr
Copy link
Copy Markdown
Collaborator

Summary

Adds the ability to view and roll back to previous versions of a post.

How it works

  • Every time a post is updated via the admin UI, a PostRevision snapshot is saved with the pre-edit state (title, slug, content, draft, post type)
  • The admin post editor shows a "Revision History" table at the bottom listing all previous versions with timestamps
  • Clicking "Restore" on any revision saves the current state as a new revision first (so rollbacks are always reversible), then applies the old version

Changes

File Change
blog/revision.go New PostRevision model
tools/migrate.go Add PostRevision to AutoMigrate
admin/admin.go Save revision in UpdatePost; add ListRevisions and RollbackRevision handlers
goblog.go Register GET /api/v1/posts/:id/revisions and POST /api/v1/posts/:id/revisions/:revisionId/rollback
templates/post-admin.html Revision history section with AJAX loading
www/js/admin-script.js rollbackRevision() function
admin/admin_test.go Tests for listing, rollback, auth checks, and bad revision ID

Design decisions

  • Tags not stored in revisions — tag associations add complexity (join table) and are rarely the critical part of a rollback. Users can adjust tags manually after restoring.
  • Revision on every save — the admin UI saves on every blur (onfocusout), which may create many revisions. If this becomes noisy, a future enhancement could skip revisions where content is identical.
  • Rollback creates a revision — ensures the user can always undo a rollback.

Closes #163

Test plan

  • All existing tests pass
  • New tests: list revisions, rollback restores correct state, auth checks, bad revision 404
  • Edit a post multiple times, verify revision history appears
  • Restore an old revision, verify content reverts and a new revision is created

🤖 Generated with Claude Code

Save a snapshot of the post before each update so users can view
previous versions and roll back to them.

- Add PostRevision model (blog/revision.go) storing title, slug,
  content, draft status, and post type for each revision
- Save a revision in UpdatePost before applying changes
- Add GET /api/v1/posts/:id/revisions to list revisions
- Add POST /api/v1/posts/:id/revisions/:revisionId/rollback to
  restore a previous version (saves current state as a new revision
  first, so rollbacks are always reversible)
- Add revision history section to post-admin.html with AJAX loading
- Add rollbackRevision JS function with confirmation dialog
- Add tests for listing revisions, rollback, and auth checks

Closes #163

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 16, 2026 22:00
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds post revision history functionality, allowing admins to view and roll back to previous versions of a post. Each time a post is updated, a snapshot of its pre-edit state is saved as a PostRevision. The admin post editor displays a revision history table and provides a "Restore" action that saves the current state before applying the old version.

Changes:

  • New PostRevision model and two admin API endpoints (ListRevisions, RollbackRevision) with corresponding route registration and DB migration
  • Frontend revision history UI in the admin post editor, loaded via AJAX with a rollback function
  • Test coverage for listing revisions, rollback, auth checks, and invalid revision ID

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
blog/revision.go New PostRevision GORM model to store post snapshots
tools/migrate.go Adds PostRevision to the AutoMigrate call
admin/admin.go Saves revision in UpdatePost; adds ListRevisions and RollbackRevision handlers
goblog.go Registers GET and POST routes for revision listing and rollback
templates/post-admin.html Adds revision history section with AJAX loading and restore links
www/js/admin-script.js Adds rollbackRevision() function with confirmation dialog
admin/admin_test.go Tests for revision listing, rollback, auth checks, and 404 on bad revision ID

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

compscidr and others added 3 commits March 16, 2026 15:12
- Skip creating a revision when nothing has changed (compare title,
  slug, content, draft, post_type_id before saving)
- Add doc comment to PostRevision struct
- Add .fail() handler to revisions AJAX call for error feedback
- Wrap rollback in a transaction with error handling so save-revision
  and apply-revision are atomic

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Use gorm:"type:text" instead of legacy sql:"type:text;" on Content
- Remove redundant safeSlug() in change detection since slug is
  already normalized earlier
- Add missing trailing newline to admin-script.js

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Gin can't have different wildcard names (:id vs :yyyy) at the same
path segment. Move revision routes from /api/v1/posts/:id/revisions
to /api/v1/revisions/:id to avoid conflicting with the existing
/api/v1/posts/:yyyy/:mm/:dd/:slug route.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@compscidr compscidr merged commit 4705e87 into main Mar 16, 2026
1 check passed
@compscidr compscidr deleted the feature/post-revision-history branch March 16, 2026 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support post history

2 participants