Skip to content
Luna
Esc
↑↓navigate↵open⌘Jpreview
On this page

Change a schema

Add, rename and remove tables and columns in lunas that already hold data, and move that data with the onMigrate hook.

Your tables change as the app grows, but every luna already holds rows in the old shape. Luna checks each schema change when you deploy, and applies it to each luna as that luna moves to the new version.

Add a table or a column

New tables and new columns that can be empty or have a default need nothing more than a deploy. This version of List adds an optional note.

import { defineLuna, t, table } from "luna";

export const List = defineLuna({
  name: "List",
  visibility: "public",
  key: { id: t.string() },
  schema: {
    items: table({
      id: t.id(),
      text: t.string(),
      note: t.string().optional(),
    }),
  },
  handlers: (h) => ({
    add: h({ args: { text: t.string() } }, (ctx, { text }) => {
      ctx.db.items.insert({ text });
    }),
  }),
});

Rows that existed before the deploy get null for note, or the default when the column has one. A luna applies the change when it moves to the new version, which The life of a luna describes.

Know which changes a deploy accepts

luna deploy compares the new schema with the one running, and refuses a change that would leave existing rows unreadable. An onMigrate hook on the luna type lets most of those through, because it moves the data itself.

Change Without onMigrate With onMigrate
Add a table, an optional column or a column with a default Accepted Accepted
Add a required column without a default Refused Accepted
Make an optional column required, without a default Refused Accepted
Change a column’s type Refused Accepted
Change a table’s primary key Refused Accepted
Remove a column and add one of the same type Asks to confirm Asks to confirm
Remove a table or a column Accepted, the data stays Accepted, the data stays
Change the luna type’s key fields Refused Refused

A removed column keeps its values in each luna, and new rows leave it empty, even when it was required.

Run luna deploy --preflight to see the list for your change without deploying. Deploy an app explains each line it prints.

Fill in existing rows with onMigrate

A required column without a default has no value for rows that already exist. The deploy refuses it with List: new required column items.qty needs a default, or an onMigrate that fills it in.

Add the hook to the luna type, and fill the column in with SQL.

schema: {
  items: table({ id: t.id(), text: t.string(), qty: t.int() }),
},
hooks: {
  onMigrate: (ctx) => {
    ctx.exec("UPDATE items SET qty = 1 WHERE qty IS NULL");
  },
},

onMigrate(ctx, { from, to }) runs in each luna that already exists, as it moves to a new version. It runs before any other handler, in one transaction with the schema change. A luna created on the new version never runs it.

The hook runs on every move to a new version, not only the one that added it. Write it so that a second run changes nothing, as the WHERE qty IS NULL above does. Its argument holds the old and new version numbers, for steps that apply to one move only.

If the hook throws, or runs longer than 60 seconds, nothing it did stays. That luna stays on its old version until you deploy a newer one. In Python the hook is on_migrate, shown in Write lunas in Python.

Try a migration before it deploys

Before a deploy goes ahead, Luna tries the new schema and the onMigrate hook on copies of real lunas. It samples up to 5 lunas of each type whose tables change or that has the hook. A migration that fails on any copy blocks the deploy, and nothing is deployed. The copies are thrown away, so the lunas themselves are never touched.

Because only a sample is tried, a migration can still fail later on data none of the copies held. That luna then stays on its old version.

Rename a column

Luna cannot tell a renamed column from one removed and another added. When a column goes and one of the same type appears, the deploy asks whether it is a rename, and --yes answers in advance.

Luna does not copy the values. Copy them in onMigrate, here from note to comment.

hooks: {
  onMigrate: (ctx) => {
    ctx.exec(
      "UPDATE items SET comment = note, note = NULL WHERE note IS NOT NULL",
    );
  },
},

Clearing note as it copies makes a second run find nothing to move.

Change a column’s type or a primary key

Existing rows hold values of the old type, and existing tables keep their old key. The deploy refuses either change until the luna type has onMigrate.

In the hook, convert the existing values, or rebuild the table under its new key, with ctx.exec. Run SQL covers running statements.

Remove a table, a column or a luna type

Removing a table or a column from the schema leaves its data in each luna. Your code can no longer reach it through ctx.db, but the rows stay until a migration deletes them with SQL.

Removing a whole luna type is accepted as well. Its lunas keep their data, but nothing can call them any more.

Keep a luna type’s key fields

The key fields are each luna’s identity, so a deploy always refuses to change them, even with onMigrate. To key the data differently, make a new luna type and move the data across with calls or messages, as Call another luna shows.

Next steps

Was this page helpful?