mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-03 15:55:45 +00:00

* WIP preparing 0.7.0 release * fix: re-enable examples * fix doctests in `sqlx-core` * cherry-pick CHANGELOG entry for 0.6.3 * add actions workflow for examples * fix(cli): close connection after running migrations * fix examples * fix(sqlite): fix parsing of URLs via `Any` * fix(example): don't let Postgres `listen` example run forever * fix Postgres `transaction` example
Mockable TODOs Example
Description
This example is based on the ideas in this blog post. The value here is that the business logic can be unit tested independently from the database layer. Otherwise it is identical to the todos example.
Setup
-
Run
docker-compose up -d
to run Postgres in the background. -
Declare the database URL, either by exporting it:
export DATABASE_URL="postgres://postgres:password@localhost/todos"
or by making a
.env
file:cp .env.example .env
-
Create the database.
$ sqlx db create
-
Run sql migrations
$ sqlx migrate run
Usage
Add a todo
cargo run -- add "todo description"
Complete a todo.
cargo run -- done <todo id>
List all todos
cargo run
Cleanup
To destroy the Postgres database, run:
docker-compose down --volumes