Rocket example to use the clone interface

This commit is contained in:
Chris Tsang 2021-09-22 16:51:50 +08:00
parent c80621916a
commit aeea07c8ba
2 changed files with 6 additions and 7 deletions

View File

@ -156,9 +156,8 @@ pub fn not_found(req: &Request<'_>) -> Template {
} }
async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result { async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
let db_url = Db::fetch(&rocket).unwrap().db_url.clone(); let conn = &Db::fetch(&rocket).unwrap().conn;
let conn = sea_orm::Database::connect(&db_url).await.unwrap(); let _ = setup::create_post_table(conn).await;
let _ = setup::create_post_table(&conn).await;
Ok(rocket) Ok(rocket)
} }

View File

@ -3,7 +3,7 @@ use rocket_db_pools::{rocket::figment::Figment, Config};
#[derive(Debug)] #[derive(Debug)]
pub struct RocketDbPool { pub struct RocketDbPool {
pub db_url: String, pub conn: sea_orm::DatabaseConnection,
} }
#[async_trait] #[async_trait]
@ -14,14 +14,14 @@ impl rocket_db_pools::Pool for RocketDbPool {
async fn init(figment: &Figment) -> Result<Self, Self::Error> { async fn init(figment: &Figment) -> Result<Self, Self::Error> {
let config = figment.extract::<Config>().unwrap(); let config = figment.extract::<Config>().unwrap();
let db_url = config.url; let conn = sea_orm::Database::connect(&config.url).await.unwrap();
Ok(RocketDbPool { Ok(RocketDbPool {
db_url: db_url.to_owned(), conn,
}) })
} }
async fn get(&self) -> Result<Self::Connection, Self::Error> { async fn get(&self) -> Result<Self::Connection, Self::Error> {
Ok(sea_orm::Database::connect(&self.db_url).await.unwrap()) Ok(self.conn.clone())
} }
} }