Get db_url from Db in run_migrations

This commit is contained in:
Sam Samai 2021-09-02 21:33:28 +10:00
parent a5ea54df61
commit b825df1331
2 changed files with 4 additions and 8 deletions

View File

@ -3,6 +3,7 @@ use rocket::form::{Context, Form};
use rocket::fs::{relative, FileServer};
use rocket::response::{Flash, Redirect};
use rocket::serde::json::Json;
use rocket::Config;
use rocket::{Build, Request, Rocket};
use rocket_db_pools::{sqlx, Connection, Database};
use rocket_dyn_templates::{context, Template};
@ -102,13 +103,8 @@ pub fn not_found(req: &Request<'_>) -> Template {
}
async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
#[cfg(feature = "sqlx-mysql")]
let db_url = "mysql://root:@localhost/rocket_example";
#[cfg(feature = "sqlx-postgres")]
let db_url = "postgres://root:root@localhost/rocket_example";
let conn = sea_orm::Database::connect(db_url).await.unwrap();
let db_url = Db::fetch(&rocket).unwrap().db_url.clone();
let conn = sea_orm::Database::connect(&db_url).await.unwrap();
let _create_post_table = setup::create_post_table(&conn).await;
Ok(rocket)
}

View File

@ -3,7 +3,7 @@ use rocket_db_pools::{rocket::figment::Figment, Config, Error};
#[derive(Debug)]
pub struct RocketDbPool {
db_url: String,
pub db_url: String,
}
#[async_trait]