forked from mehcode/tokio-diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use `task::block_in_place` with allows a number of `'static` lifetimes to be removed. This now works outside a task spawn due to tokio-rs/tokio#2410 with the restriction that a threaded runtime is used. Fixes mehcode#9. Signed-off-by: Joe Grund <jgrund@whamcloud.io>
- Loading branch information
Showing
5 changed files
with
148 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
name: ci | ||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
check: | ||
name: Check | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v1 | ||
- uses: actions-rs/toolchain@v1 | ||
with: | ||
toolchain: stable | ||
override: true | ||
- uses: actions-rs/cargo@v1 | ||
with: | ||
command: check | ||
args: --locked | ||
|
||
rustfmt: | ||
name: Format | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v1 | ||
- uses: actions-rs/toolchain@v1 | ||
with: | ||
toolchain: stable | ||
override: true | ||
- run: rustup component add rustfmt | ||
- uses: actions-rs/cargo@v1 | ||
with: | ||
command: fmt | ||
args: --all -- --check | ||
|
||
test: | ||
name: Test Suite | ||
runs-on: ubuntu-latest | ||
services: | ||
postgres: | ||
image: postgres | ||
env: | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_DB: postgres | ||
ports: | ||
- 5432:5432 | ||
# needed because the postgres container does not provide a healthcheck | ||
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 | ||
steps: | ||
- name: Checkout sources | ||
uses: actions/checkout@v1 | ||
|
||
- name: Install PostgreSQL client | ||
run: sudo apt-get -yqq install libpq-dev | ||
|
||
- name: Install toolchain | ||
uses: actions-rs/toolchain@v1 | ||
with: | ||
toolchain: stable | ||
override: true | ||
|
||
- name: Run cargo test | ||
uses: actions-rs/cargo@v1 | ||
with: | ||
command: test | ||
args: --locked | ||
env: | ||
POSTGRES_HOST: postgres | ||
POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CREATE table users (id uuid); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#[macro_use] | ||
extern crate diesel; | ||
|
||
use diesel::{ | ||
prelude::*, | ||
r2d2::{ConnectionManager, Pool}, | ||
sql_query | ||
}; | ||
use std::{env, error::Error}; | ||
use tokio_diesel::*; | ||
use uuid::Uuid; | ||
|
||
// Schema | ||
table! { | ||
users (id) { | ||
id -> Uuid, | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
#[tokio::test(threaded_scheduler)] | ||
async fn test_db_ops() -> Result<(), Box<dyn Error>> { | ||
let hostname = env::var("POSTGRES_HOST").unwrap_or_else(|_| "localhost".into()); | ||
|
||
let manager = | ||
ConnectionManager::<PgConnection>::new(&format!("postgres://postgres@{}/tokio_diesel__test", hostname)); | ||
let pool = Pool::builder().build(manager)?; | ||
|
||
let _ = sql_query(include_str!("./create_users.sql")).execute_async(&pool).await; | ||
|
||
// Add | ||
println!("add a user"); | ||
diesel::insert_into(users::table) | ||
.values(users::id.eq(Uuid::new_v4())) | ||
.execute_async(&pool) | ||
.await?; | ||
|
||
// Count | ||
let num_users: i64 = users::table.count().get_result_async(&pool).await?; | ||
println!("now there are {:?} users", num_users); | ||
|
||
assert!(num_users > 0); | ||
|
||
Ok(()) | ||
} |