feat: Add task state migration from MongoDB to PostgreSQL #128
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Implements a phased migration strategy for task state storage from MongoDB to PostgreSQL with zero-downtime rollout capability.
Key changes:
TASK_STATE_STORAGE_PHASEfeature flag to control migration phasestask_statesPostgreSQL table with JSONB state columnMigration Phases
mongodbdual_writedual_readpostgresFiles Changed
Core Implementation
src/adapters/orm.py- AddedTaskStateORMmodelsrc/config/environment_variables.py- AddedTASK_STATE_STORAGE_PHASEenv varsrc/domain/repositories/task_state_postgres_repository.py- PostgreSQL repository (new)src/domain/repositories/task_state_dual_repository.py- Dual-write wrapper (new)src/domain/use_cases/states_use_case.py- Updated to use dual repositoryDatabase Migration
database/migrations/alembic/versions/2026_01_12_0000_add_task_states_table_*.py- Alembic migrationScripts
scripts/backfill_task_states.py- Backfill existing MongoDB data to PostgreSQLscripts/verify_task_states.py- Verify data consistency between databasesTests
tests/unit/repositories/test_task_state_postgres_repository.py- 2 teststests/unit/repositories/test_task_state_dual_repository.py- 35 testsMetrics (dual_read phase)
task_state.dual_read.matchtask_state.dual_read.mismatch.missing_postgrestask_state.dual_read.mismatch.missing_mongodbtask_state.dual_read.mismatch.state_contenttask_state.dual_read.list_count_mismatchRollout Plan
TASK_STATE_STORAGE_PHASE=mongodb(no behavior change)python scripts/backfill_task_states.pyTASK_STATE_STORAGE_PHASE=dual_writeTASK_STATE_STORAGE_PHASE=dual_read, monitor metricsTASK_STATE_STORAGE_PHASE=postgresRollback
Set
TASK_STATE_STORAGE_PHASEback to the previous phase at any time.Test plan