Skip to content

armgit / lyftr

Public Imported from GitHub
0

Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.

refactor(db): serialize on one connection + enforce FK in tests; cascade guard

Cawlumm43c3ce8160b9
diff --git a/backend/controllers/cascade_test.go b/backend/controllers/cascade_test.go
new file mode 100644
index 0000000..b77e556
--- /dev/null
+++ b/backend/controllers/cascade_test.go
@@ -0,0 +1,34 @@
+package controllers
+
+import (
+ "net/http"
+ "testing"
+
+ "github.com/Cawlumm/lyftr-backend/db"
+)
+
+// TestDeleteAccountCascadesChildren guards that DeleteAccount relies on ON DELETE
+// CASCADE — i.e. foreign keys must stay enforced. This is the behavioral guard
+// for keeping foreign_keys(on) in the production DSN (PR #32 dropped it; we
+// re-added it). The harness runs with foreign_keys=on, matching production.
+func TestDeleteAccountCascadesChildren(t *testing.T) {
+ setupTestDB(t)
+ uid := createTestUser(t)
+ if _, err := db.DB.Exec(`INSERT INTO weight_logs (user_id, weight) VALUES (?, 180)`, uid); err != nil {
+ t.Fatalf("seed weight log: %v", err)
+ }
+
+ c, w := newContext(uid, http.MethodDelete, "/api/v1/me", nil)
+ th.DeleteAccount(c)
+ if w.Code != http.StatusOK {
+ t.Fatalf("DeleteAccount: expected 200, got %d (%s)", w.Code, w.Body.String())
+ }
+
+ var n int
+ if err := db.DB.QueryRow(`SELECT COUNT(*) FROM weight_logs WHERE user_id = ?`, uid).Scan(&n); err != nil {
+ t.Fatalf("count after delete: %v", err)
+ }
+ if n != 0 {
+ t.Errorf("child weight_logs not cascade-deleted: got %d, want 0 (foreign keys off?)", n)
+ }
+}
diff --git a/backend/controllers/testhelper_test.go b/backend/controllers/testhelper_test.go
index b9ae492..d23ea34 100644
--- a/backend/controllers/testhelper_test.go
+++ b/backend/controllers/testhelper_test.go
@@ -23,7 +23,9 @@ var th *Handler
func setupTestDB(t *testing.T) {
t.Helper()
var err error
- dbName := fmt.Sprintf("file:testdb_%d?mode=memory&cache=shared&_foreign_keys=on", rand.Int63())
+ // modernc ignores the mattn-style _foreign_keys=on; use the _pragma form so the
+ // harness actually enforces foreign keys, matching the production DSN.
+ dbName := fmt.Sprintf("file:testdb_%d?mode=memory&cache=shared&_pragma=foreign_keys(on)", rand.Int63())
db.DB, err = sql.Open("sqlite", dbName)
if err != nil {
t.Fatalf("open test db: %v", err)
diff --git a/backend/db/sqlite.go b/backend/db/sqlite.go
index 06def90..6dde2d3 100644
--- a/backend/db/sqlite.go
+++ b/backend/db/sqlite.go
@@ -34,8 +34,14 @@ func Connect() {
log.Fatalf("failed to open database: %v", err)
}
- DB.SetMaxOpenConns(10)
- DB.SetMaxIdleConns(5)
+ // SQLite is a single writer — it does not do concurrent writes. Rather than
+ // fight that with a larger pool (which only turns contention into lock errors
+ // to retry), serialize all DB access through ONE connection. With the
+ // nested-cursor fix (no request needs a second connection mid-query), one
+ // connection is sufficient and leaves no in-process lock contention to fail on.
+ // The busy_timeout/WAL pragmas above remain only as cheap cross-process defense.
+ DB.SetMaxOpenConns(1)
+ DB.SetMaxIdleConns(1)
if err = DB.Ping(); err != nil {
log.Fatalf("failed to ping database: %v", err)