# TT-002 — Drift Database Schema **Type:** Story **Priority:** Highest **Labels:** database, infrastructure **Depends on:** TT-001 **Blocks:** TT-004, TT-005 --- ## Summary Implement the full Drift database schema in `lib/core/database/app_database.dart` including all table definitions, DAOs, and the database connection provider. ## Background The Drift schema is documented in `.ai/database.md`. The placeholder file `lib/core/database/app_database.dart` exists but contains no implementation. ## Data Model ### Tables | Table | Key columns | |-------------------|----------------------------------------------------------| | `projects` | id, name, color_value, description, archived_at, created_at | | `time_entries` | id, project_id (FK), start_time, end_time, duration_s, note, created_at | | `tags` | id, name | | `time_entry_tags` | time_entry_id (FK), tag_id (FK) — composite PK | ## Acceptance Criteria - [ ] All 4 tables defined as Drift `Table` classes - [ ] Foreign key constraints declared and enforced - [ ] `AppDatabase` annotated with `@DriftDatabase(tables: [...])` - [ ] `_openConnection()` uses `getApplicationDocumentsDirectory()` + `NativeDatabase.createInBackground` - [ ] `schemaVersion` = 1, empty `MigrationStrategy` scaffold in place - [ ] Three DAOs created: `ProjectsDao`, `TimeEntriesDao`, `TagsDao` - [ ] Each DAO has at minimum: `watchAll()`, `insert()`, `update()`, `delete()` - [ ] `TimeEntriesDao.getActiveEntry()` returns entry where `end_time IS NULL` - [ ] Riverpod `@Riverpod(keepAlive: true)` provider for `AppDatabase` - [ ] `dart run build_runner build` generates schema without errors - [ ] Unit test: insert + read a `Project` using in-memory DB ## Files to create / modify - `lib/core/database/app_database.dart` — full implementation - `lib/core/database/daos/projects_dao.dart` - `lib/core/database/daos/time_entries_dao.dart` - `lib/core/database/daos/tags_dao.dart` - `test/core/database/app_database_test.dart` ## Notes - See `.ai/database.md` for full schema details - Use `NativeDatabase.memory()` in tests — never the real file DB