backend
Technical notes on web development, DevOps, and AI integration.
157 articles
- 18:58backend
Admin Page 500 and the Count Query That Lost Its Join
A shared WHERE filter referenced an alias the count query never joined, so every admin list request died with a 500. The fix was one line.
TL;DR: A superadmin's review moderation page crashed with a 500 due to an unknown column error, despite the list API working fine. The shared filter referenced alias c.module, but the count query hadn't joined the categories table, so MySQL couldn't resolve it. Adding the missing LEFT JOIN fixed the count and restored pagination, highlighting that shared filters need matching joins.
#mysql#go#sql - 17:10backend
The Checkbox That Silently Killed a Feature in a Go Backend
An unchecked checkbox sends nothing, and a Go backend reads that silence as false. Lessons from a silent admin-form bug.
TL;DR: Leaving checkboxes unchecked sent nothing, which the backend misread as disabling the features. In Go, a missing boolean unmarshals to false, so absent was treated as off and omitempty hid the problem. The fix was to always send explicit true or false and use null versus absent for nullable fields.
#go#json#react - 16:24backend
An Undocumented 404 Is a Contract Bug
Six lines of OpenAPI YAML and a regeneration changed what the frontend compiler knows. Error responses left undocumented are contract bugs.
TL;DR: What looked like a trivial six-line OpenAPI cleanup turned out to be critical for type safety. Since openapi-typescript only generates types for explicitly documented responses, an undocumented 404 forced the frontend to guess or use any. Documenting the error locks the contract so TypeScript catches missing handling early, saving hours of debugging later.
#openapi#api#typescript - 16:24backend
When a Feature Is Turned Off, Answer 404 Not 403
A disabled reviews feature answered 404 instead of 403. The HTTP spec actually backs that choice, and one cheap gate makes it practical.
TL;DR: Returning 403 for a disabled feature confirms the resource exists, inviting attackers to keep probing. Using 404 instead hides it completely, and a single service-layer flag can protect all related endpoints while the frontend hides UI via feature flags. For admin APIs though, 403 or 410 makes more sense since admins need to know the feature is just disabled.
#http#api#error-handling - 16:24database
Soft Delete Done Right: Predicates, RowsAffected, and Restore
Soft delete is not just DELETE swapped for UPDATE. Predicates on every query, a RowsAffected alarm, and a mirrored restore guard keep it honest.
TL;DR: Switching DELETE to soft delete with an UPDATE on deleted_at left deleted rows visible because SELECTs weren't filtered. Fixing it means adding deleted_at IS NULL to queries, checking RowsAffected to return 404, and mirroring logic for restores. You also need to invalidate cache on every state change, otherwise complexity and leaks quickly outweigh benefits.
#go#mysql#sql - 15:07backend
Three Nullable Shapes in a Go Write Path
Threading nullable columns through a Go CRUD: the PUT idiom, plain bools, a pointer ID, and an explicit null contract.
TL;DR: KotaPortal’s upsert adds four fields via three patterns: contact person uses empty-string-means-null, two booleans are always required, and type ID uses a nil pointer for null. PUT does a full overwrite, while responses avoid omitempty to ensure nulls appear explicitly for generated clients. New checks cap contact data at 100 characters and require type ID to be at least one.
#golang#api#mysql - 15:07backend
The Category Trash Can: One Endpoint and POST Restore
A soft delete API contract: the trashed query flag, POST restore, and a 404 that deliberately merges two conditions.
TL;DR: The categories list supports a trashed query param defaulting to false so existing clients keep seeing active items while new UIs can fetch soft-deleted ones. Restore is a POST endpoint returning 200 on success, 403 when out of scope, or a shared 404 for missing or not-trashed cases. It's a clean additive contract-first change that keeps compatibility without new endpoints.
#api#openapi#rest-api - 15:07backend
Designing a Down-First Migration: Additive Columns, Postponed FK
Designing a MySQL migration backwards: additive columns, a mirrored down file, and a foreign key deliberately postponed.
TL;DR: The migration adds nullable columns and indexes to entities and categories without imposing a foreign key yet. This additive approach keeps changes online and concurrent on InnoDB while ensuring rollback just drops what was added without rewriting data. Details like a nullable timestamp and deferred constraints prioritize safe rollbacks over premature relational perfection.
#mysql#golang#schema - 14:21backend
A Test That Passes by Day and Fails at Dawn
A date filter test failed every dawn from midnight to seven: two clocks in one test. Fix: derive dates from the database clock, pin midday.
TL;DR: A scheduled test failure came from two clocks: Go on a WIB host and MySQL in UTC, seven hours apart. Around midnight their dates diverged, so Go's yesterday still contained the database's today and assertions failed until morning. The fix uses MySQL's CURDATE as the single witness and pins rows to midday to stay clear of boundary drift.
#go#mysql#testing