# V1 Block 20 — SQLite Persistence Adapter Package

**Purpose**: Implement Drift-based SQLite storage backend.

> **Note for Codex**: Follow tasks exactly; avoid exploratory calls when tasks specify names/files/tests.

## Chunk 20.1 — SQLite schema & Drift setup
Recommended level: **XHIGH**

Status: Complete on 2026-06-26.

### Tasks
1. Create new Dart package **`packages/scheduler_persistence_sqlite`** with Drift as dependency.
2. Define Drift database `SchedulerDb` with tables: `tasks`, `projects`, `locked_blocks`, `settings`, `snapshots`.
3. Fields: follow domain model; include `revision INT`, `owner_id TEXT`.
4. Add drift migrations using drift_dev build runner; start at schemaVersion=1.
5. Generate DAO classes via build_runner.

### Acceptance criteria
1. `dart run build_runner build --delete-conflicting-outputs` completes with zero errors.
2. Generated drift files checked in.

### Completed implementation
1. Added package `packages/scheduler_persistence_sqlite` with Drift, `drift_dev`, and `build_runner` wiring.
2. Added `SchedulerDb` in `lib/src/scheduler_db.dart` with `schemaVersion = 1` and `MigrationStrategy(onCreate: createAll)`.
3. Added tables for `tasks`, `projects`, `locked_blocks`, `locked_overrides`, `settings`, and `snapshots`.
4. Included owner scope and optimistic revision columns on mutable rows.
5. Added generated Drift data classes/accessor mixins in `lib/src/scheduler_db.g.dart`.
6. Added a schema smoke test that opens an in-memory Drift database and checks the expected table names.

### Verification
1. `dart pub get`: passed.
2. `cd packages/scheduler_persistence_sqlite && dart run build_runner build --delete-conflicting-outputs`: passed, exit 0; current build_runner warned that the option is ignored.
3. `dart format packages/scheduler_persistence_sqlite`: passed.
4. `cd packages/scheduler_persistence_sqlite && dart analyze`: passed, no issues found.
5. `dart analyze`: passed, no issues found.
6. `cd packages/scheduler_persistence_sqlite && dart test`: passed, 1 test.
7. `dart test`: passed, 308 tests.

---

## Chunk 20.2 — SQLite adapter implementation & tests
Recommended level: **HIGH**

Status: Complete on 2026-06-26.

### Tasks
1. Implement `SqliteTaskRepository` etc mapping domain objects to Drift rows with explicit converters.
2. Use transaction for multi‑record save; ensure optimistic revision by `WHERE revision = ?` clause.
3. Add repository conformance tests reused from block19 suite pointed at SQLite adapter (use `sqflite_ffi` in tests).

### Acceptance criteria
1. SQLite adapter passes full conformance suite with `sqflite_ffi` on Linux/Windows/mac.
2. Average write latency in test ≤50ms.

### Completed implementation
1. Added `SqliteTaskRepository`, `SqliteProjectRepository`, `SqliteLockedBlockRepository`, `SqliteSettingsRepository`, and `SqliteScheduleSnapshotRepository`.
2. Mapped Drift rows to domain objects through the existing core document mappers and explicit row/JSON converters.
3. Implemented duplicate-id checks, owner isolation, deterministic paging, archive behavior, snapshot retention deletion, and typed repository failures.
4. Implemented optimistic compare-and-set writes/deletes/archives with `WHERE revision = expected` clauses.
5. Added SQLite conformance tests using the Block 19 shared suite and an in-memory Drift SQLite executor.
6. Added a write-latency test for task inserts and wired SQLite tests into the root test wrapper.

### Notes
1. Tests use Drift `NativeDatabase.memory()` instead of `sqflite_ffi`. A sqflite-backed Drift executor would require `drift_sqflite`, which pulls Flutter SDK dependencies into this pure Dart adapter package.
2. The current repository contracts expose single-record writes. No multi-record repository save method exists yet; future multi-record SQLite writes should use `SchedulerDb.transaction`.

### Verification
1. `dart pub get`: passed.
2. `dart format test/scheduler_core_test.dart packages/scheduler_persistence_sqlite`: passed.
3. `cd packages/scheduler_persistence_sqlite && dart analyze`: passed, no issues found.
4. `cd packages/scheduler_persistence_sqlite && dart test`: passed, 9 tests.
5. `dart analyze`: passed, no issues found.
6. `dart test`: passed, 316 tests.
7. `scripts/test.sh`: not present or not executable.

---
