claude-memory/graph/fixes/fix-sqlx-in-memory-sqlite-pool-requires-max-connections1-1b71b1.md
2026-03-01 20:20:58 -06:00

80 lines
2.6 KiB
Markdown

---
id: 1b71b163-b56f-4226-9731-a76ef245e532
type: fix
title: "Fix: sqlx in-memory SQLite pool requires max_connections=1"
tags: [sba-scouting, rust, sqlx, sqlite, testing, fix, in-memory]
importance: 0.8
confidence: 0.8
created: "2026-03-02T02:08:46.895029+00:00"
updated: "2026-03-02T02:20:58.504326+00:00"
relations:
- target: e5ec55be-ced4-4c30-8390-940b45dc2ed5
type: RELATED_TO
direction: outgoing
strength: 0.66
edge_id: 23a340f1-7bcf-471d-ae78-e4ef9efc73f8
- target: e895864f-9e9c-47e3-a95b-5c065ee0aae5
type: RELATED_TO
direction: outgoing
strength: 0.62
edge_id: 1403f29d-908b-4912-a04d-3950a761f980
- target: 893cf251-0ce1-43fa-9776-e88c0f3cd566
type: RELATED_TO
direction: outgoing
strength: 0.61
edge_id: 5d2f8ff7-bdc0-4103-b143-212d9eca9e40
- target: ed3e8fe6-5947-4022-96dc-dd4504ffa1a1
type: FOLLOWS
direction: outgoing
strength: 0.8
edge_id: 4c67b35b-1ebc-4bd6-a058-d1de16514fe0
- target: d75e2afd-8348-4fe6-9ea1-731604505aa6
type: FOLLOWS
direction: incoming
strength: 0.8
edge_id: 7d49db13-70e0-46d9-8707-fc0ddfe2df85
- target: d75e2afd-8348-4fe6-9ea1-731604505aa6
type: CAUSES
direction: incoming
strength: 0.9
edge_id: 090ec7e1-c8fc-40f5-8d60-4b2aef56bcbd
- target: 77c0b897-4124-432f-84de-700ff82dcde1
type: RELATED_TO
direction: outgoing
strength: 0.75
edge_id: 863714f3-635e-427a-a1fb-499b14b192e8
---
# sqlx In-Memory SQLite Pool: max_connections Must Be 1
## Problem
Integration tests using `:memory:` SQLite failed with "no such table" errors after wrapping `create_tables` in a transaction.
## Root Cause
With `max_connections > 1`, each connection in the pool gets its own separate in-memory database. A transaction on connection A (e.g. `pool.begin()` for `create_tables`) is invisible to queries on connection B. In-memory SQLite databases are per-connection — there is no shared state across connections.
## Fix
Detect `:memory:` path and force `max_connections(1)`. Also skip WAL journal mode for in-memory DBs (WAL requires a file).
```rust
let pool = if db_path == ":memory:" {
SqlitePoolOptions::new()
.max_connections(1)
.connect(":memory:")
.await?
} else {
SqlitePoolOptions::new()
.max_connections(5)
.connect(&format!("sqlite:{db_path}"))
.await?
};
// Only set WAL for file-backed DBs
if db_path != ":memory:" {
sqlx::query("PRAGMA journal_mode=WAL").execute(&pool).await?;
}
```
## Impact
All 5 integration tests were broken until this was applied. Applies to any test setup using `create_tables` inside a transaction with a shared pool.