Define new Drizzle schema and generate PostgreSQL migration.
Current Database Status
- •Existing tables: !
grep -r "pgTable" --include="*.ts" packages/db/src/ | wc -ldefined - •Database URL: !
echo "DATABASE_URL configured: $(grep -q DATABASE_URL .env* && echo 'YES' || echo 'NO')" - •Migrations count: !
ls packages/db/migrations/ 2>/dev/null | wc -l 2>/dev/null || echo "0"
Create:
- •Update
@repo/db/schema.tswith new table/relations - •Run
drizzle-kit generateto create migration file - •Commit migration to repository
Schema Generation Steps
- •Define table with $ARGUMENTS columns
- •Add relations for foreign keys if needed
- •Include Zod validation schemas
- •Export types from schema file
Schema conventions:
- •Use camelCase for column names (maps to snake_case in DB)
- •Define relations for foreign keys with
relations() - •Include Zod schemas for type safety alongside Drizzle schemas
- •Use PostgreSQL types with Drizzle enums where needed
- •Add indexes for frequently queried columns
Example Generated Schema
typescript
export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
email: varchar('email', { length: 256 }).notNull().unique(),
createdAt: timestamp('created_at').defaultNow(),
});
// Zod schema for validation
export const insertUserSchema = createInsertSchema(users);
export const selectUserSchema = createSelectSchema(users);
Migration Testing
After generation, test migration with:
bash
pnpm db:push # Apply to dev DB pnpm db:studio # Verify in Drizzle Studio