backend
Technical notes on web development, DevOps, and AI integration.
157 articles
- 21:24backend
When the Guard Cannot Answer: Fail-Closed Media Deletion
A failing usage census used to count as permission to delete. Now ErrCensusUnavailable cancels the delete: row and file survive, client gets 503.
TL;DR: Media deletion used to fail open: if the usage-count query errored, the code assumed the file was safe to delete. The fix returns ErrCensusUnavailable, mapped to a 503 CENSUS_UNAVAILABLE response, with real details logged server-side per OWASP guidance. Tests now verify deletion is blocked, data stays intact, and no stray files are written.
#golang#error-handling#http - 21:24backend
The Safe Boundary: Why the Roundtrip Test Stops at Checkpoint 56
The down chain breaks mid-way: an old down migration inserts an ENUM value that no longer exists there. Checkpoint 56 is the tested safe window.
TL;DR: The migration roundtrip test stops at version 56 instead of unwinding to zero, because a cross-dependency between migrations 45 and 51 breaks any deeper descent. Version 56 is the deepest proven-safe checkpoint, verified with spot-checks on 8 tables and 12 columns. A separate test pins the dev-only login route to return 404 everywhere except development.
#golang#golang-migrate#mysql - 13:09backend
Review Moderation State Machine: State Transition Validation Through Integration Testing
Integration tests prove the review moderation machine: a wrong 9.9/999 seed, Remove filling the audit trail, Restore clearing it, aggregates recomputed both ways.
TL;DR: Integration tests inject absurd aggregates and verify Remove triggers real recalculation through production logic, not test-only updates. The moderation flow atomically flips approved to removed, fills four audit columns, enforces whitelisted reasons, and blocks double removal with ErrNotFound, while restore reverses everything. Table-driven tests covering boundaries, duplication, and pagination turn state transitions into a guaranteed contract.
#go#testing#mysql - 11:31backend
Repository Contract at the SQL Line
Integration tests lock the repository contract at the SQL line: mapping error 1062 to a Go sentinel, soft-delete visibility, and pagination clamps on MySQL.
TL;DR: Binding a time function as a string parameter silently failed in MySQL because parameter markers only accept data values, not SQL expressions, so the fix splits inserts into two branches calling the function inline. Integration tests now lock the repository contract, mapping driver error 1062 to ErrSlugTaken via errors.Is. repository_lifecycle_test.go covers visibility, soft deletes, sorting, and pagination clamps.
#go#testing#mysql - 05:17testing
The package clause Line That Still Passes Build, Vet, and Test
A wrong package clause on a moved Go test file still passes go build, go vet, and go test. The import-binding rule behind it, and the procedural guard.
TL;DR: Moving a Go test file to a new directory silently broke its package clause: it still said package servicetype instead of servicetype_test. The build stayed green because unaliased imports share the import's name, so every servicetype.X call hit the import legally. Lesson: after relocating tests, manually check the package clause on line one, since tooling won't catch it.
#go#golang#testing - 04:27testing
Testing a Go Rate Limiter Without Waiting for Real Time
Swap the clock in Go tests: a rate limiter proven without sleep, plus when to graduate to testing/synctest.
TL;DR: Rate limiter tests shouldn't chase real time with time.Sleep, which makes them slow and flaky. Instead, expose a Now func() time.Time field on the struct and swap in a fixed clock, letting tests advance time instantly with no sleeps. For code that sleeps internally, Go's stable testing/synctest bubble fakes the clock.
#go#testing#rate-limit - 03:45backend
When to Export a Go Symbol
A test migration into external packages doubles as an API audit: three doors between public-path rewrites, real exports, and export_test.go.
TL;DR: Moving tests out of a package forces every internal function through one of three doors: rewrite via public paths, make a real export, or use export_test.go. A supposedly test-only commit exposed four importer functions worth exporting, revealing hidden API value. Test migrations double as free API audits, with zero exports signaling a healthy public interface.
#go#api-design#testing - 03:29backend
Moved the Tests, Coverage Went Up
Moving Go tests into a central tree as external _test packages forces rewrites through the public API, deletes dead helpers, and coverage rises.
TL;DR: Moving sixteen test files into a central tree looked purely mechanical, yet coverage rose instead of staying flat. Renaming packages to entityadmin_test forced black-box testing through public APIs, exposing dead helpers and internal-only tests along the way. Now _test packages are my default, export_test.go stays an emergency valve, and "mechanical" commit messages get more skepticism.
#go#testing#black-box-testing - 02:42testing
Zero Percent Coverage While Every Test Passes
Moving tests to a centralized tree makes per-package coverage read zero percent; one -coverpkg flag on the coverage mode restores the number.
TL;DR: Moving Go tests into a centralized external package caused coverage to drop to 0.0% even though everything still passes, because go test only instruments the package under test by default. The fix is the -coverpkg flag, which tells the tool which packages to measure. Execution and coverage are separate paths, so CI needs no changes.
#go#coverage#testing