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

migrations.md

48 lines2.1 KB

1---
2title: Persistence Migrations
3id: migrations
4---
5​
6Run persistence schema changes as migrations. A change on the first production request can fail under load or race across instances.
7​
8Your adapter owns its schema. TanStack AI never reads your tables, so you choose the table layout and how changes are applied. Apply each change before you deploy the code that uses it.
9​
10## Local development: create tables on open
11​
12A hand-written adapter can create its tables the first time it opens the database. The SQLite example in [Build your own adapter](./build-your-own-adapter) does this behind a `migrate` flag with `CREATE TABLE IF NOT EXISTS`, so you can run it many times:
13​
14```ts ignore
15import { sqlitePersistence } from './sqlite-persistence'
16​
17const persistence = sqlitePersistence({
18 url: 'file:.data/state.sqlite',
19 migrate: true,
20})
21```
22​
23This is good for local development and tests. Do not run migrations at request time in production.
24​
25## Production: apply migrations when you deploy
26​
271. Keep the DDL for the store tables (`messages`, `runs`, `interrupts`, `metadata`) in a versioned migration that you can review.
282. Back up the production state, if your policy requires it.
293. Apply the migration with the tool that you use for the rest of your database.
304. Deploy the code that depends on the new schema.
315. Make the rollback and partial-deploy behavior explicit.
32​
33If you build the adapter with an ORM or a query builder, let that tool own the migration history:
34​
35- A Drizzle schema uses `drizzle-kit`.
36- A Prisma models fragment uses `prisma migrate`.
37- A raw SQL adapter keeps plain `.sql` files in the repo.
38​
39The adapter skills in `@tanstack/ai-persistence` cover each of these workflows.
40​
41## An existing schema owns its own migrations
42​
43You can map the store contracts onto tables you already have. See [Build your own adapter](./build-your-own-adapter#existing-database-map-the-contracts-onto-your-schema).
44​
45Then those tables are part of your app schema, and your current migration tool owns them:
46​
47- Add the columns that the stores need in a normal migration.
48- Keep any extra columns of your own nullable or with a default, so the inserts of the stores still work.