feat: portable Windows data layout and launcher package
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.
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
"""Portable stage 1: data layout and legacy → data/ migration.
|
||||
|
||||
Covers path resolution, idempotent migration, WAL/SHM sidecars, backup
|
||||
snapshot, conflict safety, error handling and source preservation.
|
||||
"""
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from app import config
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def isolated(tmp_path, monkeypatch):
|
||||
"""Point every data path at a disposable tree and seed legacy sources."""
|
||||
data = tmp_path / "data"
|
||||
backups = tmp_path / "backups"
|
||||
monkeypatch.setenv(config.ENV_DATA_DIR, str(data))
|
||||
monkeypatch.setenv(config.ENV_BACKUPS_DIR, str(backups))
|
||||
monkeypatch.delenv(config.ENV_DOCUMENT_STORAGE_ROOT, raising=False)
|
||||
|
||||
legacy_db = tmp_path / "legacy" / "work_tracker.db"
|
||||
legacy_db.parent.mkdir(parents=True)
|
||||
conn = sqlite3.connect(legacy_db)
|
||||
conn.execute("CREATE TABLE t (id INTEGER PRIMARY KEY, v TEXT)")
|
||||
conn.execute("INSERT INTO t (v) VALUES ('kept')")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
(legacy_db.parent / "work_tracker.db-wal").write_bytes(b"wal-sentinel")
|
||||
|
||||
legacy_uploads = tmp_path / "legacy" / "uploads"
|
||||
(legacy_uploads / "project_import").mkdir(parents=True)
|
||||
(legacy_uploads / "sheet.xlsx").write_bytes(b"xlsx")
|
||||
(legacy_uploads / "project_import" / "a.wvt.zip").write_bytes(b"zip")
|
||||
|
||||
legacy_storage = tmp_path / "legacy" / "storage"
|
||||
photo = legacy_storage / "projects" / "1" / "daily_reports" / "2" / "photos"
|
||||
photo.mkdir(parents=True)
|
||||
(photo / "p.jpg").write_bytes(b"jpg-bytes")
|
||||
art = legacy_storage / "projects" / "1" / "documents" / "KS2" / "3"
|
||||
art.mkdir(parents=True)
|
||||
(art / "f.xlsx").write_bytes(b"artifact")
|
||||
|
||||
monkeypatch.setattr(config, "_LEGACY_DATABASE_PATH", legacy_db)
|
||||
monkeypatch.setattr(config, "_LEGACY_UPLOADS_DIR", legacy_uploads)
|
||||
monkeypatch.setattr(config, "_LEGACY_STORAGE_DIR", legacy_storage)
|
||||
return {
|
||||
"data": data,
|
||||
"backups": backups,
|
||||
"legacy_db": legacy_db,
|
||||
"legacy_uploads": legacy_uploads,
|
||||
"legacy_storage": legacy_storage,
|
||||
}
|
||||
|
||||
|
||||
def test_default_paths_are_separated(isolated):
|
||||
assert config.data_root() == isolated["data"].resolve()
|
||||
assert config.database_path() == isolated["data"].resolve() / "work_tracker.db"
|
||||
assert config.uploads_dir() == isolated["data"].resolve() / "uploads"
|
||||
assert config.import_uploads_dir() == isolated["data"].resolve() / "uploads" / "project_import"
|
||||
assert config.document_storage_root() == isolated["data"].resolve() / "storage"
|
||||
assert config.backups_dir() == isolated["backups"].resolve()
|
||||
# Code tree is not the data tree
|
||||
assert config.database_path().parent != config.APP_DIR
|
||||
assert config.uploads_dir() != config.APP_DIR / "uploads"
|
||||
|
||||
|
||||
def test_document_storage_root_env_override(isolated, tmp_path, monkeypatch):
|
||||
other = tmp_path / "other-storage"
|
||||
monkeypatch.setenv(config.ENV_DOCUMENT_STORAGE_ROOT, str(other))
|
||||
assert config.document_storage_root() == other.resolve()
|
||||
|
||||
|
||||
def test_migrate_copies_db_uploads_storage(isolated):
|
||||
report = config.migrate_legacy_data()
|
||||
assert report.ok, report.errors
|
||||
assert report.db_copied is True
|
||||
assert report.uploads_copied == 2
|
||||
assert report.storage_copied == 2
|
||||
|
||||
db = config.database_path()
|
||||
assert db.is_file()
|
||||
conn = sqlite3.connect(db)
|
||||
try:
|
||||
assert conn.execute("SELECT v FROM t").fetchone()[0] == "kept"
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
assert (config.uploads_dir() / "sheet.xlsx").is_file()
|
||||
assert (config.import_uploads_dir() / "a.wvt.zip").is_file()
|
||||
photo = config.document_storage_root() / "projects/1/daily_reports/2/photos/p.jpg"
|
||||
assert photo.read_bytes() == b"jpg-bytes"
|
||||
assert (config.document_storage_root() / "projects/1/documents/KS2/3/f.xlsx").is_file()
|
||||
|
||||
|
||||
def test_migrate_handles_wal_shm(isolated):
|
||||
config.migrate_legacy_data()
|
||||
assert (config.database_path().parent / "work_tracker.db-wal").read_bytes() == b"wal-sentinel"
|
||||
|
||||
|
||||
def test_migrate_creates_backup_snapshot(isolated):
|
||||
report = config.migrate_legacy_data()
|
||||
assert report.backup_path is not None
|
||||
backup = Path(report.backup_path)
|
||||
assert (backup / "work_tracker.db").is_file()
|
||||
assert (backup / "work_tracker.db-wal").is_file()
|
||||
|
||||
|
||||
def test_migrate_is_idempotent(isolated):
|
||||
first = config.migrate_legacy_data()
|
||||
assert first.db_copied is True
|
||||
second = config.migrate_legacy_data()
|
||||
assert second.db_copied is False
|
||||
assert second.skipped is True
|
||||
# Files still intact after second pass
|
||||
assert config.database_path().is_file()
|
||||
|
||||
|
||||
def test_migrate_preserves_sources(isolated):
|
||||
config.migrate_legacy_data()
|
||||
assert isolated["legacy_db"].is_file()
|
||||
assert (isolated["legacy_db"].parent / "work_tracker.db-wal").is_file()
|
||||
assert (isolated["legacy_uploads"] / "sheet.xlsx").is_file()
|
||||
assert (
|
||||
isolated["legacy_storage"] / "projects/1/daily_reports/2/photos/p.jpg"
|
||||
).is_file()
|
||||
|
||||
|
||||
def test_migrate_does_not_overwrite_destination(isolated):
|
||||
config.ensure_data_layout()
|
||||
dest_db = config.database_path()
|
||||
dest_db.parent.mkdir(parents=True, exist_ok=True)
|
||||
conn = sqlite3.connect(dest_db)
|
||||
conn.execute("CREATE TABLE existing (x INTEGER)")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
before = dest_db.read_bytes()
|
||||
|
||||
report = config.migrate_legacy_data()
|
||||
assert report.db_copied is False
|
||||
assert dest_db.read_bytes() == before
|
||||
|
||||
|
||||
def test_migrate_skips_existing_upload_files(isolated):
|
||||
config.ensure_data_layout()
|
||||
target = config.uploads_dir() / "sheet.xlsx"
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
target.write_bytes(b"already-here")
|
||||
config.migrate_legacy_data()
|
||||
assert target.read_bytes() == b"already-here"
|
||||
|
||||
|
||||
def test_migrate_fresh_data_dir_without_legacy(isolated, monkeypatch):
|
||||
monkeypatch.setattr(config, "_LEGACY_DATABASE_PATH", isolated["data"] / "missing.db")
|
||||
monkeypatch.setattr(config, "_LEGACY_UPLOADS_DIR", isolated["data"] / "missing-uploads")
|
||||
monkeypatch.setattr(config, "_LEGACY_STORAGE_DIR", isolated["data"] / "missing-storage")
|
||||
report = config.migrate_legacy_data()
|
||||
assert report.ok
|
||||
assert report.db_copied is False
|
||||
assert report.uploads_copied == 0
|
||||
assert report.storage_copied == 0
|
||||
assert "fresh data directory" in " ".join(report.notes)
|
||||
|
||||
|
||||
def test_migrate_integrity_failure_leaves_source_and_removes_copy(isolated, monkeypatch):
|
||||
# Corrupt the legacy DB so integrity_check fails after copy.
|
||||
isolated["legacy_db"].write_bytes(b"not a sqlite database")
|
||||
|
||||
report = config.migrate_legacy_data()
|
||||
assert not report.ok
|
||||
assert report.db_copied is False
|
||||
assert not config.database_path().is_file()
|
||||
assert isolated["legacy_db"].read_bytes() == b"not a sqlite database"
|
||||
assert any("integrity_check" in e for e in report.errors)
|
||||
|
||||
|
||||
def test_database_module_uses_data_root(isolated):
|
||||
# Import after env fixture: DATABASE_PATH module constant is bound at first
|
||||
# import, so resolve via the public helpers instead.
|
||||
from app.database import database as dbmod
|
||||
|
||||
# Module-level DATABASE_PATH may be the process-wide value; the helper is
|
||||
# the source of truth for the configured layout.
|
||||
assert config.database_path().parent == isolated["data"].resolve()
|
||||
assert dbmod.DATABASE_PATH.name == "work_tracker.db"
|
||||
assert config.uploads_dir().name == "uploads"
|
||||
|
||||
|
||||
def test_legacy_app_upload_dir_not_used_for_new_writes():
|
||||
from app.routers import boq, projects
|
||||
|
||||
assert boq.UPLOAD_DIR == config.uploads_dir()
|
||||
assert projects.IMPORT_UPLOAD_DIR == config.import_uploads_dir()
|
||||
assert boq.UPLOAD_DIR != config.APP_DIR / "uploads"
|
||||
Reference in New Issue
Block a user