Skip to content

[Production readiness] Add Drift indexes and linear preparation chain reconstruction #541

Description

@jjoonleo

Problem

Preparation and schedule Drift queries rely on non-primary-key predicates without explicit table indexes, and preparation linked-list reconstruction is quadratic in the number of steps. This is small today, but it adds avoidable latency risk as local schedule/preparation data grows.

Related: #392. Any index/schema change should be coordinated with the Drift migration work there so existing installs get tested upgrade paths.

Evidence

  • lib/data/tables/preparation_schedule_table.dart and lib/data/tables/preparation_user_table.dart define only primary keys; scheduleId, userId, and nextPreparationId are queried but not explicitly indexed.
  • PreparationScheduleDao.getPreparationSchedulesByScheduleId filters by scheduleId, then reconstructs order with nested scans.
  • PreparationUserDao.getPreparationUsersByUserId repeats the same pattern for userId.
  • Delete paths update by nextPreparationId, another unindexed predicate.
  • ScheduleDao.getSchedulesByDate filters by scheduleTime and joins through placeId; Schedules also defines only the primary key.
  • PreparationEntity.ordered already shows a linear map/set-based reconstruction approach that the DAOs can mirror or reuse.

Proposed direction

Add Drift indexes for the DAO access patterns:

  • preparation_schedules(schedule_id)
  • preparation_schedules(next_preparation_id)
  • preparation_users(user_id)
  • preparation_users(next_preparation_id)
  • schedules(schedule_time)
  • consider schedules(place_id) if join/query plans show it helps

Refactor DAO linked-list reconstruction to build stepById and referencedIds once, then walk the chain in O(n), with cycle/broken-chain protection consistent with existing preparation-chain validation docs.

Acceptance criteria

  • Drift schema declares and generates the needed indexes.
  • Migration coverage includes adding indexes for existing databases, aligned with [Production readiness] Complete and test Drift schema migrations #392.
  • Preparation schedule/user reconstruction is O(n), not O(n^2).
  • DAO tests cover multi-step ordering, missing/broken links, and cycle-safe behavior.
  • Tests verify schedule/preparation DAO behavior still passes after regenerated Drift files.

Related: #392

Source: Codex codebase audit on 2026-06-28.

Metadata

Metadata

Assignees

No one assigned

    Labels

    preparationrelated to preparation featurepriority: P2Medium priority production-readiness workproduction-readinessWork required before production releaserefactorschedulerelated to schedule feature

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions