|
| 1 | +use diesel::prelude::*; |
| 2 | +use diesel::sqlite::{Sqlite, SqliteConnection}; |
| 3 | +use diesel_async::async_connection_wrapper::AsyncConnectionWrapper; |
| 4 | +use diesel_async::sync_connection_wrapper::SyncConnectionWrapper; |
| 5 | +use diesel_async::{AsyncConnection, RunQueryDsl, SimpleAsyncConnection}; |
| 6 | +use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; |
| 7 | + |
| 8 | +// ordinary diesel model setup |
| 9 | + |
| 10 | +table! { |
| 11 | + users { |
| 12 | + id -> Integer, |
| 13 | + name -> Text, |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug, Queryable, Selectable)] |
| 18 | +#[diesel(table_name = users)] |
| 19 | +struct User { |
| 20 | + id: i32, |
| 21 | + name: String, |
| 22 | +} |
| 23 | + |
| 24 | +const MIGRATIONS: EmbeddedMigrations = embed_migrations!(); |
| 25 | + |
| 26 | +type InnerConnection = SqliteConnection; |
| 27 | + |
| 28 | +type InnerDB = Sqlite; |
| 29 | + |
| 30 | +async fn establish(db_url: &str) -> ConnectionResult<SyncConnectionWrapper<InnerConnection>> { |
| 31 | + SyncConnectionWrapper::<SqliteConnection>::establish(db_url).await |
| 32 | +} |
| 33 | + |
| 34 | +async fn run_migrations<A>(async_connection: A) -> Result<(), Box<dyn std::error::Error>> |
| 35 | +where |
| 36 | + A: AsyncConnection<Backend = InnerDB> + 'static, |
| 37 | +{ |
| 38 | + let mut async_wrapper: AsyncConnectionWrapper<A> = |
| 39 | + AsyncConnectionWrapper::from(async_connection); |
| 40 | + |
| 41 | + tokio::task::spawn_blocking(move || { |
| 42 | + async_wrapper.run_pending_migrations(MIGRATIONS).unwrap(); |
| 43 | + }) |
| 44 | + .await |
| 45 | + .map_err(|e| Box::new(e) as Box<dyn std::error::Error>) |
| 46 | +} |
| 47 | + |
| 48 | +#[tokio::main] |
| 49 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 50 | + let db_url = std::env::var("DATABASE_URL").expect("Env var `DATABASE_URL` not set"); |
| 51 | + |
| 52 | + // create an async connection for the migrations |
| 53 | + let sync_wrapper: SyncConnectionWrapper<InnerConnection> = establish(&db_url).await?; |
| 54 | + run_migrations(sync_wrapper).await?; |
| 55 | + |
| 56 | + let mut sync_wrapper: SyncConnectionWrapper<InnerConnection> = establish(&db_url).await?; |
| 57 | + |
| 58 | + sync_wrapper.batch_execute("DELETE FROM users").await?; |
| 59 | + |
| 60 | + sync_wrapper |
| 61 | + .batch_execute("INSERT INTO users(id, name) VALUES (3, 'toto')") |
| 62 | + .await?; |
| 63 | + |
| 64 | + let data: Vec<User> = users::table |
| 65 | + .select(User::as_select()) |
| 66 | + .load(&mut sync_wrapper) |
| 67 | + .await?; |
| 68 | + println!("{data:?}"); |
| 69 | + |
| 70 | + diesel::delete(users::table) |
| 71 | + .execute(&mut sync_wrapper) |
| 72 | + .await?; |
| 73 | + |
| 74 | + diesel::insert_into(users::table) |
| 75 | + .values((users::id.eq(1), users::name.eq("iLuke"))) |
| 76 | + .execute(&mut sync_wrapper) |
| 77 | + .await?; |
| 78 | + |
| 79 | + let data: Vec<User> = users::table |
| 80 | + .filter(users::id.gt(0)) |
| 81 | + .or_filter(users::name.like("%Luke")) |
| 82 | + .select(User::as_select()) |
| 83 | + .load(&mut sync_wrapper) |
| 84 | + .await?; |
| 85 | + println!("{data:?}"); |
| 86 | + |
| 87 | + Ok(()) |
| 88 | +} |
0 commit comments