See Ash Code logo See Ash Code

Why my drafts lived in files and my published posts lived in a database

Ashlynn Antrobus
• • 3 min read

I want to edit my blog posts as Markdown files. I also want the public site to read a published post without rendering the Markdown from scratch for every request.

The February post-management code uses files for the editable content and a database for the published representation. It isn’t quite as simple as “drafts have no database record.” Drafts have database metadata too.

One dragon edits a manuscript while another puts a book on a library shelf.

Illustration: an editable manuscript and a shelved edition serve different purposes.

That distinction matters when following the handler boundaries. The stores have different jobs, and a successful write to one doesn’t automatically update the others.

What each store contains

The filesystem draft has Markdown content and YAML frontmatter. The database post has fields including the slug, title, author, publication state, timestamps, and HTML content.

The selected revision uses SQLModel for persistence, and its settings build a PostgreSQL connection URL using the psycopg2 driver. SQLModel is the access layer here; it doesn’t identify a different database by itself.

When I save an edit, the save handler checks ownership using the database post, updates the Markdown file, and attempts a GitHub commit. Updating the file doesn’t publish its new content automatically.

Publishing produces another representation

The publication handler reads the draft, renders its Markdown, sanitizes the HTML, updates the post’s published state, and saves the database record. It then tries to update the file’s frontmatter and commit the updated draft to GitHub.

The order is important:

Markdown draft -> render -> sanitize -> published Post
                                            |
                                            v
                                    save database record
                                            |
                                            v
                              update draft file, then GitHub

The database save is required. The later file and GitHub updates are best-effort operations in this handler.

That gives the reader a stored HTML representation and leaves me with an editable source file. It also gives the application several representations whose publication state can disagree after a partial failure.

Try a failure between the saves

Consider a simulated publication where rendering and sanitizing succeed, but the database save raises an exception. The handler propagates that failure before reaching the frontmatter update. It doesn’t report that the later steps completed.

Now move the simulated failure later: the database save succeeds, and writing the draft frontmatter raises OSError. The handler logs that the post is published but the file may be stale, then continues. A failed GitHub commit likewise leaves publication in place and records the synchronization problem.

Those outcomes are different. Repeating “publish” blindly after a synchronization warning might be the wrong recovery operation. I need to know which representation was saved and which still needs repair.

Saving the draft and publishing the HTML still happen separately. The handler has to tell me which save succeeded so I can retry the right one.

AA

Written by Ashlynn Antrobus

Software developer, author, and builder. Sharing technical lessons learned from building tools like Ebook2Text, IndexedDict, and rag-mcp, and navigating life as a programmer with dyslexia.