A site with a database

Store data in SQLite, try it safely on Preview, then change Live's database with a migration.

This tutorial is for people who want to see how data works. Your agent normally does all of this for you.

1. Ask for something that stores data

Add an events page. I want to add events with a title, date and description, and show upcoming ones.

Your agent will create a migration: a small SQL file under migrations/app.sqlite/, such as 001_events.sql. Migrations are how a database's structure changes.

2. Play with it on Preview

Preview has its own copy of the data. Add events, delete them, break things: none of it touches Live.

You can look at the data yourself:

hm sqlite schema
hm sqlite query "select title, starts_at from events"

3. Put it Live

On a paid Plan, putting a database change Live takes two steps:

hm migrate     # change Live's database
hm publish     # switch Live to the new code

hm migrate saves a Restore Point first, so you can go back if something's wrong. hm publish refuses to run until the migrations are done.

Why two steps?

Live keeps running its old code while the database changes. So add things first (a new column), and use them in the next Publish. Code and data explains why.

Good to know

  • The data you added on Preview is never published. Live keeps its own data.
  • Never edit a migration that's already Live. Add a new one instead.