Course Versioning for an LMS: Editing a Live Course Without Losing Learner Progress
How we version course content in PostgreSQL: immutable published revisions, stable keys that keep learner progress, locked quizzes and an atomic publish.
Key Takeaways
- A published course revision should be an immutable snapshot. Edits go to a separate draft, and database triggers reject any change to a published revision, even one caused by a bug in the application.
- Modules and lessons carry a stable key copied into every new revision. Progress and quiz attempts are stored against that key, so learner progress survives a republish with no migration step.
- Quizzes and flashcard decks are versioned rows locked at publish. The first edit afterwards creates version n+1 (copy-on-write), so past attempts are always graded against the version the learner took.
- Publishing is an atomic pointer switch, verified by deferred constraint triggers at commit. When two people publish at once, exactly one wins, and automated tests cover it.
- The same approach keeps promotional media (cover, intro video) apart from protected lesson media, enforced in the database.
Kuvuty is a learning platform for university courses and workshops in English and Kiswahili, starting in Tanzania. It works closely with the University of Dar es Salaam and Zanzibar University and is in discussions with UNDP. We designed and built kuvuty.com and the platform behind it, and we run both for the client.
One requirement shaped the data model more than any other: a teacher must be able to improve a course that students are already taking. The teacher fixes a typo, rewrites a quiz question, adds a module. Meanwhile a learner is halfway through the same course and must not see a lesson change under their feet, lose completed lessons, or have an old quiz score recalculated.
This article describes how we solved that in PostgreSQL. The patterns apply wherever content is edited while people consume it: courses, compliance training, handbooks, product documentation.
What goes wrong when a live course is edited in place?
Three things, and each is a data-model problem rather than an interface problem.
- Learners see half-finished work. If the draft and the live course are the same rows, a lesson with no video yet is visible to students.
- Progress points at things that moved. If progress is stored against a lesson's row id, any approach that recreates lessons on republish orphans every progress row.
- History changes. If a quiz question is edited after 200 attempts, stored answers no longer match the questions they were given for.
The usual workarounds push the problem into code: a "published" flag per row, a draft copy table, a script that remaps progress after each publish. Each works until someone forgets a step. We wanted the database to refuse the wrong thing.
Published revisions are snapshots
A course has many revisions, and each revision owns its own modules and lessons. Nothing is shared between revisions by row. A revision is WORKING (the draft), PUBLISHED (what learners see) or SUPERSEDED (a former version kept for history). Two partial unique indexes allow at most one WORKING and one PUBLISHED revision per course.
Once a revision leaves WORKING, triggers forbid changing it, its modules or its lessons. This is enforced by the database, not by convention: a write to a lesson whose revision is not WORKING fails with invariant:revision_immutable. An update checks both the old and the new revision, so a lesson cannot be moved out of a frozen revision either.
-- Attached BEFORE INSERT OR UPDATE OR DELETE to course_modules and lessons
-- (error messages shortened).
create function inv_revision_child_working() returns trigger
language plpgsql as $$
begin
if tg_op in ('UPDATE', 'DELETE') and not exists (
select 1 from course_revisions
where id = old.course_revision_id and status = 'WORKING'
for share
) then
raise exception 'invariant:revision_immutable';
end if;
if tg_op in ('INSERT', 'UPDATE') and not exists (
select 1 from course_revisions
where id = new.course_revision_id and status = 'WORKING'
for share
) then
raise exception 'invariant:revision_immutable';
end if;
if tg_op = 'DELETE' then return old; end if;
return new;
end $$;The FOR SHARE lock is what makes this safe under concurrency. Publishing updates the revision row, and that row lock conflicts with FOR SHARE, so a lesson write and a publish of the same revision cannot interleave. A write that arrives mid-publish waits for the publish to commit, then, under PostgreSQL's default READ COMMITTED isolation, re-evaluates the revision row, finds it PUBLISHED and fails. Without the lock, a write could pass the check against the pre-publish row and commit into a revision that is already live.
The revision row has its own lifecycle trigger: new revisions must start as WORKING, the only legal transitions are WORKING → PUBLISHED → SUPERSEDED, and publishing and editing must be separate statements, so one update cannot publish and quietly change the title. The application still checks permissions and gives readable errors. The triggers are the backstop for when the application is wrong, and a test confirms that after publishing, updating a lesson title or deleting a module is rejected by the database.
Stable keys: how progress survives a republish
When a teacher edits a published course, the platform never touches the published rows. On the first edit it copies the published tree into a new WORKING revision, created lazily and not at publish time. Every module and lesson in the copy keeps its stable key, a UUID copied verbatim. Row ids are new; stable keys are not.
Learner data is stored against the stable key, never the row id: lesson progress, quiz attempts and flashcard reviews (by the card's stable key). To show progress, the platform joins it to the lessons of the course's current published revision:
select l.*
from lesson_progress p
join courses c on c.id = p.course_id
join lessons l on l.course_revision_id = c.current_revision_id
and l.stable_key = p.stable_lesson_key
where p.user_id = $1;There is no remapping step after a publish, because nothing needs remapping. In our database tests a student completes a lesson in revision 1, the teacher builds revision 2 with the same stable key, and after the republish the progress row resolves to the revision-2 lesson.
Two rules keep this honest:
- Addressing is by stable key inside the working draft. The editor never accepts a row id from the client. A key from another course does not resolve, which makes cross-course id substitution impossible by construction (tested).
- Removed lessons keep their progress. If a later revision deletes a lesson, its key stops resolving and the progress row stays. Progress counts only lessons in the current revision, so the learner's number is correct and history is not destroyed. Triggers forbid deleting progress and attempt rows.
Quizzes and flashcard decks: locked at publish, copied on write
Each quizzes or flashcard_decks row is itself a version, identified by a stable key and a revision number, with a locked_at timestamp.
- While unlocked, authors edit it freely.
- Publishing locks every deck and quiz the draft uses, in the same transaction. Locking validates the content: at least one card or question, at least two options per question, exactly one correct.
- Once locked, triggers freeze the version and its questions, options and cards.
The interesting moment is the first edit after a publish. The new draft's lesson still points at the live, locked quiz. Instead of unlocking it, the platform creates version n+1, copies the questions and options with their stable keys, and repoints only the draft lesson at the copy.
The result: learners keep seeing the locked version until the next publish. A quiz attempt references the exact version it was taken on, so an open attempt continues on that version and a graded attempt is always reviewed against its own version. Scores and correct answers never change retroactively. Grading runs on the server, in one transaction, against the locked answer key, and the player never receives which option is correct. A partial unique index allows only one open attempt per learner and lesson, so a double-click or two tabs starting the same quiz collapse into a single attempt.
The cost is extra rows: each editing session after a publish can create new versions. For course-sized content that is cheap, and it is worth it for never having to explain why a historical score changed. One edge case is documented: if a later revision removes a lesson, an open attempt on it is abandoned, since the quiz can no longer be reached.
Publishing as an atomic pointer switch
A course points at its live revision through current_revision_id. Publishing is one transaction: lock the course row, lock the decks and quizzes, mark the old revision SUPERSEDED and the draft PUBLISHED, then move the pointer. The order is forced: the one-PUBLISHED-per-course index is not deferrable and is checked as each row changes, so the old revision must be superseded before the new one is marked published.
Before that, a preflight shows the author a readable checklist, and the same rules exist again in the database. A revision cannot be published with no lessons, with a lesson that has no content behind it (placeholders are allowed in drafts, never in a published course), or with media that is not ready or belongs to another publisher.
The check we trust most runs at commit. We use deferred constraint triggers: PostgreSQL supports constraint triggers that can be deferred to fire when the transaction commits, and deferred constraints are not checked until commit. Ours verifies that if the pointer is set it references a PUBLISHED revision of that same course, and that a course with a PUBLISHED revision has a pointer. A deferrable composite foreign key stops the pointer referencing another course's revision.
Two tests show the effect. A transaction that marks a revision PUBLISHED without switching the pointer fails at commit with invariant:published_revision_must_be_current, and the revision is still WORKING afterwards. Pointing a course at a WORKING revision also fails at commit.
Deferring matters because mid-transaction the data is briefly inconsistent: after the first step, the pointer still references a revision that is now SUPERSEDED. An immediate check would reject every valid publish. The rule only has to hold when the transaction commits, and because PostgreSQL readers only ever see committed data, no reader can observe a course without a coherent live revision. The same reasoning applies to module and lesson ordering: their position uniqueness constraints are DEFERRABLE INITIALLY DEFERRED, so the editor can swap two lessons' positions inside one transaction.
What if two people publish at once?
Exactly one wins, and both layers are tested. At the database level, two concurrent transactions publish the same revision: one succeeds, one fails, and one PUBLISHED revision remains. At the service level, an owner and an administrator publish at the same moment: exactly one call succeeds. Every course mutation, publishing included, starts by locking the course row with FOR UPDATE, so publishes run one after the other and the second finds nothing left to publish. The same lock serialises the lazy creation of the draft: in a related test, four simultaneous first edits after a publish create exactly one working revision containing all four edits.
The same rigor for media
A course can have a cover image and an introductory video visible to anyone on the public catalog, and also lecture videos, PDFs and ZIP resources that only learners with access may reach. A public path must never become a way into lesson content.
We recorded this as two classes of asset, decided by relationship and not by an is_public flag that could drift. Presentation assets fill slots on the course revision and are public only while they sit in the current published revision. Educational assets are referenced from lessons and governed by access grants. Triggers enforce the separation (decision record ADR-0012): an asset used by a lesson can never fill a presentation slot, so a lecture video cannot be passed off as an intro even through a service bug. Because slots live on revisions, a new cover becomes public only at the next publish.
How we know it holds
These invariants are tested against a real PostgreSQL database, not mocks: immutability after publish, progress resolving across revisions, the commit-time pointer checks, both concurrent-publish cases and the quiz locking rules. At the 27 November 2026 release check the project ran 660 unit and database tests and 45 browser journeys, all passing.
Two caveats. Logic that lives in both the service and a trigger exists in two places, so we reserve triggers for invariants whose breach would be an integrity incident and pin each one with a test. And the design copies rows on each editing session, which suits course-sized content, not millions of rows per revision.
When this pattern fits
Consider it when people consume content while others edit it, when their activity must stay meaningful across versions, and when you must be able to say later exactly what someone saw. If that matches a system you are building or rescuing, our custom web application development and software architecture work starts with these questions. See the Kuvuty case study for the project, or contact us.
Frequently asked questions
How do you publish course updates without losing student progress?
Store progress against a stable key copied into every revision, not against the lesson's row id. After a republish, progress resolves to the equivalent lesson in the new revision and nothing needs remapping.
Why use database triggers instead of application checks?
Application checks give good error messages but only protect the code paths that use them. A trigger protects published content from every path, including a future bug or a manual fix. We use both.
What happens to quiz attempts when a quiz is edited?
Each attempt references the locked quiz version it was taken on. Editing creates a new version, so past attempts keep their questions, answers and score.
How do you handle two editors publishing at the same time?
Publishing locks the course row, so the publishes run in sequence and the second is rejected. Constraint triggers at commit also stop a half-finished publish from becoming visible.