# Portable Stage 1 — Separating user data from application code ### Status DONE ### What was done Persistent user data is now resolved from a dedicated `data/` tree instead of living inside `app/`. A safe, idempotent migration copies the legacy database, uploads and storage on first start. Sources are never deleted. POST-E2E/BUG-UI working-tree changes are untouched. ### Directory scheme ``` / app/ # application code, UI templates, templates_docs (package/update) data/ # persistent user data (never replaced by updates) work_tracker.db # SQLite uploads/ # Excel imports, project_import/*.wvt.zip storage/ # document artifacts + Daily Report photos backups/ # pre-migrate snapshots and future manual backups ``` Environment overrides: | Variable | Meaning | Default | |---|---|---| | `WORK_TRACKER_DATA_DIR` | data root | `/data` | | `DOCUMENT_STORAGE_ROOT` | storage root (historical name) | `/storage` | | `WORK_TRACKER_BACKUPS_DIR` | backups root | `/backups` | Legacy mapping: | Old | New | |---|---| | `app/database/work_tracker.db` (+wal/shm) | `data/work_tracker.db` | | `app/uploads/` | `data/uploads/` | | `storage/` | `data/storage/` | ### Changed files | File | Change | |---|---| | `app/config.py` | centralized paths, `ensure_data_layout()`, `migrate_legacy_data()`, backup helper | | `app/database/database.py` | `DATABASE_PATH` from `config.database_path()`; layout+migration before engine | | `app/routers/boq.py` | `UPLOAD_DIR = uploads_dir()` only (POST-E2E hunks preserved) | | `app/routers/projects.py` | `IMPORT_UPLOAD_DIR = import_uploads_dir()` only (POST-E2E hunks preserved) | | `tests/test_data_layout_migration.py` | new — 13 regression tests | | `docs/implementation/REQ-portable-data-separation.md` | this report | Not changed: models, migrations, KS-2/KS-3/KS-6a business logic, export/import internals (they already call `document_storage_root()`), photo service, POST-E2E/BUG-UI edits. ### Migration algorithm `migrate_legacy_data()` runs at `database.py` import (i.e. every app start / test import) and is **idempotent**: 1. `ensure_data_layout()` creates `data/`, `data/uploads/`, `data/uploads/project_import/`, `data/storage/`, `backups/`. 2. **Database** - If `data/work_tracker.db` exists → skip (already migrated or fresh install). - Else if `app/database/work_tracker.db` exists: 1. Snapshot `work_tracker.db` (+`-wal`, `-shm`) into `backups/pre-migrate-/`. 2. Copy DB and sidecars into `data/` (never overwrite an existing file). 3. `PRAGMA integrity_check` on the copy; on failure delete **only the copy** and report an error — source remains. 3. **Uploads** — copy `app/uploads/**` → `data/uploads/**`, skipping existing files. 4. **Storage** — copy `storage/**` → configured `document_storage_root()` (`data/storage/` by default), skipping existing files. 5. Return `MigrationReport` (counts, backup path, notes, errors). Guarantees: - sources never deleted or overwritten; - destination files never overwritten; - WAL/SHM sidecars handled together with the main DB file; - failed integrity check rolls back only the new copy; - second run is a no-op. ### Backup / restore - **Automatic:** `backups/pre-migrate-/` before any legacy DB copy (contains `work_tracker.db` and any `-wal`/`-shm`). - **Manual restore of DB:** stop the app, copy `backups/pre-migrate-*/work_tracker.db*` (or a zip of `data/`) back over `data/work_tracker.db*`. - **Full backup set:** `data/work_tracker.db` + `data/work_tracker.db-wal` + `data/work_tracker.db-shm` + `data/storage/` + `data/uploads/`. - Future portable installer should add a `Backup.bat`/UI step around the same `data/` tree (`backups/`). ### Data migration result (this machine) | Item | Result | |---|---| | `data/work_tracker.db` | created, 770 048 bytes, `integrity_check` ok | | Legacy `app/database/work_tracker.db` | **preserved**, 770 048 bytes | | `data/uploads/` | 9 files copied | | `data/storage/` | files copied under `projects/` | | `backups/pre-migrate-20261016T104850Z/` | snapshot of legacy DB | ### Tests **`tests/test_data_layout_migration.py` — 13 passed:** 1. `test_default_paths_are_separated` — data paths ≠ `app/` 2. `test_document_storage_root_env_override` 3. `test_migrate_copies_db_uploads_storage` 4. `test_migrate_handles_wal_shm` 5. `test_migrate_creates_backup_snapshot` 6. `test_migrate_is_idempotent` 7. `test_migrate_preserves_sources` 8. `test_migrate_does_not_overwrite_destination` 9. `test_migrate_skips_existing_upload_files` 10. `test_migrate_fresh_data_dir_without_legacy` 11. `test_migrate_integrity_failure_leaves_source_and_removes_copy` 12. `test_database_module_uses_data_root` 13. `test_legacy_app_upload_dir_not_used_for_new_writes` **Profile suite:** data layout + KS-2 + KS-3 + photos + export/import artifacts + database reset + project export/import — **117 passed**. **Full `pytest -q`:** **755 passed, 5 failed**: | Failure | Cause | Related to this task? | |---|---|---| | `test_post_e2e_three_followups` ×3 | KS-2 form redesigned to quantity-only (REQ-KS2); POST-E2E Block A still expects `include-all` checkboxes | **no** — pre-existing from previous accepted task; POST-E2E file left untouched | | `test_project_dashboard` | copies real user DB, expects project id=2 | **no** — pre-existing | | `test_project_workflow_regressions` | same real-DB fixture | **no** — pre-existing | No new failures introduced by the data-path change. Export/import roundtrips, photo storage and database reset (which uses `document_storage_root()`) are green. ### Compatibility - `python -m app.main` / `uvicorn app.main:app` — works; engine opens `data/work_tracker.db` after migration. - `DOCUMENT_STORAGE_ROOT` tests keep working (override wins over default). - `DATABASE_PATH` symbol in `app.database.database` still exists (tests that copy the real DB keep working). - Project export/import of artifacts and Daily Report photos uses `document_storage_root()` and is covered by `test_doc_b7_*` / photo tests. - Old launch paths (`app/database/work_tracker.db`, `app/uploads/`, `storage/`) remain as **read-only sources**; the app no longer writes there. ### Risks / follow-ups 1. **Dual copies** — legacy `app/database/work_tracker.db`, `app/uploads/`, `storage/` still exist. After a backup, they can be removed in a later approved cleanup. Until then they are historical snapshots. 2. **`data/storage` may gain files from tests** that do not pin `DOCUMENT_STORAGE_ROOT` — same behaviour as the old default `storage/`. 3. **Concurrent first start** — two processes importing `database.py` at the same instant could race the copy; `copy_file_safe` refuses overwrites so one wins, but a lock file could be added later. 4. **POST-E2E test expectations** for the old KS-2 checkbox UI are still red — separate task. 5. **Portable package** (installer, Start/Stop bats, Backup UI) — next stage, `docs/tasks/REQ-windows-installer.md`. ### git status (preserve check) Uncommitted POST-E2E/BUG-UI still present and unmodified in substance: - `M` `app/exporters/package_service.py`, `project_export.py`, `excel_importer.py`, `projects.py` (router), `detail.html`, `import_preview.html`, `test_excel_importer.py`, `test_task019_*`, `test_task020_*` - `M` `app/routers/boq.py`, `app/routers/projects.py` — **path lines only added**; POST-E2E import-adjustments and `ks6a_xlsx` hunks intact - untracked `test_post_e2e_three_followups.py`, `test_bug_export_missing_ks2_xlsx.py`, QA dirs, docs Staging remains empty. No commit / push.