Overview
Drizzle provides a type-safe way to define database schemas and generates SQL migration files automatically. Key Files:src/db/schema.ts- Schema definitions (source of truth)drizzle.config.ts- Drizzle configurationsrc/db/migrate.ts- Migration runnerdrizzle/- Generated migration SQL filessprout.db- SQLite database file (created after first migration)
Initial Setup
After installing dependencies, initialize the database:Run Migrations
Apply the database schema:Expected output:This creates
sprout.db in the backend directory.The default user is automatically seeded when you start the backend with
npm run dev.Database Schema
The schema is defined insrc/db/schema.ts using Drizzle’s SQLite dialect.
Core Tables
- users
- branches
- nodes
- node_edges
Single-user application with a default user auto-seeded on startup.
Additional Tables
| Table | Purpose |
|---|---|
topic_documents | S3-stored documents uploaded for topics |
node_contents | Explanations, visualizations, and learning cards |
assessments | Diagnostic, quiz, and recall assessments |
questions | MCQ and open-ended questions |
answers | Student answers with scores and feedback |
user_node_progress | Mastery scores and completion tracking |
chat_sessions | Tutoring chat sessions |
chat_messages | Chat history with roles and kinds |
hint_events | Hint requests and responses |
See
src/db/schema.ts:1 for the complete schema with indexes and constraints.Migration Commands
Drizzle provides three main commands for managing migrations:- db:migrate
- db:generate
- db:push
Apply MigrationsRuns all pending migrations from the
drizzle/ folder.When to use:- First time setup
- After pulling schema changes from Git
- After generating new migrations
src/db/migrate.ts):Migration Workflow
Recommended workflow for schema changes:Existing Migrations
Sprout ships with four migrations:Drizzle auto-generates migration names. The meta folder contains schema snapshots for diffing.
Database Location
The database file location is configured via environment variable:.env
drizzle.config.ts):
Inspecting the Database
Use SQLite tools to inspect the database:Common Operations
Reset database
Reset database
Delete the database file and re-run migrations:
Rollback migration
Rollback migration
Drizzle doesn’t support automatic rollbacks. Manually revert:
- Delete the migration file from
drizzle/ - Edit
drizzle/meta/_journal.jsonto remove the entry - Run the SQL
DOWNmigration manually (you need to write it)
Seed test data
Seed test data
The default user is auto-seeded on backend startup (For additional test data, add a seed script or use Drizzle Studio.
src/index.ts:17):Backup database
Backup database
SQLite databases are single files - just copy:
Troubleshooting
Migration fails: table already exists
Migration fails: table already exists
The database schema is out of sync with migrations.Solution:
- Backup your data
- Reset the database:
rm sprout.db - Re-run migrations:
npm run db:migrate
Drizzle can't find schema
Drizzle can't find schema
Error:
Cannot find module './src/db/schema.ts'Solution: Check drizzle.config.ts schema path matches your directory structure.Database is locked
Database is locked
Error:
SQLITE_BUSY: database is lockedSolution:- Close any SQLite browser tools
- Ensure only one backend instance is running
- Check for abandoned connections
Generated migration looks wrong
Generated migration looks wrong
Drizzle generated unexpected SQL.Solution:
- Review the generated SQL in
drizzle/XXXX_*.sql - If incorrect, delete the migration and fix your schema
- Re-generate with
npm run db:generate - Manually edit the SQL if needed (not recommended)
Next Steps
Schema Reference
Learn more about Drizzle schema syntax
Document Uploads
Configure S3 for document storage