Mainstay GitHub
Editing

Drafts beside what is live

An editor needs somewhere to work on a change before visitors see it, and a way back from a bad publish. One way is to keep every version in a versions table and work out which is live on every read. Mainstay keeps the entry's own row as what is live, always. The site's read stays the single-row select it was, with no version resolution in it, because the hot path is the public site and versioning has no business being on it.

A draft is what differs

A draft sits beside the entry, at most one per entry, in a table of its own, mainstay_drafts. Mainstay keeps one draft rather than a queue of them, so which version to show collapses to whether there is a draft. It holds only the fields that differ from what is live, so a change to the summary is a draft of the summary.

php artisan tinker
$draft = Mainstay::drafts()->save(Article::class, [
    'summary' => 'A sharper summary.',
], entry: 5, overrideAccess: true);

Mainstay::drafts()->publish($draft->id, overrideAccess: true);

In the admin these are Save draft and Publish. They need different capabilities, editing and publishing, so a writer can be let draft and not publish. The admin's Content tab draws the page from the form as it stands; the site itself never looks at a draft.

Publishing is one transaction

Publishing writes the draft over the entry's row, files the version it replaces as a revision and deletes the draft, in one transaction, with no resolution logic anywhere. The query layer announces it with a Published event, dispatched once the transaction has committed. It is one of four: Saved follows a create, an update or a global's save, Deleted a move to the trash, and Restored a restore from it. A plugin that clears a cache or rebuilds something on every live change listens to all four.

Revisions

Every write that replaces something live files the outgoing version: the whole entry, its shared fields, every language's translated ones, its view and its terms, as one JSON snapshot in one table shared by every type, mainstay_revisions. Not a table per type mirroring its columns, since history is read whole and never queried by field, the same reasoning as for blocks. Each revision records who had put that version live and when it stopped being live.

A revision is made when content stops being live, so every stored revision was really public once, which is the only thing anyone asks to roll back to. Edits to a draft make none: there is no history of drafts, and what happened between two publishes is gone, deliberately. Full snapshots rather than diffs, so a revision is read whole and depends on none of the others. The last 50 of each entry are kept, a number mainstay.revisions changes, or null to keep them all.

php artisan tinker
$revision = Mainstay::revisions()->of(Article::class, 5, overrideAccess: true)->first();

$draft = Mainstay::revisions()->restore($revision->id, overrideAccess: true);

Restoring is saving a draft

Restoring a revision makes it the entry's draft, which then leaves through Publish like any other. Restoring has no rules of its own to get wrong.

There is one sharp edge, and it comes straight from declaring types in code. A revision is shaped by the type as it was declared then, and classes change with deploys. An old snapshot may hold a field the type no longer declares, a language no longer configured, or a value its field now refuses. Old revisions are never migrated forward. What a restore cannot bring back is left as it is live and reported on the draft it hands back, and a field declared since keeps its live value, the revision having nothing to say about it.

The trash

Deleting an entry moves it to the trash, in every language at once, and takes its address out of the lookup in the same request, so the address stops answering immediately rather than resolving to a row the trash then hides. Restoring it puts it back at its address. If another entry took that address meanwhile, the restored one's slug is suffixed, since a restore has no editor typing a slug to ask. Making an entry the front page suffixes the page it replaces in the same way, when that page's own path is already taken. Emptying the trash deletes for good.

Tags and other terms are the exception to all of this: a taxonomy's terms are written live, with no drafts and no revisions.

Keep reading

All articles