Skip to content

fix(tag-db): import fs so SG tag database uploads persist - #39

Open
meelyn-pandit wants to merge 2 commits into
lts_24-06.isofrom
fix/tag-db-upload-fs-import
Open

fix(tag-db): import fs so SG tag database uploads persist#39
meelyn-pandit wants to merge 2 commits into
lts_24-06.isofrom
fix/tag-db-upload-fs-import

Conversation

@meelyn-pandit

Copy link
Copy Markdown
Contributor

Problem

Uploading a SensorGnome tag database through the web interface silently destroyed the existing database and stored nothing. The SensorGnome interface then failed with:

Error reading file: {"code":1,...,"cmd":"/usr/bin/sqlite3 /data/sg_files/SG_tag_database.sqlite 'select ... from tags ...'"}

i.e. Error: no such table: tags, because /data/sg_files/SG_tag_database.sqlite was left empty (0 bytes).

Root cause

src/station-interface/routes/sensorgnome/upload-sg-tag-file.js calls fs.writeFileSync(uri, req.body) but only imported RunCommandfs was never imported. The handler runs in order:

  1. rm /data/sg_files/SG_tag_database* — deletes the current tag DB (succeeds)
  2. fs.writeFileSync(...) — throws ReferenceError: fs is not defined

So every upload wiped the old database and wrote nothing. Confirmed on-station in the station-web-interface logs:

tag database upload
about to delete sg tag db files
executing command rm /data/sg_files/SG_tag_database*
writing tag database file
something went wrong handling new SG tag database file
ReferenceError: fs is not defined
    at .../upload-sg-tag-file.js:14:7

Fix

Add the missing import fs from 'fs'.

Follow-up (not in this PR)

The handler still rms the existing DB before writing the new one, so an aborted/invalid upload can still destroy a working database. A safer pattern would write to a temp file and rename into place only on success. Left out here to keep this a focused, minimal fix for the reported bug.

🤖 Generated with Claude Code

The /upload-sg-tag-file handler calls fs.writeFileSync() but never
imported fs, so every upload threw `ReferenceError: fs is not defined`
after the handler had already run `rm /data/sg_files/SG_tag_database*`.
Net effect: uploading a tag database deleted the existing one and wrote
nothing, leaving an empty/missing SG_tag_database.sqlite and causing the
SensorGnome interface to fail with `no such table: tags`.

Add the missing `import fs from 'fs'`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The handler deletes /data/sg_files/SG_tag_database* before writing the new
file. On a station that has no tag DB yet the glob matches nothing, so plain
rm exits non-zero; RunCommand rejects and the upload aborts before
fs.writeFileSync runs, surfacing "Command failed: rm ... No such file or
directory". Use rm -f so an empty match is treated as success.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant