Databases with AI: Set Up, Migrate, Store
Intermediate · 13 missions · about 4 hours
AI coding agents can create a database, write migrations and deploy in minutes, and they can wipe production just as fast. Learn where data should live (D1, KV, R2, Durable Objects or hosted Postgres), how to brief an agent to set up, migrate, seed and verify a real Cloudflare D1 database, and how to catch the schema and migration mistakes AI makes before they cost you data. You finish by shipping a live Worker + D1 app with migrations applied in production.
Missions
- Where your data can live: SQL, key-value, files and live objects: what each is good and bad at.
- Prompt: get a schema designed, not guessed: Brief an AI to design tables, keys, rules and indexes, and to say what not to build.
- Review the AI's schema before it runs: Fix the missing keys, rules and indexes in an agent-written migration.
- Plan the migration run: order and danger check: Sort pending migrations the way Wrangler does and flag destructive SQL before it runs.
- D1 migrations: local, remote, and the gap between: The real Wrangler workflow, and why "works locally, fails in production" happens.
- Seed data without string-built SQL: Turn AI-generated JSON rows into safe, parameterized INSERT batches.
- Fix the AI's migration before it eats your data: A dropped column, a lost cent, a surprise rename and a NOT NULL that can never apply.
- Safe uploads to R2: never trust the filename: Check the size and the real file type, and let the server choose the object key.
- chooseStorage(): turn the decision into code: Write down where data should live as rules your agent can follow, with tests.
- Prompt: brief an agent to set up D1 end to end: Steps, proof, and the things it must never do without you.
- Backups, exports, recovery and least privilege: Time Travel, exports, test restores, and who (or what) gets which keys.
- A database health check that tells the truth: Ping D1, check tables and migrations, time out fast, and leak nothing.
- Boss: ship a Worker + D1 app built with an agent: Two migrations applied remotely, a JSON API and a health check, live on the internet.
Part of: Indie SaaS Founder career path, Backend & Cloud Engineer career path