Server: verify Phase 1 end to end against PostgreSQL 17
Set up the dev environment (uv-managed CPython 3.14.7, postgres:17 in a Podman container) and ran the skeleton for real: migrations apply, the API serves /api/v1/health and /healthz, the worker claims a job and drains it, 12/12 tests pass with nothing skipped. The schema-drift test failed on its first real run and found a genuine problem: Postgres reflects a varchar default as 'x'::character varying while the model renders 'x', so all ten string defaults read as permanent drift. Left alone that is the failure mode where you learn to ignore the test and then miss the one real change. Add schema_compare.py, a normaliser shared by migrations/env.py and the test so autogenerate and the test can never disagree about what counts as a difference. A real default change still fails it. Also: ruff import-order fix in captures.py, and the README gains the uv + Podman dev setup, the expected test count, and the CREATE DATABASE privilege the drift test needs. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
8a51e3d95e
commit
5d16ded553
6 files changed
+99
-9
No files matched your search
@@ -28,7 +28,7 @@ Anything here that contradicts SPEC means SPEC needs an edit, not a workaround.
|
|||||||
| Firmware | Written, compiles, **never run on hardware**. Known gaps below. |
|
| Firmware | Written, compiles, **never run on hardware**. Known gaps below. |
|
||||||
| Device↔server contract | Specified in full (`firmware/API.md`). Nothing implements the server half. |
|
| Device↔server contract | Specified in full (`firmware/API.md`). Nothing implements the server half. |
|
||||||
| Data model | Designed (SPEC §3) + §0 additions. No migration written. |
|
| Data model | Designed (SPEC §3) + §0 additions. No migration written. |
|
||||||
| Server | Phase 1 skeleton in [`server/`](server/) — config, schema, health, job queue, scheduler, systemd units. **Not yet run against a real PostgreSQL.** |
|
| Server | Phase 1 skeleton in [`server/`](server/) — config, schema, health, job queue, scheduler, systemd units. Verified end to end on Python 3.14.7 against PostgreSQL 17: 12/12 tests, migrations applied, API serving, worker draining the queue. Not yet on the real host. |
|
||||||
| WebUI | **Nothing exists.** |
|
| WebUI | **Nothing exists.** |
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -83,7 +83,8 @@ written vs. what still needs doing on the real host:
|
|||||||
- [x] systemd units `poorbird-api.service` + `poorbird-worker.service`, `After=postgresql.service`, restart policies, sandboxing
|
- [x] systemd units `poorbird-api.service` + `poorbird-worker.service`, `After=postgresql.service`, restart policies, sandboxing
|
||||||
- [x] Caddyfile with DNS-01 TLS, the `:8080` plain-HTTP camera listener and the auth-checked image handoff
|
- [x] Caddyfile with DNS-01 TLS, the `:8080` plain-HTTP camera listener and the auth-checked image handoff
|
||||||
- [x] `poorbird check` — validates config, paths and DB before anything starts
|
- [x] `poorbird check` — validates config, paths and DB before anything starts
|
||||||
- [ ] **Run it against a real PostgreSQL** — nothing here has executed yet; `pytest` and `poorbird check` are the first two commands
|
- [x] **Run it against a real PostgreSQL** — done on the dev machine (uv-managed Python 3.14.7, Podman `postgres:17`): `poorbird check`, `migrate`, `serve`, `worker` and 12/12 tests all green
|
||||||
|
- [ ] Repeat the same four commands on the actual thin client, which is the run that counts
|
||||||
- [ ] Base install on the thin client: static IP (or DHCP reservation) and **hardcode that address in firmware** — more reliable than mDNS
|
- [ ] Base install on the thin client: static IP (or DHCP reservation) and **hardcode that address in firmware** — more reliable than mDNS
|
||||||
- [ ] PostgreSQL 17 from the distro or PGDG repo; dedicated role + database; **loopback TCP with `scram-sha-256`, not a Unix socket** (see 1.2)
|
- [ ] PostgreSQL 17 from the distro or PGDG repo; dedicated role + database; **loopback TCP with `scram-sha-256`, not a Unix socket** (see 1.2)
|
||||||
- [ ] Caddy DNS provider plugin build (`caddy add-package …`) and a real hostname
|
- [ ] Caddy DNS provider plugin build (`caddy add-package …`) and a real hostname
|
||||||
|
|||||||
+40
-1
@@ -11,6 +11,34 @@ behind Caddy. No containers — but nothing here blocks them either.
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## Developing on Windows (uv + Podman)
|
||||||
|
|
||||||
|
This is the verified setup on the dev machine. Python is managed by `uv`, so
|
||||||
|
nothing touches the system PATH or the registry, and PostgreSQL runs in a
|
||||||
|
throwaway Podman container. Neither changes how the project deploys — the app
|
||||||
|
still talks loopback TCP exactly as it will on the server.
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
winget install astral-sh.uv
|
||||||
|
uv python install 3.14.7
|
||||||
|
|
||||||
|
podman machine start
|
||||||
|
podman run -d --name poorbird-db -p 127.0.0.1:5432:5432 `
|
||||||
|
-e POSTGRES_USER=poorbird -e POSTGRES_PASSWORD=poorbird -e POSTGRES_DB=poorbird `
|
||||||
|
docker.io/library/postgres:17
|
||||||
|
|
||||||
|
cd server
|
||||||
|
uv venv --python 3.14.7
|
||||||
|
uv pip install -e ".[dev]"
|
||||||
|
# write .env (see the Linux section below; use Windows paths for the roots)
|
||||||
|
|
||||||
|
.venv\Scripts\poorbird check
|
||||||
|
.venv\Scripts\poorbird migrate
|
||||||
|
.venv\Scripts\python -m pytest
|
||||||
|
```
|
||||||
|
|
||||||
|
Later sessions only need `podman machine start` and `podman start poorbird-db`.
|
||||||
|
|
||||||
## Try it on any Linux machine
|
## Try it on any Linux machine
|
||||||
|
|
||||||
Needs Python ≥ 3.11 and a PostgreSQL you can reach. Production targets 3.13;
|
Needs Python ≥ 3.11 and a PostgreSQL you can reach. Production targets 3.13;
|
||||||
@@ -64,7 +92,11 @@ pytest -m 'not db' # no PostgreSQL needed
|
|||||||
Queue and schema tests need a real PostgreSQL — `FOR UPDATE SKIP LOCKED` has
|
Queue and schema tests need a real PostgreSQL — `FOR UPDATE SKIP LOCKED` has
|
||||||
no equivalent elsewhere, and mocking it would test nothing. They skip
|
no equivalent elsewhere, and mocking it would test nothing. They skip
|
||||||
automatically when the database is unreachable, so a green run with no
|
automatically when the database is unreachable, so a green run with no
|
||||||
Postgres proves less than it looks. Check for `skipped` in the summary.
|
Postgres proves less than it looks. **Check for `skipped` in the summary**;
|
||||||
|
a complete run is 12 passed, 0 skipped.
|
||||||
|
|
||||||
|
`test_schema_matches_models.py` creates and drops a `poorbird_schema_check`
|
||||||
|
database on the same server. It needs a role that may `CREATE DATABASE`.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -101,6 +133,13 @@ Validation belongs in a Pydantic model, not in the DDL. The trade: you cannot
|
|||||||
index or constrain an individual setting in SQL. If one ever needs querying
|
index or constrain an individual setting in SQL. If one ever needs querying
|
||||||
across devices, promote just that key to a column.
|
across devices, promote just that key to a column.
|
||||||
|
|
||||||
|
**Server defaults are compared through a normaliser.** Postgres reflects a
|
||||||
|
varchar default as `'viewer'::character varying` while the model renders
|
||||||
|
`'viewer'`. Compared literally, every string default in the schema reads as
|
||||||
|
drift forever — which trains you to ignore the one time it isn't. See
|
||||||
|
`schema_compare.py`; autogenerate and the drift test share it, so the tool and
|
||||||
|
the test can never disagree about what counts as a difference.
|
||||||
|
|
||||||
**Migration 0001 builds from `Base.metadata`.** A bounded exception, and
|
**Migration 0001 builds from `Base.metadata`.** A bounded exception, and
|
||||||
`tests/test_schema_matches_models.py` migrates a scratch database and asserts
|
`tests/test_schema_matches_models.py` migrates a scratch database and asserts
|
||||||
an empty autogenerate diff, so the models and the migration cannot silently
|
an empty autogenerate diff, so the models and the migration cannot silently
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from sqlalchemy import engine_from_config, pool
|
|||||||
|
|
||||||
from poorbird.config import get_settings
|
from poorbird.config import get_settings
|
||||||
from poorbird.models import Base
|
from poorbird.models import Base
|
||||||
|
from poorbird.schema_compare import COMPARE_OPTS
|
||||||
|
|
||||||
config = context.config
|
config = context.config
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ if not config.get_main_option("sqlalchemy.url", None):
|
|||||||
|
|
||||||
target_metadata = Base.metadata
|
target_metadata = Base.metadata
|
||||||
|
|
||||||
OPTS = {"compare_type": True, "compare_server_default": True}
|
OPTS = COMPARE_OPTS
|
||||||
|
|
||||||
|
|
||||||
def run_migrations_offline() -> None:
|
def run_migrations_offline() -> None:
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ from sqlalchemy import (
|
|||||||
BigInteger,
|
BigInteger,
|
||||||
Boolean,
|
Boolean,
|
||||||
CheckConstraint,
|
CheckConstraint,
|
||||||
|
Column,
|
||||||
DateTime,
|
DateTime,
|
||||||
Float,
|
Float,
|
||||||
ForeignKey,
|
ForeignKey,
|
||||||
@@ -27,7 +28,6 @@ from sqlalchemy import (
|
|||||||
String,
|
String,
|
||||||
Table,
|
Table,
|
||||||
Text,
|
Text,
|
||||||
Column,
|
|
||||||
Uuid,
|
Uuid,
|
||||||
func,
|
func,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
"""Shared Alembic comparison options.
|
||||||
|
|
||||||
|
Used by both ``migrations/env.py`` and the schema-drift test, so autogenerate
|
||||||
|
and the test can never disagree about what counts as a difference.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
def _normalise_default(value: Any) -> str:
|
||||||
|
"""Strip a Postgres type cast and the SQL quoting from a default.
|
||||||
|
|
||||||
|
Postgres reflects a varchar default as ``'viewer'::character varying``
|
||||||
|
while the model's metadata renders it as ``'viewer'``. Compared literally,
|
||||||
|
every string default in the schema looks like drift, forever -- which
|
||||||
|
trains you to ignore the one time it isn't.
|
||||||
|
"""
|
||||||
|
text = str(value).strip()
|
||||||
|
if "::" in text:
|
||||||
|
text = text.split("::", 1)[0]
|
||||||
|
return text.strip().strip("'").strip().lower()
|
||||||
|
|
||||||
|
|
||||||
|
def compare_server_default(
|
||||||
|
context: Any,
|
||||||
|
inspected_column: Any,
|
||||||
|
metadata_column: Any,
|
||||||
|
inspected_default: Any,
|
||||||
|
metadata_default: Any,
|
||||||
|
rendered_metadata_default: Any,
|
||||||
|
) -> bool | None:
|
||||||
|
"""Return True when the defaults genuinely differ.
|
||||||
|
|
||||||
|
Returning None hands the decision back to Alembic, which is what we want
|
||||||
|
whenever one side has no default at all -- that IS a real difference and
|
||||||
|
Alembic's own logic reports it correctly.
|
||||||
|
"""
|
||||||
|
if inspected_default is None or rendered_metadata_default is None:
|
||||||
|
return None
|
||||||
|
return _normalise_default(inspected_default) != _normalise_default(rendered_metadata_default)
|
||||||
|
|
||||||
|
|
||||||
|
# One place to define what "compare the schema" means.
|
||||||
|
COMPARE_OPTS: dict[str, Any] = {
|
||||||
|
"compare_type": True,
|
||||||
|
"compare_server_default": compare_server_default,
|
||||||
|
}
|
||||||
@@ -26,6 +26,7 @@ def test_migrated_schema_matches_the_models():
|
|||||||
from poorbird.config import get_settings
|
from poorbird.config import get_settings
|
||||||
from poorbird.db import engine
|
from poorbird.db import engine
|
||||||
from poorbird.models import Base
|
from poorbird.models import Base
|
||||||
|
from poorbird.schema_compare import COMPARE_OPTS
|
||||||
|
|
||||||
scratch = "poorbird_schema_check"
|
scratch = "poorbird_schema_check"
|
||||||
|
|
||||||
@@ -53,10 +54,9 @@ def test_migrated_schema_matches_the_models():
|
|||||||
command.upgrade(cfg, "head")
|
command.upgrade(cfg, "head")
|
||||||
|
|
||||||
with scratch_engine.connect() as conn:
|
with scratch_engine.connect() as conn:
|
||||||
ctx = MigrationContext.configure(
|
# The same options autogenerate uses, so the test and the tool can
|
||||||
conn,
|
# never disagree about what counts as a difference.
|
||||||
opts={"compare_type": True, "compare_server_default": True},
|
ctx = MigrationContext.configure(conn, opts=dict(COMPARE_OPTS))
|
||||||
)
|
|
||||||
diff = compare_metadata(ctx, Base.metadata)
|
diff = compare_metadata(ctx, Base.metadata)
|
||||||
|
|
||||||
# alembic_version is Alembic's own bookkeeping, not part of the models.
|
# alembic_version is Alembic's own bookkeeping, not part of the models.
|
||||||
|
|||||||
Reference in new issue
Block a user