fix(files): a file is added when it is done, not when it was asked for

A download that failed for days and then went through sat days down the library, under its
request date, and read as missing. A job now records when it became done (`ready_at`), stamped
where the expiry already is (`folders.stamp_done`, `finalize_if_running`) and on an insert that is
already done; a check constraint holds every path to done to it. The library sorts and dates its
"Added" by it; the queue keeps the request order.

The migration dates a file from before by its "ready to watch" notice where the inbox still holds
one, else by its request date, where it already sat.
This commit is contained in:
2026-10-12 22:19:37 +02:00
parent 132fb56428
commit 96509fe356
14 changed files with 204 additions and 21 deletions
+52
View File
@@ -0,0 +1,52 @@
"""Migration 0104's data step, DB-lane: a done file from before is dated by when it finished where
its "ready to watch" notice says so, else by when it was asked for; a job not done has no date."""
from alembic import command
from alembic.config import Config
from sqlalchemy import text
from app.db import engine
def test_a_done_file_takes_its_notice_time_else_its_request_time(db, make_user):
uid = make_user().id
cfg = Config("alembic.ini")
command.downgrade(cfg, "0103_files_sha256")
try:
with engine.begin() as conn:
def job(status, created):
return conn.execute(
text(
"INSERT INTO download_jobs (user_id, source_kind, source_ref,"
" profile_snapshot, status, created_at) VALUES (:u, 'url', 'x', '{}', :s,"
" CAST(:c AS timestamptz)) RETURNING id"
),
{"u": uid, "s": status, "c": created},
).scalar_one()
retried = job("done", "2026-10-01T10:00:00+00:00")
quiet = job("done", "2026-10-02T10:00:00+00:00")
failed = job("error", "2026-10-03T10:00:00+00:00")
for at in ("2026-10-05T09:00:00+00:00", "2026-10-06T09:00:00+00:00"):
conn.execute(
text(
"INSERT INTO notifications (user_id, type, title, data, created_at) VALUES"
" (:u, 'download_done', 't', CAST(:d AS json), CAST(:c AS timestamptz))"
),
{"u": uid, "d": f'{{"job_id": {retried}}}', "c": at},
)
finally:
command.upgrade(cfg, "head")
with engine.connect() as conn:
rows = dict(
conn.execute(
text(
"SELECT id, to_char(ready_at AT TIME ZONE 'UTC', 'MM-DD HH24') FROM download_jobs"
)
).all()
)
assert rows[retried] == "10-06 09" # its last finish
assert rows[quiet] == "10-02 10" # no notice left: where it already sat
assert rows[failed] is None