Skip to content

Replace TinyDB with PostgreSQL#56

Open
max-tet wants to merge 4 commits intomainfrom
replace-tinydb-with-postgres
Open

Replace TinyDB with PostgreSQL#56
max-tet wants to merge 4 commits intomainfrom
replace-tinydb-with-postgres

Conversation

@max-tet
Copy link
Copy Markdown
Member

@max-tet max-tet commented Apr 10, 2026

Summary

  • Replace TinyDB (JSON file-based, global thread lock) with PostgreSQL + psycopg3 (async) + yoyo-migrations
  • Create per-entity database modules following conn-first-arg pattern with RETURNING *, class_row/dict_row, and LiteralString SQL
  • Convert all service and web layers from sync to async database access
  • Convert blinker signals to async (send_async()) for handlers that write to the database
  • Add one-time TinyDB→Postgres data migration on first startup (reads JSON directly, no tinydb dependency)
  • Remove TEST_ENV / requires_test_env distinction — all tests use pytest-docker Postgres
  • Add docker-compose postgres service for both dev and test environments

Database modules created

connection.py, migration.py, errors.py, installed_apps.py, identities.py, terminals.py, peers.py, backups.py, tours.py, app_usage.py, kv_store.py, tinydb_migration.py

Test plan

  • All 117 tests pass (2 pre-existing Docker-related failures unrelated to this change)
  • TinyDB→Postgres migration tested with sample database fixture
  • Formatting/linting clean (just cleanup)

🤖 Generated with Claude Code

MagicMock/mock.effective_git_name/140297175410272 and others added 4 commits April 10, 2026 15:11
Migrate from TinyDB (JSON file-based) to PostgreSQL with psycopg3 (async)
and yoyo-migrations. This eliminates the global threading lock that serialized
all DB access and the full JSON rewrite on every mutation.

Key changes:
- Add async connection pool (psycopg_pool) with db_conn() context manager
- Add yoyo-migrations with initial schema for all 8 tables
- Create per-entity database modules (installed_apps, identities, terminals,
  peers, backups, tours, app_usage, kv_store) following conn-first-arg pattern
- Convert all service/web layers from sync TinyDB to async Postgres
- Convert blinker signals to async (send_async) for DB-writing handlers
- Add one-time TinyDB→Postgres data migration on first startup
- Remove TEST_ENV distinction — all tests use pytest-docker Postgres
- Add docker-compose postgres service for both dev and test

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Remove **/*.sql from .gitignore so migration files are tracked in git;
  without this, yoyo ran against an empty migrations/ directory, applied
  no schema, and tests failed with 'relation does not exist' before the
  fixture yield, leaking the open connection pool
- Wrap pool init/teardown in try/finally in app_client and db fixtures so
  shutdown_database() is always called even when setup fails; the leaked
  pool's background reconnect task previously blocked event loop shutdown,
  causing the full 6-hour GitHub Actions timeout

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant