Skip to content

Commit bd8b96f

Browse files
committed
Adding creating a post, example coming from https://diesel.rs/guides/getting-started
1 parent 0391aa2 commit bd8b96f

File tree

9 files changed

+161
-3
lines changed

9 files changed

+161
-3
lines changed

Cargo.lock

Lines changed: 85 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/embedded_async_diesel_r2d2/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ version.workspace = true
99
postgresql_embedded = { path = "../../postgresql_embedded" }
1010
tokio = { workspace = true, features = ["full"] }
1111
diesel = { version = "2.2.1", features = ["postgres", "r2d2"] }
12-
r2d2_postgres = "0.18.1"
12+
r2d2_postgres = "0.18.1"
13+
diesel_migrations = { version = "2.2.0", features = ["postgres"] }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# For documentation on how to configure this file,
2+
# see https://diesel.rs/guides/configuring-diesel-cli
3+
4+
[print_schema]
5+
file = "src/schema.rs"
6+
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
7+
8+
[migrations_directory]
9+
dir = "./migrations"

examples/embedded_async_diesel_r2d2/migrations/.keep

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE posts
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE posts (
2+
id SERIAL PRIMARY KEY,
3+
title VARCHAR NOT NULL,
4+
body TEXT NOT NULL,
5+
published BOOLEAN NOT NULL DEFAULT FALSE
6+
)

examples/embedded_async_diesel_r2d2/src/main.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,66 @@
33

44
use diesel::r2d2::{ConnectionManager, Pool};
55
use diesel::PgConnection;
6+
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
67
use postgresql_embedded::{PostgreSQL, Result, Settings, VersionReq};
8+
mod models;
9+
pub mod schema;
710

11+
use self::models::*;
12+
use diesel::prelude::*;
13+
14+
const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations/");
815
#[tokio::main]
916
async fn main() -> Result<()> {
1017
let settings = Settings {
1118
version: VersionReq::parse("=16.4.0")?,
19+
username: "postgres".to_string(),
20+
password: "postgres".to_string(),
1221
..Default::default()
1322
};
1423
let mut postgresql = PostgreSQL::new(settings);
1524
postgresql.setup().await?;
1625
postgresql.start().await?;
1726

18-
let database_name = "test";
27+
let database_name = "diesel_demo";
1928
postgresql.create_database(database_name).await?;
2029
postgresql.database_exists(database_name).await?;
2130

2231
{
2332
let database_url = postgresql.settings().url(database_name);
2433
let manager = ConnectionManager::<PgConnection>::new(database_url);
25-
let _pool = Pool::builder()
34+
let pool = Pool::builder()
2635
.test_on_check_out(true)
2736
.build(manager)
2837
.expect("Could not build connection pool");
38+
let mut mig_run = pool.clone().get().unwrap();
39+
mig_run.run_pending_migrations(MIGRATIONS).unwrap();
40+
41+
let post = create_post(
42+
&mut pool.get().unwrap(),
43+
"My First Post",
44+
"This is my firs post",
45+
);
46+
println!("Post '{}' created", post.title);
2947
}
3048

3149
postgresql.drop_database(database_name).await?;
3250

3351
postgresql.stop().await
3452
}
3553

54+
pub fn create_post(conn: &mut PgConnection, title: &str, body: &str) -> Post {
55+
use crate::schema::posts;
56+
57+
let new_post = NewPost { title, body };
58+
59+
diesel::insert_into(posts::table)
60+
.values(&new_post)
61+
.returning(Post::as_returning())
62+
.get_result(conn)
63+
.expect("Error saving new post")
64+
}
65+
3666
#[cfg(test)]
3767
mod test {
3868
use super::*;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use diesel::prelude::*;
2+
3+
#[derive(Queryable, Selectable)]
4+
#[diesel(table_name = crate::schema::posts)]
5+
#[diesel(check_for_backend(diesel::pg::Pg))]
6+
pub struct Post {
7+
pub id: i32,
8+
pub title: String,
9+
pub body: String,
10+
pub published: bool,
11+
}
12+
13+
#[derive(Insertable)]
14+
#[diesel(table_name = crate::schema::posts)]
15+
pub struct NewPost<'a> {
16+
pub title: &'a str,
17+
pub body: &'a str,
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
diesel::table! {
2+
posts (id) {
3+
id -> Int4,
4+
title -> Varchar,
5+
body -> Text,
6+
published -> Bool,
7+
}
8+
}

0 commit comments

Comments
 (0)