Skip to content

Migrations

One way to run migrations in Val Town is to write a single val and keep updating it with each migration. The version history of that val can act as a log of schema changes.

Let’s look at an example…

In our first migration, we create a users table:

Migration 1Run in Val Town ↗
import { sqlite } from "https://esm.town/v/std/sqlite";
await sqlite.execute(`
create table users (
id integer primary key autoincrement,
email text not null unique
)
`);

Later, we realize we also want to store names. We can update the same val to alter the table:

Migration 2Run in Val Town ↗
import { sqlite } from "https://esm.town/v/std/sqlite";
await sqlite.execute(`
alter table users
add column name text
`);