Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify todo example to use postgres and diesel_async #2648

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on: [push, pull_request]

env:
CARGO_TERM_COLOR: always
VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"

jobs:
test:
Expand Down Expand Up @@ -63,15 +64,20 @@ jobs:
brew install mysql-client libpq sqlite coreutils
echo "/usr/local/opt/mysql-client/bin" >> "$GITHUB_PATH"

- name: Export GitHub Actions cache environment variables (Windows, vcpkg)
if: matrix.platform.name == 'Windows'
uses: actions/github-script@v6
with:
script: |
core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');

# vcpkg --triplet x64-windows install libmysql libpq sqlite3 openssl
# + vcpkg/installed/vcpkg (in particular, the status file)
- name: Install Native Dependencies (Windows)
if: matrix.platform.name == 'Windows'
run: |
curl -fsS -o vcpkg.7z https://rocket.rs/static/vcpkg-2019-07-05.7z
7z x vcpkg.7z -y -bb0
xcopy .\vcpkg $env:VCPKG_INSTALLATION_ROOT /s /e /h /y /q
vcpkg integrate install
vcpkg --triplet x64-windows install libmysql libpq sqlite3 openssl
echo "VCPKGRS_DYNAMIC=1" >> "$env:GITHUB_ENV"
echo "VCPKG_ROOT=$env:VCPKG_INSTALLATION_ROOT" >> "$env:GITHUB_ENV"
echo "$env:VCPKG_INSTALLATION_ROOT\installed\x64-windows\lib" >> "$env:GITHUB_PATH"
Expand All @@ -82,6 +88,39 @@ jobs:
sudo apt-get update
sudo apt-get install -y libmysqlclient-dev libpq-dev libsqlite3-dev

- name: Start Postgres (macOS)
if: matrix.platform.name == 'macOS'
run: |
brew services start postgresql@14
RETRIES=5
until pg_isready > /dev/null 2>&1 || [[ $RETRIES -eq 0 ]]; do
echo "waiting for Postgres to start, $((RETRIES--)) remaining attempts"
sleep 1
done
psql postgres -c "CREATE ROLE rocket_runner PASSWORD 'password' SUPERUSER CREATEDB INHERIT LOGIN"
createdb -O rocket_runner epic_todo_database

- name: Start Postgres (Linux)
if: matrix.platform.name == 'Linux'
run: |
sudo systemctl start postgresql.service
RETRIES=5
until pg_isready > /dev/null 2>&1 || [[ $RETRIES -eq 0 ]]; do
echo "waiting for Postgres to start, $((RETRIES--)) remaining attempts"
sleep 1
done
sudo -u postgres psql -U postgres -c "CREATE ROLE rocket_runner PASSWORD 'password' SUPERUSER CREATEDB INHERIT LOGIN"
sudo -u postgres createdb -O rocket_runner epic_todo_database

- name: Start Postgres (Windows)
if: matrix.platform.name == 'Windows'
run: |
$pgService = Get-Service -Name postgresql*
Set-Service -InputObject $pgService -Status running -StartupType automatic
Start-Process -FilePath "$env:PGBIN\pg_isready" -Wait -PassThru
& $env:PGBIN\psql --command "CREATE ROLE rocket_runner PASSWORD 'password' SUPERUSER CREATEDB INHERIT LOGIN"
& $env:PGBIN\createdb -O rocket_runner epic_todo_database

- name: Install Rust
uses: dtolnay/rust-toolchain@master
id: toolchain
Expand Down
8 changes: 4 additions & 4 deletions examples/todo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ publish = false

[dependencies]
rocket = { path = "../../core/lib" }
diesel = { version = "2.0.0", features = ["sqlite", "r2d2"] }
diesel = { version = "2.0.0", features = ["postgres", "r2d2"] }
diesel_migrations = "2.0.0"

[dev-dependencies]
parking_lot = "0.12"
rand = "0.8"

[dependencies.rocket_sync_db_pools]
path = "../../contrib/sync_db_pools/lib/"
features = ["diesel_sqlite_pool"]
[dependencies.rocket_db_pools]
path = "../../contrib/db_pools/lib/"
features = ["diesel_postgres"]

[dependencies.rocket_dyn_templates]
path = "../../contrib/dyn_templates"
Expand Down
6 changes: 4 additions & 2 deletions examples/todo/Rocket.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[default]
template_dir = "static"

[default.databases.sqlite_database]
url = "db/db.sqlite"
[default.databases.epic_todo_database]
url = "postgresql://rocket_runner:password@localhost:5432/epic_todo_database"
max_connections = 1
connect_timeout = 5
1 change: 1 addition & 0 deletions examples/todo/db/DB_LIVES_HERE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
db does not live here :(
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.

DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
DROP FUNCTION IF EXISTS diesel_set_updated_at();
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.




-- Sets up a trigger for the given table to automatically set a column called
-- `updated_at` whenever the row is modified (unless `updated_at` was included
-- in the modified columns)
--
-- # Example
--
-- ```sql
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
--
-- SELECT diesel_manage_updated_at('users');
-- ```
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
BEGIN
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
BEGIN
IF (
NEW IS DISTINCT FROM OLD AND
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
) THEN
NEW.updated_at := current_timestamp;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
CREATE TABLE tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
id SERIAL PRIMARY KEY,
description VARCHAR NOT NULL,
completed BOOLEAN NOT NULL DEFAULT 0
completed BOOLEAN NOT NULL DEFAULT FALSE
);

INSERT INTO tasks (description) VALUES ("demo task");
INSERT INTO tasks (description) VALUES ("demo task2");
INSERT INTO tasks (description) VALUES ('demo task');
INSERT INTO tasks (description) VALUES ('demo task2');
52 changes: 30 additions & 22 deletions examples/todo/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_sync_db_pools;
#[macro_use] extern crate diesel;

#[cfg(test)]
mod tests;
Expand All @@ -13,13 +11,15 @@ use rocket::response::{Flash, Redirect};
use rocket::serde::Serialize;
use rocket::form::Form;
use rocket::fs::{FileServer, relative};
use rocket_db_pools::{Connection, Database};

use rocket_dyn_templates::Template;

use crate::task::{Task, Todo};

#[database("sqlite_database")]
pub struct DbConn(diesel::SqliteConnection);
#[derive(Database)]
#[database("epic_todo_database")]
pub struct Db(rocket_db_pools::diesel::PgPool);

#[derive(Debug, Serialize)]
#[serde(crate = "rocket::serde")]
Expand All @@ -29,14 +29,14 @@ struct Context {
}

impl Context {
pub async fn err<M: std::fmt::Display>(conn: &DbConn, msg: M) -> Context {
pub async fn err<M: std::fmt::Display>(conn: &mut Connection<Db>, msg: M) -> Context {
Context {
flash: Some(("error".into(), msg.to_string())),
tasks: Task::all(conn).await.unwrap_or_default()
}
}

pub async fn raw(conn: &DbConn, flash: Option<(String, String)>) -> Context {
pub async fn raw(conn: &mut Connection<Db>, flash: Option<(String, String)>) -> Context {
match Task::all(conn).await {
Ok(tasks) => Context { flash, tasks },
Err(e) => {
Expand All @@ -51,11 +51,11 @@ impl Context {
}

#[post("/", data = "<todo_form>")]
async fn new(todo_form: Form<Todo>, conn: DbConn) -> Flash<Redirect> {
async fn new(todo_form: Form<Todo>, mut conn: Connection<Db>) -> Flash<Redirect> {
let todo = todo_form.into_inner();
if todo.description.is_empty() {
Flash::error(Redirect::to("/"), "Description cannot be empty.")
} else if let Err(e) = Task::insert(todo, &conn).await {
} else if let Err(e) = Task::insert(todo, &mut conn).await {
error_!("DB insertion error: {}", e);
Flash::error(Redirect::to("/"), "Todo could not be inserted due an internal error.")
} else {
Expand All @@ -64,50 +64,58 @@ async fn new(todo_form: Form<Todo>, conn: DbConn) -> Flash<Redirect> {
}

#[put("/<id>")]
async fn toggle(id: i32, conn: DbConn) -> Result<Redirect, Template> {
match Task::toggle_with_id(id, &conn).await {
async fn toggle(id: i32, mut conn: Connection<Db>) -> Result<Redirect, Template> {
match Task::toggle_with_id(id, &mut conn).await {
Ok(_) => Ok(Redirect::to("/")),
Err(e) => {
error_!("DB toggle({}) error: {}", id, e);
Err(Template::render("index", Context::err(&conn, "Failed to toggle task.").await))
Err(Template::render("index", Context::err(&mut conn, "Failed to toggle task.").await))
}
}
}

#[delete("/<id>")]
async fn delete(id: i32, conn: DbConn) -> Result<Flash<Redirect>, Template> {
match Task::delete_with_id(id, &conn).await {
async fn delete(id: i32, mut conn: Connection<Db>) -> Result<Flash<Redirect>, Template> {
match Task::delete_with_id(id, &mut conn).await {
Ok(_) => Ok(Flash::success(Redirect::to("/"), "Todo was deleted.")),
Err(e) => {
error_!("DB deletion({}) error: {}", id, e);
Err(Template::render("index", Context::err(&conn, "Failed to delete task.").await))
Err(Template::render("index", Context::err(&mut conn, "Failed to delete task.").await))
}
}
}

#[get("/")]
async fn index(flash: Option<FlashMessage<'_>>, conn: DbConn) -> Template {
async fn index(flash: Option<FlashMessage<'_>>, mut conn: Connection<Db>) -> Template {
let flash = flash.map(FlashMessage::into_inner);
Template::render("index", Context::raw(&conn, flash).await)
Template::render("index", Context::raw(&mut conn, flash).await)
}

async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
use diesel::Connection;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};

const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");

DbConn::get_one(&rocket).await
.expect("database connection")
.run(|conn| { conn.run_pending_migrations(MIGRATIONS).expect("diesel migrations"); })
.await;
let config: rocket_db_pools::Config = rocket
.figment()
.extract_inner("databases.epic_todo_database")
.expect("Db not configured");

rocket::tokio::task::spawn_blocking(move || {
diesel::PgConnection::establish(&config.url)
.expect("No database")
.run_pending_migrations(MIGRATIONS)
.expect("Invalid migrations");
})
.await.expect("tokio doesn't work");

rocket
}

#[launch]
fn rocket() -> _ {
rocket::build()
.attach(DbConn::fairing())
.attach(Db::init())
.attach(Template::fairing())
.attach(AdHoc::on_ignite("Run Migrations", run_migrations))
.mount("/", FileServer::from(relative!("static")))
Expand Down
41 changes: 18 additions & 23 deletions examples/todo/src/task.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use rocket::serde::Serialize;
use diesel::{self, result::QueryResult, prelude::*};
use rocket_db_pools::diesel::RunQueryDsl;

mod schema {
table! {
diesel::table! {
tasks {
id -> Nullable<Integer>,
description -> Text,
Expand All @@ -13,7 +14,7 @@ mod schema {

use self::schema::tasks;

use crate::DbConn;
type DbConn = rocket_db_pools::diesel::AsyncPgConnection;

#[derive(Serialize, Queryable, Insertable, Debug, Clone)]
#[serde(crate = "rocket::serde")]
Expand All @@ -31,41 +32,35 @@ pub struct Todo {
}

impl Task {
pub async fn all(conn: &DbConn) -> QueryResult<Vec<Task>> {
conn.run(|c| {
tasks::table.order(tasks::id.desc()).load::<Task>(c)
}).await
pub async fn all(conn: &mut DbConn) -> QueryResult<Vec<Task>> {
tasks::table.order(tasks::id.desc()).load::<Task>(conn).await
}

/// Returns the number of affected rows: 1.
pub async fn insert(todo: Todo, conn: &DbConn) -> QueryResult<usize> {
conn.run(|c| {
let t = Task { id: None, description: todo.description, completed: false };
diesel::insert_into(tasks::table).values(&t).execute(c)
}).await
pub async fn insert(todo: Todo, conn: &mut DbConn) -> QueryResult<usize> {
let t = Task { id: None, description: todo.description, completed: false };
diesel::insert_into(tasks::table).values(&t).execute(conn).await
}

/// Returns the number of affected rows: 1.
pub async fn toggle_with_id(id: i32, conn: &DbConn) -> QueryResult<usize> {
conn.run(move |c| {
let task = tasks::table.filter(tasks::id.eq(id)).get_result::<Task>(c)?;
let new_status = !task.completed;
let updated_task = diesel::update(tasks::table.filter(tasks::id.eq(id)));
updated_task.set(tasks::completed.eq(new_status)).execute(c)
}).await
pub async fn toggle_with_id(id: i32, conn: &mut DbConn) -> QueryResult<usize> {
let task = tasks::table.filter(tasks::id.eq(id)).get_result::<Task>(conn).await?;
let new_status = !task.completed;
let updated_task = diesel::update(tasks::table.filter(tasks::id.eq(id)));
updated_task.set(tasks::completed.eq(new_status)).execute(conn).await
}

/// Returns the number of affected rows: 1.
pub async fn delete_with_id(id: i32, conn: &DbConn) -> QueryResult<usize> {
conn.run(move |c| diesel::delete(tasks::table)
pub async fn delete_with_id(id: i32, conn: &mut DbConn) -> QueryResult<usize> {
diesel::delete(tasks::table)
.filter(tasks::id.eq(id))
.execute(c))
.execute(conn)
.await
}

/// Returns the number of affected rows.
#[cfg(test)]
pub async fn delete_all(conn: &DbConn) -> QueryResult<usize> {
conn.run(|c| diesel::delete(tasks::table).execute(c)).await
pub async fn delete_all(conn: &mut DbConn) -> QueryResult<usize> {
diesel::delete(tasks::table).execute(conn).await
}
}
Loading
Loading