fix(tests): share one in-memory SQLite connection via StaticPool - #174
Merged
Conversation
The session-scoped integration engine used a bare
`sqlite+aiosqlite:///:memory:` URL. Each pooled connection then gets its
own empty in-memory database; `Base.metadata.create_all` ran on only one,
so once the pool opened a second connection (concurrent sessions — e.g. a
request-scoped session plus a service opening its own) that connection saw
an empty DB and every subsequent test errored at setup with
`sqlite3.OperationalError: no such table: users`. This intermittently broke
CI (e.g. starting at test_project_cache_failed_docs.py) and gated the
Heroku auto-deploy, which only fires on a green CI run on main.
Use `poolclass=StaticPool` + `connect_args={"check_same_thread": False}` so
the whole session-scoped engine shares a single connection — all sessions
hit the same in-memory DB with the schema present.
Verified: full integration suite 525 passed locally, 0 "no such table".
Co-Authored-By: Claude Opus 4.8 (1M context) <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.
Problem
CI (
backend-lint-test→ Integration tests) was failing onmainwith a cascade of:starting at
tests/integration/test_project_cache_failed_docs.pyand taking out every following integration test. Because the Heroku auto-deploy (deploy.yml) only fires on a green CI run on main, this blocked all deploys.Root cause
The session-scoped engine fixture used a bare
sqlite+aiosqlite:///:memory:URL. With the default pool, each connection gets its own empty in-memory database.Base.metadata.create_allruns on a single connection, so as soon as the pool opens a second connection (concurrent sessions — e.g. the request-scoped session plus a service opening its own), that connection sees an empty DB →no such table. Whether it triggered depended on connection/session timing, hence the intermittent CI failures.Fix
Create the engine with
poolclass=StaticPool+connect_args={"check_same_thread": False}so the whole session-scoped engine shares one connection — every session hits the same in-memory DB with the schema present. This is the canonical SQLAlchemy pattern for in-memory SQLite test databases.Verification
0"no such table" occurrences.ruff format --check+ruff checkclean.🤖 Generated with Claude Code