Book › Part 6: Bring your own storage › A database
A database¶
Put the run log in a store that featherweight does not ship.
This is an outline, and the biggest of the four. The databases are SqliteDatabase, PostgresDatabase and yours. Most people never need to write one.
- Read the schema. The tables, their columns and constraints are written down in the schema.
- Subclass
Databaseand write its methods. They come in groups: connect and close; create, get and change a job; the compare-and-set steps that take, cancel, renew or take over a job; the events of a job; and the runs (create, mark running, finish, get, list, find one to reuse). Records go in and rows come out as Pydantic models, so a wrong type fails when the row is built. - Mind the two hard rules. An event is written in the same transaction as the status change, and a compare-and-set step is one atomic statement, so that of a worker and a client exactly one wins.
- Check it with the kit. It gives you a fresh, empty and unopened database, opens and closes it itself, and names the rule that you break:
await testing.check_database(lambda: MyDatabase(dsn))
What to notice¶
- Both ready-made databases pass this same kit. It is the contract, and the schema is the documentation.
- If you only need the results somewhere else, you want a result store, not a database.
Why a
Databaseis an abstract class with a kit: we cannot know your store, so we say exactly what it must do. See the database.
← A job queue · Part 6: Bring your own storage · An observer →