Skip to content

Commit 96b76dc

Browse files
committed
update todo examples to 0.4 and use migrations
* update tests/x.py * add examples/x.py
1 parent 820618f commit 96b76dc

File tree

26 files changed

+480
-236
lines changed

26 files changed

+480
-236
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@ target/
99

1010
# Environment
1111
.env
12-
!tests/.env

examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sqlite/todos/todos.db

examples/mysql/todos/README.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
# TODOs Example
22

3+
## Setup
4+
5+
1. Declare the database URL
6+
7+
```
8+
export DATABASE_URL="mysql://root:password@localhost/todos"
9+
```
10+
11+
2. Create the database.
12+
13+
```
14+
$ sqlx db create
15+
```
16+
17+
3. Run sql migrations
18+
19+
```
20+
$ sqlx migrate run
21+
```
22+
323
## Usage
424
5-
Declare the database URL:
25+
Add a todo
626
727
```
8-
export DATABASE_URL="mysql://localhost/todos"
28+
cargo run -- add "todo description"
929
```
1030
11-
Connect to `mysql` and create the database:
31+
Complete a todo.
1232
1333
```
14-
$ mysql
15-
mysql> CREATE DATABASE todos;
34+
cargo run -- done <todo id>
1635
```
1736
18-
Load the database schema (using the MySQL CLI interface thats already open):
37+
List all todos
1938
2039
```
21-
mysql> USE todos;
22-
mysql> source schema.sql
40+
cargo run
2341
```
24-
25-
Use `exit` to exit the MySQL CLI. Then, to run this example:
26-
27-
- Add a todo: `cargo run -- add "todo description"`
28-
- Complete a todo: `cargo run -- done <todo id>`
29-
- List all todos: `cargo run`
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
CREATE TABLE IF NOT EXISTS todos (
1+
CREATE TABLE IF NOT EXISTS todos
2+
(
23
id BIGINT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
3-
description TEXT NOT NULL,
4+
description TEXT NOT NULL,
45
done BOOLEAN NOT NULL DEFAULT FALSE
56
);

examples/mysql/todos/src/main.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use anyhow::Context;
2-
use sqlx::MySqlPool;
1+
use sqlx::mysql::MySqlPool;
2+
use sqlx::Done;
33
use std::env;
44
use structopt::StructOpt;
55

@@ -18,10 +18,7 @@ enum Command {
1818
#[async_std::main]
1919
#[paw::main]
2020
async fn main(args: Args) -> anyhow::Result<()> {
21-
let pool = MySqlPool::new(
22-
&env::var("DATABASE_URL").context("`DATABASE_URL` must be set to run this example")?,
23-
)
24-
.await?;
21+
let pool = MySqlPool::connect(&env::var("DATABASE_URL")?).await?;
2522

2623
match args.cmd {
2724
Some(Command::Add { description }) => {
@@ -47,22 +44,19 @@ async fn main(args: Args) -> anyhow::Result<()> {
4744
}
4845

4946
async fn add_todo(pool: &MySqlPool, description: String) -> anyhow::Result<u64> {
50-
// Insert the TODO, then obtain the ID of this row
51-
sqlx::query!(
47+
// Insert the task, then obtain the ID of this row
48+
let todo_id = sqlx::query!(
5249
r#"
5350
INSERT INTO todos ( description )
5451
VALUES ( ? )
5552
"#,
5653
description
5754
)
5855
.execute(pool)
59-
.await?;
56+
.await?
57+
.last_insert_id();
6058

61-
let rec: (u64,) = sqlx::query_as("SELECT LAST_INSERT_ID()")
62-
.fetch_one(pool)
63-
.await?;
64-
65-
Ok(rec.0)
59+
Ok(todo_id)
6660
}
6761

6862
async fn complete_todo(pool: &MySqlPool, id: u64) -> anyhow::Result<bool> {
@@ -75,7 +69,8 @@ WHERE id = ?
7569
id
7670
)
7771
.execute(pool)
78-
.await?;
72+
.await?
73+
.rows_affected();
7974

8075
Ok(rows_affected > 0)
8176
}

examples/postgres/todos/README.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
# TODOs Example
22

3+
## Setup
4+
5+
1. Declare the database URL
6+
7+
```
8+
export DATABASE_URL="postgres://postgres:password@localhost/todos"
9+
```
10+
11+
2. Create the database.
12+
13+
```
14+
$ sqlx db create
15+
```
16+
17+
3. Run sql migrations
18+
19+
```
20+
$ sqlx migrate run
21+
```
22+
323
## Usage
424
5-
Declare the database URL:
25+
Add a todo
626
727
```
8-
export DATABASE_URL="postgres://postgres@localhost/todos"
28+
cargo run -- add "todo description"
929
```
1030
11-
Create the database:
31+
Complete a todo.
1232
1333
```
14-
createdb -U postgres todos
34+
cargo run -- done <todo id>
1535
```
1636
17-
Load the database schema:
37+
List all todos
1838
1939
```
20-
psql -d "$DATABASE_URL" -f ./schema.sql
40+
cargo run
2141
```
22-
23-
Run:
24-
25-
- Add a todo: `cargo run -- add "todo description"`
26-
- Complete a todo: `cargo run -- done <todo id>`
27-
- List all todos: `cargo run`
28-
29-
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
CREATE TABLE IF NOT EXISTS todos (
1+
CREATE TABLE IF NOT EXISTS todos
2+
(
23
id BIGSERIAL PRIMARY KEY,
3-
description TEXT NOT NULL,
4+
description TEXT NOT NULL,
45
done BOOLEAN NOT NULL DEFAULT FALSE
56
);

examples/postgres/todos/src/main.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use anyhow::Context;
2-
use sqlx::PgPool;
1+
use sqlx::postgres::PgPool;
2+
use sqlx::Done;
33
use std::env;
44
use structopt::StructOpt;
55

@@ -18,10 +18,7 @@ enum Command {
1818
#[async_std::main]
1919
#[paw::main]
2020
async fn main(args: Args) -> anyhow::Result<()> {
21-
let mut pool = PgPool::new(
22-
&env::var("DATABASE_URL").context("`DATABASE_URL` must be set to run this example")?,
23-
)
24-
.await?;
21+
let pool = PgPool::connect(&env::var("DATABASE_URL")?).await?;
2522

2623
match args.cmd {
2724
Some(Command::Add { description }) => {
@@ -39,14 +36,14 @@ async fn main(args: Args) -> anyhow::Result<()> {
3936
}
4037
None => {
4138
println!("Printing list of all todos");
42-
list_todos(&mut pool).await?;
39+
list_todos(&pool).await?;
4340
}
4441
}
4542

4643
Ok(())
4744
}
4845

49-
async fn add_todo(mut pool: &PgPool, description: String) -> anyhow::Result<i64> {
46+
async fn add_todo(pool: &PgPool, description: String) -> anyhow::Result<i64> {
5047
let rec = sqlx::query!(
5148
r#"
5249
INSERT INTO todos ( description )
@@ -55,13 +52,13 @@ RETURNING id
5552
"#,
5653
description
5754
)
58-
.fetch_one(&mut pool)
55+
.fetch_one(pool)
5956
.await?;
6057

6158
Ok(rec.id)
6259
}
6360

64-
async fn complete_todo(mut pool: &PgPool, id: i64) -> anyhow::Result<bool> {
61+
async fn complete_todo(pool: &PgPool, id: i64) -> anyhow::Result<bool> {
6562
let rows_affected = sqlx::query!(
6663
r#"
6764
UPDATE todos
@@ -70,8 +67,9 @@ WHERE id = $1
7067
"#,
7168
id
7269
)
73-
.execute(&mut pool)
74-
.await?;
70+
.execute(pool)
71+
.await?
72+
.rows_affected();
7573

7674
Ok(rows_affected > 0)
7775
}

examples/sqlite/todos/README.md

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
11
# TODOs Example
22

3+
## Setup
4+
5+
1. Declare the database URL
6+
7+
```
8+
export DATABASE_URL="sqlite:todos.db"
9+
```
10+
11+
2. Create the database.
12+
13+
```
14+
$ sqlx db create
15+
```
16+
17+
3. Run sql migrations
18+
19+
```
20+
$ sqlx migrate run
21+
```
22+
323
## Usage
424
5-
Declare the database URL:
25+
Add a todo
626
727
```
8-
export DATABASE_URL="sqlite:///path/to/this/directory/todos.db"
28+
cargo run -- add "todo description"
929
```
1030
11-
Create the database:
31+
Complete a todo.
1232
1333
```
14-
sqlite3 todos.db
34+
cargo run -- done <todo id>
1535
```
1636
17-
Load the database schema (using the SQLite CLI interface opened from the previous command):
37+
List all todos
1838
1939
```
20-
sqlite> .read schema.sql
40+
cargo run
2141
```
22-
23-
Use `.exit` to leave the SQLite CLI. Then, to run this example:
24-
25-
- Add a todo: `cargo run -- add "todo description"`
26-
- Complete a todo: `cargo run -- done <todo id>`
27-
- List all todos: `cargo run`
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE IF NOT EXISTS todos
2+
(
3+
id INTEGER PRIMARY KEY NOT NULL,
4+
description TEXT NOT NULL,
5+
done BOOLEAN NOT NULL DEFAULT 0
6+
);

examples/sqlite/todos/schema.sql

Lines changed: 0 additions & 5 deletions
This file was deleted.

examples/sqlite/todos/src/main.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use anyhow::Context;
2-
use sqlx::SqlitePool;
1+
use sqlx::sqlite::SqlitePool;
2+
use sqlx::Done;
33
use std::env;
44
use structopt::StructOpt;
55

@@ -18,10 +18,7 @@ enum Command {
1818
#[async_std::main]
1919
#[paw::main]
2020
async fn main(args: Args) -> anyhow::Result<()> {
21-
let pool = SqlitePool::new(
22-
&env::var("DATABASE_URL").context("`DATABASE_URL` must be set to run this example")?,
23-
)
24-
.await?;
21+
let pool = SqlitePool::connect(&env::var("DATABASE_URL")?).await?;
2522

2623
match args.cmd {
2724
Some(Command::Add { description }) => {
@@ -49,35 +46,33 @@ async fn main(args: Args) -> anyhow::Result<()> {
4946
async fn add_todo(pool: &SqlitePool, description: String) -> anyhow::Result<i64> {
5047
let mut conn = pool.acquire().await?;
5148

52-
// Insert the TODO, then obtain the ID of this row
53-
sqlx::query!(
49+
// Insert the task, then obtain the ID of this row
50+
let id = sqlx::query!(
5451
r#"
5552
INSERT INTO todos ( description )
56-
VALUES ( $1 )
53+
VALUES ( ?1 )
5754
"#,
5855
description
5956
)
6057
.execute(&mut conn)
61-
.await?;
58+
.await?
59+
.last_insert_rowid();
6260

63-
let rec: (i64,) = sqlx::query_as("SELECT last_insert_rowid()")
64-
.fetch_one(&mut conn)
65-
.await?;
66-
67-
Ok(rec.0)
61+
Ok(id)
6862
}
6963

7064
async fn complete_todo(pool: &SqlitePool, id: i64) -> anyhow::Result<bool> {
7165
let rows_affected = sqlx::query!(
7266
r#"
7367
UPDATE todos
7468
SET done = TRUE
75-
WHERE id = $1
69+
WHERE id = ?1
7670
"#,
7771
id
7872
)
7973
.execute(pool)
80-
.await?;
74+
.await?
75+
.rows_affected();
8176

8277
Ok(rows_affected > 0)
8378
}

0 commit comments

Comments
 (0)