Seeking Advice: Simplifying Versioned Document Publishing in a B2B Marketplace

I’m building a B2B marketplace and looking for advice from developers experienced with database-backed applications, document management, and version control.

I’m trying to simplify an administrative workflow while maintaining data integrity and reliability.

The use case

A single authorized administrator manages a document template used to generate agreements between marketplace participants.

The desired workflow is simple:

  1. Open the currently published template.

  2. Edit the content and configuration.

  3. Preview the complete document.

  4. Save and publish the updated version.

The new version should become active for future agreements, while existing agreements retain their original document versions. Previous versions should remain available for historical reference.

The challenge

The application currently has lifecycle rules enforced at both the application and database levels. Updating the document-management workflow requires changes that preserve those safeguards.

We also need to ensure that published documents remain consistent with the exact content that was validated, rather than allowing the underlying source document to change unexpectedly.

Questions for the community

  • What is the simplest reliable pattern for editing a published template and atomically replacing it with a new immutable version?

  • How would you preserve existing agreement references while ensuring new agreements use the latest published version?

  • What database safeguards would you recommend to prevent failed or concurrent updates from leaving the application in an inconsistent state?

  • What are the best practices for ensuring that stored documents remain immutable and verifiable?

  • Would you recommend any alternative architecture or workflow that achieves these goals with less complexity?

The goal is to maintain a straightforward administrative experience without sacrificing version history, data integrity, or reliability.

I’d appreciate practical recommendations, relevant documentation, and lessons learned from similar applications. Thanks!

1 Like

The simplest pattern that holds up is “versions are rows you never edit”:

  • One append-only table for versions. Each row has the template id, a version number, the full content, and a hash of that content. Nothing ever updates or deletes a row. You can enforce that in Postgres by removing UPDATE and DELETE rights on the table or with a trigger that rejects them.
  • One pointer for “current”. The template row stores the id of its published version. Publishing is a single transaction: insert the new version row, then move the pointer. If anything fails, both roll back and the old version is still live.
  • Agreements point at a version, not a template. Save the version id on the agreement when it’s created. Old agreements keep their exact text and new ones pick up the latest automatically.
  • Concurrent edits. Put a unique constraint on (template id, version number) and lock the template row inside the publish transaction (SELECT ... FOR UPDATE). Two admins publishing at once can’t both win.
  • Verifiable. Store a SHA-256 of the content at publish time and compare it when you render. A mismatch means something changed that shouldn’t have.

Drafts and previews fall out of this for free: a draft is just a version row the pointer doesn’t reference yet, and preview renders one version id.

When you hand this to the Agent, give it the rules above as constraints, do the table change and the publish function as two separate tasks, and test against the development database first. Happy to look at your current lifecycle rules if you paste them.