Separate persistent user data from application code (data/work_tracker.db, data/uploads, data/storage) with idempotent legacy migration and SQLite integrity checks. Add offline packaging scripts for a Windows x11 x64 portable ZIP (embeddable Python, Start/Stop/Backup.bat). Fix launcher: no multiline python -c in BAT and no os.dup2 — uvicorn runs detached with wvt.log; health wait and busy-port diagnostics included.
7.6 KiB
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
<project_root>/
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 | <project_root>/data |
DOCUMENT_STORAGE_ROOT |
storage root (historical name) | <data_root>/storage |
WORK_TRACKER_BACKUPS_DIR |
backups root | <project_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:
ensure_data_layout()createsdata/,data/uploads/,data/uploads/project_import/,data/storage/,backups/.- Database
- If
data/work_tracker.dbexists → skip (already migrated or fresh install). - Else if
app/database/work_tracker.dbexists:- Snapshot
work_tracker.db(+-wal,-shm) intobackups/pre-migrate-<UTC>/. - Copy DB and sidecars into
data/(never overwrite an existing file). PRAGMA integrity_checkon the copy; on failure delete only the copy and report an error — source remains.
- Snapshot
- If
- Uploads — copy
app/uploads/**→data/uploads/**, skipping existing files. - Storage — copy
storage/**→ configureddocument_storage_root()(data/storage/by default), skipping existing files. - 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-<timestamp>/before any legacy DB copy (containswork_tracker.dband any-wal/-shm). - Manual restore of DB: stop the app, copy
backups/pre-migrate-*/work_tracker.db*(or a zip ofdata/) back overdata/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 samedata/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:
test_default_paths_are_separated— data paths ≠app/test_document_storage_root_env_overridetest_migrate_copies_db_uploads_storagetest_migrate_handles_wal_shmtest_migrate_creates_backup_snapshottest_migrate_is_idempotenttest_migrate_preserves_sourcestest_migrate_does_not_overwrite_destinationtest_migrate_skips_existing_upload_filestest_migrate_fresh_data_dir_without_legacytest_migrate_integrity_failure_leaves_source_and_removes_copytest_database_module_uses_data_roottest_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 opensdata/work_tracker.dbafter migration.DOCUMENT_STORAGE_ROOTtests keep working (override wins over default).DATABASE_PATHsymbol inapp.database.databasestill 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 bytest_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
- 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. data/storagemay gain files from tests that do not pinDOCUMENT_STORAGE_ROOT— same behaviour as the old defaultstorage/.- Concurrent first start — two processes importing
database.pyat the same instant could race the copy;copy_file_saferefuses overwrites so one wins, but a lock file could be added later. - POST-E2E test expectations for the old KS-2 checkbox UI are still red — separate task.
- 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:
Mapp/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_*Mapp/routers/boq.py,app/routers/projects.py— path lines only added; POST-E2E import-adjustments andks6a_xlsxhunks intact- untracked
test_post_e2e_three_followups.py,test_bug_export_missing_ks2_xlsx.py, QA dirs, docs
Staging remains empty. No commit / push.