Repository navigation
Fix usage counters, address_tx key, tx address folding, insert defaults - #6
Merged
Merged
Conversation
The file had CRLF line endings, so `gofmt -l .` flagged it. Whitespace only; no change to any query. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
- GET /fmsgid/:address scanned the usage query into the wrong fields (e.g. recvSizeTotal got the 1-day received size). sqlActuals now selects in the order the handler scans, via QueryRow since the aggregate always returns one row. - address_tx had primary key (address_lower, ts), so a second message to an address with the same timestamp failed with 500 and wasn't counted. It now has a bigserial id primary key; the existing (address_lower, type, ts desc) index stays. dd.sql migrates an existing table idempotently: adds id, drops the old primary key and adds the new one, each only when needed. - POST /fmsgid/send and /recv inserted the address as given into address_lower, so any case difference failed the foreign key. The address is now validated (400 if invalid) and case folded, the same as address rows are keyed. - POST /fmsgid inserted hard-coded limits; it now gives only the identity columns so accepting_new and the limits take the table's column defaults. - Correct parseCSV's comment: missing limits default to the table's defaults, not -1. Adds database tests (skipped unless FMSGID_TEST_DATABASE_URL is set) that record messages of both types, with shared timestamps and mixed case, and assert every usage field, and that check a new address takes the column defaults. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Four bugs in recording and reporting address usage:
GET /fmsgid/:addressscannedsqlActualsin a different order from its SELECT, so e.g.recvSizeTotalheld the 1-day received size andsendSizePer1dheld the total received size. The query now selects in the order the handler scans (viaQueryRow; the aggregate always returns one row).address_txhadprimary key (address_lower, ts), so a second message to an address at the same timestamp failed with 500 and was not counted. It now has abigserialidprimary key; the existing non-unique(address_lower, type, ts desc)index stays.POST /fmsgid/sendand/recvinserted the address as given intoaddress_lower, so any case difference failed the foreign key toaddress. The address is now validated (400if invalid) and case folded withcases.Fold(), the same as address rows are keyed.POST /fmsgidinserted literal limit values; it now inserts onlyaddress_lower,addressanddisplay_name, soaccepting_newand every limit take the table's column defaults. The CSV upsert path is unchanged; its comment wrongly said missing limits default to-1and now says they match the table defaults.Also includes a whitespace-only commit converting
src/sql.gofrom CRLF to LF (gofmt -lflagged it), so the query diffs are readable.Schema migration
dd.sqlstays idempotent and migrates an existing, populatedaddress_txwhen re-run:doblock addsid bigserialonly if the column is missing (notadd column if not exists, which on older PostgreSQL versions leaves an orphan sequence on every run),id, andprimary key (id)if there is none.Adding the column rewrites the table and briefly holds an exclusive lock on it. The script must run as the table's owner. The role the service connects as needs
USAGEon the new sequenceaddress_tx_id_seqto insert. The old binary keeps working against the migrated schema (it never namesid), so the schema can be applied before the new binary starts.Tests
src/usage_test.go(helpers insrc/db_test.go), skipped unlessFMSGID_TEST_DATABASE_URLis set; documented under Build > Test in the README:TestAddressDetail_Usagerecords sends and receives through the API (some older than a day, some sharing a timestamp, some in different case) and asserts each of the six usage fields.TestAddressDetail_NoUsage: an unused address reports zeros.TestPostCreateAddress_ColumnDefaultscopies theaddresstable into a scratch schema with different column defaults and checks a created address takes them.TestPostAddressTx_InvalidAddress(no database needed).mainthese tests fail: the same-timestamp/mixed-case receive returns 500, and the invalid-address request reaches the database.go vet ./...is clean.go test -race ./...passes with and without a database (PostgreSQL 16 in a throwaway container).dd.sqlthree times on a database built from the olddd.sqlwith rows in it, and twice on a fresh database. Both ended withaddress_tx_pkeyon(id)and exactly one sequence, existing rows were kept and given ids, and duplicate(address_lower, ts)inserts then succeeded.CI does not start PostgreSQL, so it skips the database tests.
🤖 Generated with Claude Code