sailfork / t3code-github / .repos/ alchemy-effect/ website/ src/ content/ docs/ turso/ migrations.mdx

Point migrations at a folder of .sql files. Each deploy runs the new ones, in order.

Apply a folder

const db = yield* Turso.Database("Db", {
  group: group.name,
  migrations: "./migrations",
});
-- migrations/0001_users.sql
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);

If any statement in a file fails, none of that file's changes are kept and the deploy fails.

Alchemy keeps track of which files ran in the __alchemy_migrations table.

Make migrations with Drizzle

const schema = yield* Drizzle.Schema("Schema", {
  schema: "./src/schema.ts",
  out: "./migrations",
  dialect: "sqlite",
});

const db = yield* Turso.Database("Db", {
  group: group.name,
  migrations: schema,
});

Drizzle.Schema writes a migration when src/schema.ts changes. Add Drizzle.providers() to your Stack and commit the ./migrations folder.

Rather run drizzle-kit generate yourself? Pass the folder instead: migrations: "./migrations".

Already used drizzle-kit on this database? Alchemy picks up its history on the first deploy.

Use a different tracking table

migrations: { dir: "./migrations", table: "schema_migrations" },

Seed with SQL files

const db = yield* Turso.Database("Db", {
  group: group.name,
  migrations: "./migrations",
  importFiles: ["./seed/users.sql"],
});

importFiles run after migrations. A file runs again only if you change it.

Don't edit old migrations

Changing or deleting a file that already ran makes the deploy fail. Add a new file instead.

Where next