D1 Database Migrations
Project Database
- Database name:
agentic-commerce-p2p-db - Location:
migrations/directory
List Existing Migrations
ls -la migrations/
Apply Migrations (Remote)
npx wrangler d1 execute agentic-commerce-p2p-db --remote --file=migrations/YOUR_MIGRATION.sql
Apply All Pending Migrations
npx wrangler d1 migrations apply agentic-commerce-p2p-db --remote
Check Current Schema
npx wrangler d1 execute agentic-commerce-p2p-db --remote --command="SELECT name FROM sqlite_master WHERE type='table';"
Creating New Migrations
- Create file:
migrations/NNN_description.sql - Use sequential numbering (e.g., 019, 020)
- Include both up and down if possible
- Test locally first with
--localflag
Local Testing
npx wrangler d1 execute agentic-commerce-p2p-db --local --file=migrations/YOUR_MIGRATION.sql
Rollback
D1 doesn't have built-in rollback. Create a reverse migration manually.
Best Practices
- Always backup before major schema changes
- Test migrations locally first
- Use transactions where possible
- Document breaking changes