fix(scan): don't write NULL over NOT NULL columns when reconciling files

A scanned row leaves unset columns as None and the model defaults only
apply on insert, so the reconciliation path could write NULL into
file_size_bytes or has_embedded_cover. Unset values now leave the stored
value alone.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-07-23 10:08:01 -04:00
parent d9050eb4cc
commit 1ee41f9a66
2 changed files with 50 additions and 2 deletions

View File

@@ -16,6 +16,9 @@ from sqlalchemy import (
delete,
false,
func,
)
from sqlalchemy import inspect as sa_inspect
from sqlalchemy import (
literal,
not_,
or_,
@@ -179,6 +182,11 @@ TRACK_META_SCANNED_COLUMNS = (
)
@functools.cache
def _nullable_columns(model: type) -> frozenset[str]:
return frozenset(c.key for c in sa_inspect(model).columns if c.nullable)
class SyncedRomFiles(NamedTuple):
files: list[RomFile]
orphaned_cover_paths: list[str]
@@ -1772,6 +1780,8 @@ class DBRomsHandler(DBBaseHandler):
# clears it (with its file) once the cover is gone.
if column == "cover_path" and value is None:
continue
if value is None and column not in _nullable_columns(TrackMeta):
continue
if getattr(meta, column) != value:
setattr(meta, column, value)
@@ -1786,6 +1796,11 @@ class DBRomsHandler(DBBaseHandler):
"""Returns the cover path this update orphaned, if any."""
for column in ROM_FILE_SCANNED_COLUMNS:
value = getattr(scanned, column)
# A column left unset on the scanned row reads as None, and the
# model default only applies on insert, so writing it through would
# break the NOT NULL columns on the update path.
if value is None and column not in _nullable_columns(RomFile):
continue
if getattr(row, column) != value:
setattr(row, column, value)

View File

@@ -208,8 +208,41 @@ class TestSyncRomFiles:
(second,) = _sync(rom, [_scanned_file(rom, "a.bin", size=200, sha1="new-sha1")])
assert second.id == first.id
assert second.file_size_bytes == 200
assert second.sha1_hash == "new-sha1"
reloaded = db_rom_handler.get_rom_file_by_id(first.id)
assert reloaded is not None
assert reloaded.file_size_bytes == 200
assert reloaded.sha1_hash == "new-sha1"
def test_retagged_track_meta_is_updated_in_place(self, rom: Rom):
def scanned(title: str, year: int) -> RomFile:
return _scanned_file(
rom,
"track01.flac",
category=RomFileCategory.SOUNDTRACK,
track_meta=TrackMeta(rom_id=rom.id, title=title, year=year),
)
(first,) = _sync(rom, [scanned("Green Hill", 1991)])
_sync(rom, [scanned("Green Hill Zone", 1992)])
reloaded = db_rom_handler.get_rom_file_by_id(first.id)
assert reloaded is not None
assert reloaded.track_meta is not None
assert reloaded.track_meta.title == "Green Hill Zone"
assert reloaded.track_meta.year == 1992
def test_unset_columns_do_not_overwrite_not_null_values(self, rom: Rom):
"""A scanned row leaves unset columns as None, and the model defaults
only apply on insert. The update path has to skip them rather than
write NULL into a NOT NULL column."""
scanned = _scanned_file(rom, "a.bin", size=200)
_sync(rom, [scanned])
unset = _scanned_file(rom, "a.bin")
unset.file_size_bytes = None # type: ignore[assignment]
(updated,) = _sync(rom, [unset])
assert updated.file_size_bytes == 200
def test_renamed_file_is_matched_by_content(self, rom: Rom):
(first,) = _sync(rom, [_scanned_file(rom, "a.bin")])