Serve, console and database¶
Use to run the service (tempest serve), open the console (tempest shell) and operate the database (tempest db).
Part of the CLI recipe.
Run the service — tempest serve¶
tempest serve # host/port/reload from the settings
tempest serve --reload # turn reload on for this run
tempest serve --host 0.0.0.0 --port 9000 # override
tempest serve --workers 4 # several processes (no reload)
tempest serve --app src.api.app:create_app # app outside the conventional spots
The scaffolded main.py already boots uvicorn programmatically; this command
is for the project that does not have that file yet, or for overriding host
and port without editing .env. Resolution is run_server's: flag > the
project's settings > the SDK default (127.0.0.1, 8000, no reload).
uvicorn gets the string, never the object
The reloader re-imports the target in the child process. Handing it the
already-imported instance works — and silently turns --reload into
nothing. So the command imports the candidate once to validate it, and
passes "src.server:app" along.
--reload together with --workers exits 2: uvicorn cannot honour both.
And --no-reload really does turn it off, even with SERVER_RELOAD=true in
.env — the flag has three states (on, off, "you decide").
Interactive console — tempest shell¶
A REPL with the project already loaded, so "how many orders does this user have?" does not cost a throwaway script.
tempest shell — Python 3.11.12, top-level await enabled
available: WidgetModel, db, select, session, settings, text
session: open on sqlite+aiosqlite:///./app.db
>>> rows = await session.execute(select(WidgetModel))
>>> rows.scalars().all()
[<WidgetModel id=...>]
What the prompt holds: settings (the project's instance), every mapped
model the project defines (the SDK's BaseModel stays out — a query
against the base class is never what you meant), select, text, the
AsyncDatabaseManager as db, and an already-open session.
Top-level await, on purpose
The console compiles with ast.PyCF_ALLOW_TOP_LEVEL_AWAIT and runs the
coroutine on the same loop the session was opened on. Exposing a
run(coro) helper would look equivalent and is not: a session driven
from a second loop raises MissingGreenlet at the first lazy load.
--no-db starts without connecting — useful when the database is down and
you only want to inspect settings and models.
Database — tempest db¶
Alembic wrapper backed by AlembicHelper — your project's alembic.ini + env.py stay the source of truth.
DATABASE_URL resolution order:
--database-urlflag.DATABASE_URLenv var.- Settings on the project's
core/settings.py, under either code root (srcorapp). Anypydantic_settings.BaseSettingsinstance qualifies — the namessettingsandconfigare tried first, then the module is scanned by type — or the result of a zero-arg factory that returns one (get_settings(), or any callable annotated to returnBaseSettings). DATABASE_URLin the project's.env.sqlalchemy.urlfromalembic.ini.
The instance name no longer matters, and the factory counts
Up to 0.280.0 only the name settings was accepted, and the failure was
an ImportError swallowed by except Exception: return None. A
service naming its instance config got "no database URL. Pass
--database-url, set DATABASE_URL, or run inside a project with
src/core/settings.py" — pointing at two conditions the project already
satisfied. Today the instance's type decides, the documented
factory (get_settings()) is accepted, .env is read, and when
nothing resolves each attempt reports why on stderr:
note: importing src.core.settings failed: ImportError("cannot import name ...")
error: no database URL. Pass --database-url, set DATABASE_URL, put it in
.env, or expose settings with a DATABASE_URL on
src/core/settings.py (or app/core/settings.py).
The same discovery is shared by tempest db, tempest doctor,
tempest user and the generated env.py — a project whose settings
exist only as get_settings() now migrates outside the CLI too.
tempest db init # create alembic.ini + alembic/env.py
tempest db revision -m "init users table" # autogenerate by default
tempest db revision -m "manual change" --manual # empty file you'll edit
tempest db upgrade # alembic upgrade head
tempest db upgrade <rev> # upgrade to a specific revision
tempest db downgrade # roll back one step
tempest db downgrade <rev> # roll back to a specific revision
tempest db current # print the applied revision
tempest db history # revisions newest → oldest
tempest db history -v # with full message body
tempest db stamp head # mark the DB without running migrations
tempest db squash -m "init" --yes # collapse history into 1 migration
tempest db backup # dump to backups/<db>_<ts>.<ext>
tempest db backup -o dump.sql # plain SQL (Postgres) by extension
tempest db restore dump.dump --yes # restore (clean + recreate)
tempest db seed # runs src.db.seeds:seed
tempest db seed --seed src.db.fixtures:demo # custom callable
stamp head does not adopt an existing database
stamp writes the pointer without running anything. Against a schema
Alembic did not build, stamping head records revisions that never ran
as applied: the database keeps its old columns while current, upgrade
and history all answer that it is up to date, and the failure surfaces
weeks later in production. The right revision for adoption is the base —
see Migrations », which ships AlembicHelper.adopt()
and sync_schema() so the choice is not made by hand.
When Alembic refuses the command¶
An Alembic error describing the state of the database — not a bug — comes out as one line, with what to do about it:
$ tempest db revision -m "new field" --autogenerate
error: the database is behind head — a migration is pending. Run `tempest db upgrade`, then try again.
(TEMPEST_DEBUG=1 for the full traceback)
$ echo $?
1
Four conditions get advice: the database is behind head, the history has more
than one head, the revision is not in alembic/versions/, and two revisions
are not on the same branch. Any other message is printed exactly as Alembic
wrote it — a wrong hint would be worse than the raw words.
Want the traceback back?
TEMPEST_DEBUG=1 tempest db revision ... re-raises untouched. Use it when
the error is not one of the conditions above and you need to see where
it came from.
Only the CLI translates
Calling AlembicHelper.revision(...) directly, from a script or a
lifespan, still raises alembic.util.exc.CommandError. Code wants the
typed exception; a person at a terminal wants the sentence.
Collapse the history — tempest db squash¶
Over time the alembic/versions/ directory grows without bound — every schema tweak adds another file Alembic must walk on every upgrade. squash resets that history to a single root migration describing the current schema, while keeping existing databases usable.
Destructive — run it against a development database
squash runs downgrade base on the configured database (DROPs every table) so it can autogenerate the full schema into a single file. That's why it requires --yes. Make sure DATABASE_URL points at a dev database before running.
The flow is:
- Capture the current
head(used to name the backup directory). downgrade base— empty the database so autogenerate sees an empty schema.- Move the old revisions into
alembic/versions/_squashed_<oldhead>/(a subdirectory Alembic ignores). Pass--no-backupto delete them instead. - Autogenerate one root migration from
BaseModel.metadata. upgrade headrecreates the schema and stamps the new revision.
tempest db squash -m "init" --yes # recoverable backup (default)
tempest db squash -m "init" --yes --no-backup # delete the old files
Production databases are not touched
squash only touches the configured database. After deploying the collapsed tree, mark production databases as migrated without recreating tables:
Manual squash (keep your data) → stamp --purge
If you unify migrations by hand (delete versions/, write one baseline migration, keep the database with its data), alembic_version still points at the old revision — which no longer exists in the tree. A plain stamp fails with Can't locate revision. Use --purge to clear the stale pointer and record the new baseline:
Recap: squash swaps an ever-growing history for one clean initial migration; stamp reconciles databases already at the final schema (use --purge when the recorded revision no longer exists). Recoverable backup by default — Git is your second net.
Backup and restore — tempest db backup / tempest db restore¶
Snapshot the database to a file and back. The strategy differs per dialect, but the CLI is the same:
- PostgreSQL —
pg_dump/pg_restore. The format comes from the file extension:.dump→ custom (pg_dump -Fc, compressed, restored withpg_restore),.sql→ plain (psql). Force it with--plain/--custom. Requires the PostgreSQL client tools onPATH. - SQLite — copies the database file.
Prerequisite: PostgreSQL client tools
backup / restore against a PostgreSQL database depend on the pg_dump, pg_restore and psql binaries being on your PATH. They do not ship with the Python package — they are installed by your operating system. Without them the CLI fails with a clear message ('pg_dump' not found on PATH).
SQLite needs nothing — the backup is a stdlib file copy.
tempest db backup # backups/<db>_<YYYYMMDD-HHMMSS>.dump
tempest db backup -o snapshot.sql # plain SQL (Postgres) by extension
tempest db backup -o snap.dump --custom # force the custom format
tempest db restore snapshot.sql --yes # restore (psql)
tempest db restore snap.dump --yes # restore (pg_restore --clean --if-exists)
Restore overwrites the target database
By default the restore is clean + recreate: existing objects are dropped before being recreated, so the result is a faithful copy of the backup (pg_restore --clean --if-exists; plain drops/recreates the public schema; SQLite overwrites the file). That's why it requires --yes. Pass --no-clean to apply the dump on top of the current schema.
The Postgres password never leaks into ps
The URL is parsed into -h/-p/-U/-d and the password is passed via PGPASSWORD in the subprocess environment — never on the command line.
Recap: backup takes a snapshot (format by extension on Postgres, file copy on SQLite); restore --yes brings it back, cleaning the target by default.
Catch model drift — tempest db check¶
tempest db revision --autogenerate records a diff when you ask for one.
Nothing notices when somebody edits a model and forgets to ask — until
the deploy that needed the column.
No drift:
With drift, it names the operations a revision would contain and exits 1:
error: New upgrade operations detected: [('add_column', ...'colour'...)]
Run `tempest db revision -m "<message>"` to record them.
This is a CI step
Run it after tempest db upgrade in the pipeline. The comparison needs
the database at head — when it is behind, Alembic says so and the
message is passed through instead of being reported as drift.
Seed the database — tempest db seed¶
Runs a project seed callable inside a managed session (commit on success, rollback on error). The callable takes a positional AsyncSession and may be sync or async; what it inserts is up to you — the SDK only wires the session lifecycle. Defaults to importing src.db.seeds:seed.
from sqlalchemy.ext.asyncio import AsyncSession
from src.db.models import CategoryModel
async def seed(session: AsyncSession) -> int:
"""Seed initial categories. Return the count (optional)."""
session.add_all([CategoryModel(name="Books"), CategoryModel(name="Games")])
await session.flush()
return 2
When the callable returns an int, the CLI prints the count: Seeded via src.db.seeds:seed (2 rows).