Skip to content

Replace TinyDB with a Postgres #25

@max-tet

Description

@max-tet
  • Add postgres to the docker-compose stack
  • Use psycopg3
  • Write SQL
  • refactor all database objects and operations
  • migrate existing database to Postgres on startup
    • Make a backup of the tinyDB database before
    • Only do this if it was not done before
  • Make unit tests work again and make them fast by reusing the application and just truncating tables between tests where possible

Database Patterns

Look at https://github.com/FreeshardBase/freeshard-controller/tree/main/freeshard-controller-backend/freeshard_controller/db for examples. Copy what is needed.

  • All functions take AsyncConnection as their first param — the caller is responsible for connection lifecycle.
  • Use db_conn() context manager from connection.py to get a connection from the pool.
  • Row-to-model mapping uses cursor(row_factory=class_row(PydanticModel)).
  • SQL is written inline as f-strings with parameterized values (psycopg %(name)s style placeholders). Type-annotate SQL strings as LiteralString where possible.
  • Dynamic SQL construction (for variable columns in UPDATE/INSERT) is built from Pydantic model_dump() keys.
  • Explicit await conn.commit() is needed when autocommit is off (most service-layer callers commit explicitly).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions