Schema
Learn how to update your database schema and migrate changes with Drizzle.
The database schema is defined in packages/database/src/schema.ts. This schema file uses Drizzle's schema definition to describe your database tables, relationships and types.
Update
To update your database schema, you can edit the schema.ts file located in the /packages/database package. More information about the Drizzle schema can be found here.
For example we can add a new field businessPhone to the contact table.
export const contactTable = pgTable('contact', {
// ...
businessPhone: varchar('address', { length: 32 })
// ...
});That's it, we have defined a new field. The field is an optional string with a max length of 32 characters. Now let's inform the database about the change using a migration.
Migration
Create a new migration by running the following command:
pnpm --filter database generateThis will keep a migration history of your database changes. Drizzle creates a new migrations under packages/database/drizzle/migrations.
Push changes to the database
To apply your schema changes to the database, run:
pnpm --filter database pushAfter the migration is applied, Drizzle creates a new migration history record in the database.