Handle database schema changes using Drizzle ORM. Use when modifying database tables, columns, relationships, or any schema changes. Includes migration generation, application, and verification steps.
This skill guides you through the complete database migration process using Drizzle ORM.
<schema-file>Edit <schema-file> with your schema changes:
// Example: Adding a new column
export const users = pgTable("users", {
id: text("id").primaryKey(),
email: text("email").notNull(),
newField: text("new_field"), // New field added
createdAt: timestamp("created_at").defaultNow().notNull(),
});
Run the migration generation command:
<generate-command>
This command:
<migrations-dir>Example output:
✔ Loaded env from .env
✔ Pulling schema from database...
✔ Pulling migration state...
✔ Loaded all config
✔ Pulling existing migrations...
◐ Pulling schema from database...0001_new_field_migration.sql migrated!
Check the generated migration file in <migrations-dir>:
-- Example: migrations/0001_add_new_field.sql
ALTER TABLE "users" ADD COLUMN "new_field" text;
Important: Review the SQL to ensure it matches your intentions.
Run the migration to update your database:
<migrate-command>
This applies all pending migrations to your database.
Open Drizzle Studio to verify:
<studio-command>
Check:
Always commit both files together:
git add <schema-file> <migrations-dir>/
git commit -m "feat: add new_field to users table"
export const newTable = pgTable("new_table", {
id: text("id").primaryKey().$defaultFn(() => crypto.randomUUID()),
name: text("name").notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
});
export const posts = pgTable("posts", {
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
// other fields...
});
// Before