The best way to control Claude Code, Codex and more from any computer or phone
| 1 | --- |
| 2 | title: Migrations |
| 3 | description: Run SQL migrations and seed files on every deploy. |
| 4 | --- |
| 5 | |
| 6 | Point `migrations` at a folder of `.sql` files. Each deploy runs the new ones, in order. |
| 7 | |
| 8 | ## Apply a folder |
| 9 | |
| 10 | ```typescript |
| 11 | const db = yield* Turso.Database("Db", { |
| 12 | group: group.name, |
| 13 | migrations: "./migrations", |
| 14 | }); |
| 15 | ``` |
| 16 | |
| 17 | ```sql |
| 18 | -- migrations/0001_users.sql |
| 19 | CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL); |
| 20 | ``` |
| 21 | |
| 22 | If any statement in a file fails, none of that file's changes are kept and the deploy fails. |
| 23 | |
| 24 | Alchemy keeps track of which files ran in the `__alchemy_migrations` table. |
| 25 | |
| 26 | ## Make migrations with Drizzle |
| 27 | |
| 28 | ```typescript |
| 29 | const schema = yield* Drizzle.Schema("Schema", { |
| 30 | schema: "./src/schema.ts", |
| 31 | out: "./migrations", |
| 32 | dialect: "sqlite", |
| 33 | }); |
| 34 | |
| 35 | const db = yield* Turso.Database("Db", { |
| 36 | group: group.name, |
| 37 | migrations: schema, |
| 38 | }); |
| 39 | ``` |
| 40 | |
| 41 | `Drizzle.Schema` writes a migration when `src/schema.ts` changes. Add `Drizzle.providers()` to your Stack and commit the `./migrations` folder. |
| 42 | |
| 43 | Rather run `drizzle-kit generate` yourself? Pass the folder instead: `migrations: "./migrations"`. |
| 44 | |
| 45 | Already used drizzle-kit on this database? Alchemy picks up its history on the first deploy. |
| 46 | |
| 47 | ## Use a different tracking table |
| 48 | |
| 49 | ```typescript |
| 50 | migrations: { dir: "./migrations", table: "schema_migrations" }, |
| 51 | ``` |
| 52 | |
| 53 | ## Seed with SQL files |
| 54 | |
| 55 | ```typescript |
| 56 | const db = yield* Turso.Database("Db", { |
| 57 | group: group.name, |
| 58 | migrations: "./migrations", |
| 59 | importFiles: ["./seed/users.sql"], |
| 60 | }); |
| 61 | ``` |
| 62 | |
| 63 | `importFiles` run after migrations. A file runs again only if you change it. |
| 64 | |
| 65 | ## Don't edit old migrations |
| 66 | |
| 67 | Changing or deleting a file that already ran makes the deploy fail. Add a new file instead. |
| 68 | |
| 69 | ## Where next |
| 70 | |
| 71 | - [SQL migrations](/sql/effect-sql/migrations) — how migrations work for every database |
| 72 | - [Database per tenant](/turso/database-per-tenant#update-existing-tenants) — updating many databases |