secondsky/claude-skills / plugins/drizzle-orm-d1/skills/drizzle-orm-d1
drizzle-orm-d1
Verified| Type-safe ORM for Cloudflare D1 databases using Drizzle. Use when: building D1 database schemas, writing type-safe SQL queries, managing migrations with Drizzle Kit, defining table relations, implementing prepared statements, using D1 batch API, or encountering D1_ERROR, transaction errors, foreign key constraint failures, or schema inference issues.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 9.2 KB |
| references/common-errors.md | markdown | 5.3 KB |
| references/error-catalog.md | markdown | 7.9 KB |
| references/links-to-official-docs.md | markdown | 1.8 KB |
| references/migration-workflow.md | markdown | 2.7 KB |
| references/query-builder-api.md | markdown | 3.9 KB |
| references/schema-patterns.md | markdown | 3.4 KB |
| references/wrangler-setup.md | markdown | 2.7 KB |
| scripts/check-versions.shexecutable | script | 1017 B |
| templates/basic-queries.ts | script | 7.5 KB |
| templates/basic-schema.ts | script | 2.9 KB |
| templates/client.ts | script | 2.2 KB |
| templates/cloudflare-worker-integration.ts | script | 7.7 KB |
| templates/drizzle.config.ts | script | 2.2 KB |
| templates/migrations/0001_example.sql | other | 2.1 KB |
| templates/package.json | manifest | 1.1 KB |
| templates/prepared-statements.ts | script | 7.3 KB |
| templates/relations-queries.ts | script | 6.8 KB |
| templates/schema.ts | script | 6.5 KB |
| templates/transactions.ts | script | 6.8 KB |
references/migration-workflow.md
View on GitHub# Migration Workflow
Complete guide to database migrations with Drizzle Kit and Wrangler.
---
## Generate vs Push
### `drizzle-kit generate`
- Creates SQL migration files in `./migrations`
- Versioned, trackable in Git
- Can be reviewed before applying
- **Recommended for production**
### `drizzle-kit push`
- Pushes schema directly to database
- No SQL files generated
- Fast for prototyping
- **Not recommended for production**
---
## Complete Workflow
### 1. Make Schema Changes
Edit `src/db/schema.ts`:
```typescript
export const users = sqliteTable('users', {
id: integer('id').primaryKey({ autoIncrement: true }),
email: text('email').notNull().unique(),
name: text('name').notNull(),
// Add new field
role: text('role').notNull().default('user'),
});
```
### 2. Generate Migration
```bash
npx drizzle-kit generate
# or
npm run db:generate
```
Output:
```
Generated migration:
./migrations/0002_add_user_role.sql
```
### 3. Review Generated SQL
Check `./migrations/0002_add_user_role.sql`:
```sql
ALTER TABLE users ADD COLUMN role text DEFAULT 'user' NOT NULL;
```
### 4. Apply to Local Database
```bash
npx wrangler d1 migrations apply my-database --local
# or
npm run db:migrate:local
```
### 5. Test Locally
```bash
npm run dev
# Test your changes
```
### 6. Commit Migration
```bash
git add migrations/0002_add_user_role.sql
git commit -m "Add user role field"
git push
```
### 7. Deploy Code
```bash
npm run deploy
```
### 8. Apply to Production
```bash
npx wrangler d1 migrations apply my-database --remote
# or
npm run db:migrate:remote
```
---
## Best Practices
1. **Always test locally first**
2. **Review generated SQL** before applying
3. **Commit migrations to Git**
4. **Apply migrations in CI/CD** for production
5. **Never skip migrations** - apply in order
6. **Backup production database** before major changes
---
## Troubleshooting
### Migration Fails
```bash
# Delete failed migration
rm migrations/0002_bad_migration.sql
# Regenerate
npx drizzle-kit generate
```
### Need to Rollback
D1 doesn't support automatic rollback. Options:
1. Create a new migration to reverse changes
2. Restore from backup
3. Manually edit data with SQL
---
## Migration Naming
Drizzle auto-generates names like:
- `0001_initial_schema.sql`
- `0002_add_user_role.sql`
- `0003_create_posts_table.sql`
---
## Advanced: Custom Migrations
Sometimes you need custom SQL:
```sql
-- migrations/0004_custom.sql
-- Add data
INSERT INTO users (email, name, role) VALUES
('admin@example.com', 'Admin', 'admin');
-- Update existing data
UPDATE users SET role = 'admin' WHERE email = 'admin@example.com';
-- Create index
CREATE INDEX idx_users_role ON users(role);
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| Agent Cost Reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |
| Agent Cost Reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |