Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

migrations.md

56 lines2.2 KB

1---
2title: Persistence Migrations
3id: migrations
4---
5​
6Your adapter owns its schema. TanStack AI never inspects your tables, so you
7decide the table layout and how schema changes are applied. Apply those changes
8before deploying code that reads or writes the corresponding stores.
9​
10## Create tables on open, for local development
11​
12A hand-rolled adapter can create its tables the first time it opens the database.
13The SQLite example in [Build your own adapter](./build-your-own-adapter) does this
14behind a `migrate` flag with `CREATE TABLE IF NOT EXISTS`, so it is idempotent:
15​
16```ts ignore
17import { sqlitePersistence } from './sqlite-persistence'
18​
19const persistence = sqlitePersistence({
20 url: 'file:.data/state.sqlite',
21 migrate: true,
22})
23```
24​
25This is convenient for local development and tests. Avoid request-time migrations
26in production.
27​
28## Apply migrations in production
29​
30In production, run schema changes through your normal deployment workflow, not on
31first request. Keep the DDL for the four tables (`messages`, `runs`,
32`interrupts`, `metadata`) in a versioned migration and apply it with the same
33tool you use for the rest of your database, before the new code ships.
34​
35If you build the adapter with an ORM or query builder, let that tool own the
36migration journal. A Drizzle schema drives `drizzle-kit`; a Prisma models
37fragment drives `prisma migrate`; a raw SQL adapter checks in plain `.sql` files.
38The adapter-building skills in `@tanstack/ai-persistence` cover each of these
39workflows.
40​
41## An existing schema owns its own migrations
42​
43If you map the store contracts onto tables you already have (see
44[Build your own adapter](./build-your-own-adapter#existing-database-map-the-contracts-onto-your-schema)),
45those tables are part of your application schema, and your existing migration tool
46already owns them. Add the columns the stores need in a normal migration. Keep any
47extra app-owned columns nullable or defaulted so the stores' inserts still
48succeed.
49​
50## Upgrade discipline
51​
521. Keep the DDL for the store tables in a reviewable migration, not inline in
53 request handlers.
542. Back up production state where required.
553. Apply migrations before deploying code that depends on them.
564. Keep rollback and partial-deployment behavior explicit.