From f191645467e3724ee685508ef92dd719f9d702e9 Mon Sep 17 00:00:00 2001 From: Sam Samai Date: Fri, 27 Aug 2021 22:31:54 +1000 Subject: [PATCH] Run migration via try_on_ignite --- examples/rocket_example/src/sqlx/mod.rs | 143 ++++-------------------- 1 file changed, 22 insertions(+), 121 deletions(-) diff --git a/examples/rocket_example/src/sqlx/mod.rs b/examples/rocket_example/src/sqlx/mod.rs index 2909ed9d..42431920 100644 --- a/examples/rocket_example/src/sqlx/mod.rs +++ b/examples/rocket_example/src/sqlx/mod.rs @@ -1,4 +1,4 @@ -use rocket::fairing::AdHoc; +use rocket::fairing::{self, AdHoc}; use rocket::response::status::Created; use rocket::serde::json::Json; use rocket::{futures, Build, Rocket}; @@ -81,131 +81,32 @@ async fn destroy(conn: Connection) -> Result<()> { Ok(()) } -// async fn run_migrations(rocket: Rocket) -> fairing::Result { -// use crate::rocket_db_pools::Pool; -// // match Db::fetch(&rocket) { -// // Some(db) => match sqlx::migrate!("db/sqlx/migrations").run(&**db).await { -// // Ok(_) => Ok(rocket), -// // Err(e) => { -// // error!("Failed to initialize SQLx database: {}", e); -// // Err(rocket) -// // } -// // }, -// // None => Err(rocket), -// // } -// // let conn = Db::get(&rocket).await.expect("database connection"); - -// match Db::fetch(&rocket) { -// Some(db) => match setup::create_post_table(db.get().await().expect("database connection")).await { -// Ok(_) => { -// println!("rocket: {:#?}", rocket); - -// Ok(rocket) -// } -// Err(e) => { -// error!("Failed to initialize SQLx database: {}", e); -// Err(rocket) -// } -// }, -// None => Err(rocket), -// } -// // Ok(rocket) -// } +async fn run_migrations(rocket: Rocket) -> fairing::Result { + let con = sea_orm::Database::connect("mysql://root:@localhost/rocket_example") + .await + .unwrap(); + let create_post_table = con + .execute(Statement::from_string( + DatabaseBackend::MySql, + r#" + CREATE TABLE posts ( + id int NOT NULL AUTO_INCREMENT, + title VARCHAR(255) NOT NULL, + text VARCHAR(255) NOT NULL, + PRIMARY KEY (id) + )"# + .to_owned(), + )) + .await; + println!("create_post_table: {:#?}", create_post_table); + Ok(rocket) +} pub fn stage() -> AdHoc { AdHoc::on_ignite("SQLx Stage", |rocket| async { rocket .attach(Db::init()) - .attach(AdHoc::try_on_ignite("Create init post", |rocket| async { - let con = sea_orm::Database::connect("mysql://root:@localhost/rocket_example") - .await - .unwrap(); - // let res = sqlx::query( - // r#" - // CREATE TABLE posts ( - // id INTEGER PRIMARY KEY AUTOINCREMENT, - // title VARCHAR NOT NULL, - // text VARCHAR NOT NULL, - // published BOOLEAN NOT NULL DEFAULT 0 - // )"#, - // ) - // .execute(&**db) - // .await; - let create_post_table = con - .execute(Statement::from_string( - DatabaseBackend::MySql, - r#" - CREATE TABLE posts ( - id int NOT NULL AUTO_INCREMENT, - title VARCHAR(255) NOT NULL, - text VARCHAR(255) NOT NULL, - PRIMARY KEY (id) - )"# - .to_owned(), - )) - .await; - println!("create_post_table: {:#?}", create_post_table); - - let create_post = con - .execute(Statement::from_string( - DatabaseBackend::MySql, - "INSERT INTO posts (title, text) VALUES ('a post', 'content of a post')" - .to_owned(), - )) - .await; - println!("create_post: {:#?}", create_post); - - // println!("all_posts: {:#?}", all_posts); - - // let res2 = sqlx::query( - // r#" - // INSERT INTO posts (title, text) VALUES ('a post', 'content of a post') - // "#, - // ) - // .execute(&**db) - // .await; - // println!("res2: {:#?}", res2); - - // Db::fetch(&rocket) - // .run(|db| { - // sqlx::query("DELETE FROM table").execute(&pool).await; - - // // conn.execute( - // // r#" - // // CREATE TABLE posts ( - // // id INTEGER PRIMARY KEY AUTOINCREMENT, - // // title VARCHAR NOT NULL, - // // text VARCHAR NOT NULL, - // // published BOOLEAN NOT NULL DEFAULT 0 - // // )"#, - // // params![], - // // ) - // }) - // .await - // .expect("can init rusqlite DB"); - Ok(rocket) - - // match Db::fetch(&rocket) { - // Some(db) => { - // println!("db: {:#?}", db); - // println!("&**db: {:#?}", &**db); - - // Ok(rocket) - // } - // None => Err(rocket), - // } - })) + .attach(AdHoc::try_on_ignite("SQLx Migrations", run_migrations)) .mount("/sqlx", routes![create, delete, destroy, list, read,]) }) } - -// pub async fn create_post(db: &DbConn) { -// let post = post::ActiveModel { -// title: Set("Post One".to_owned()), -// text: Set("post content 1".to_owned()), -// ..Default::default() -// } -// .save(db) -// .await -// .expect("could not insert post"); -// }